0

I´m trying to create a variable like this in Stata:

date
2012_1
2012_2
2013_1
2013_2

with the next loop:

forval y=2012/2013{
forval m=1/2{
display `m'         
    gen date =  `y'_`m'
}
}   

But I´m getting this error in the first iteration: 2012_1 invalid name. Sorry if the question is obvious, I´m newbie in Stata.

2 Answers 2

2

You face more problems than you realise here, but all are simple.

  1. The immediate problem with your loop is that a value such as 2012_1 is intended by you as a value of a variable, but if so it must explicitly be a string, surrounded by "". The reason is that underscore _ is only acceptable as part of a string. Stata is clearly puzzled by your command. The error message does not quite fit the situation, although it is correct that 2012_1 is not an acceptable name, meaning the name of a variable or scalar.

  2. If you fixed that, your next problem would be that second time around your loop the variable already exists and so generate is unacceptable. You would need to replace. So, the generate statement should be taken outside the loop.

  3. Then again, all your loop does even with those problems fixed is to overwrite the variable each time with the same value. At your end of your loops all observations would contain the constant value 2013_2.

  4. Longer term, there is still a problem. Evidently you want a monthly date variable, but monthly date variables like that are of little use in Stata. They sort in the correct order, but they are essentially useless for statistics or graphics.

This is a better idea all round:

generate mdate = . 
local i = 1 

forval y = 2012/2013 {
    forval m = 1/2 {
        replace mdate = ym(`y', `m') in `i' 
        local ++i       
    }
} 

That is still not good style. I guess that you don't really want only months 1 and 2, but we can't know what you really want.

Do this in Stata:

clear 
set obs 48 
generate mdate = ym(2011, 12) + _n 
format mdate %tm 
list 

to get an idea of a better approach -- with no loops at all.

Sign up to request clarification or add additional context in comments.

Comments

2

There are quite some problems with your code. I'll go through them one by one.

`y'_`m' evaluates to 2012_1 the first iteration. Since it contains an underscore it cannot be interpreted as numeric. To be interpreted as a string value would require it to be enclosed in "". In the end, Stata tries to interpret it as a variable but 2012_1 is not a valid name (has to start with a letter), hence your error.

You could enclose your value in quotes to create a string variable: "`y'_`m'". This will work for the first iteration, but the second iteration you will get an error, since variable 'date' already exists. After creating a variable, you can only replace it.

Finally, your code says nothing about which value goes to which observation. Even if you would fix the problems already mentioned, your variable will just contain the same values for all observations which is the value of the last iteration in the loop. To replace only one observation you have to specify in i where i is the observation number.

All in all, this would be the amended code:

gen date = "."
local obs = 1
forval y=2012/2013{
    forval m=1/2{
        display `m'         
        replace date =  "`y'_`m'" in `obs'
        local ++obs
    }
}   

However, I would not recommend creating this type of date variable, as string variables are limited in what you can do with it. Stata's internal date format is the most convenient. If your values 1 and 2 represent half years you could create a half-yearly date variable, see help datetime for information on how to do this. Another option is to create a numeric variable containing the year, and a second numeric variable containing 1 and 2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.