I want to generate a variable month
that has the month and year together as 2013M01
.
Below is a sample of my data:
clear
input expected_arrival_month year
1 2013
2 2014
3 2015
4 2016
5 2017
6 2018
end
I tried the following command:
generate month = .
replace month = 2013M01 if expected_arrival_month == 1 & year == 2013
However, I received the error:
2013M01 invalid name
r(198)
How can I get the desired output?
The issue here is in dealing with string rather than numeric variables. Given that the variable you are generating is a string variable, the contents of the variable must be enclosed in quotation marks:
There would also be other more efficient ways to deal with this generation, for example using Stata's
egen
command (andconcat
), ordatetime
functions as indicated in another response.