Hello,
I'm writing with what I'm sure is a naive question that I've struggled with for years in Stata. I've either not found or had a hard time understanding existing help documentation for this specific question, and if someone is able to simply help me with this code, I would be super grateful. I am using Stata BE 17.
I would like to create 24 new variables, one new variable for each month of a year (so, January 2022 through December 2023). Ideally, these variables would be named date1 - date24, and they would be labeled Jan2022 through Dec2023.
I can create 24 new date variables fairly easily as follows:
However, when it comes to adding labels, I have a hard time understanding whether it's possible to write another loop for the labels. In the past, because I haven't been able to figure it out, I've simply coded them by hand (which, depending on the number of variables, can take a long time):
etc.
However, if I write something like:
then the program overwrites values that have already been defined. Therefore, I'll end up with a list of dates all with the same label (the last label in the list).
Is there a way to assign labels in a 1:1 way, *without* needing to hard-code each label by hand? (I understand this may be obvious to others who are more well-versed in data management in Stata--this problem has just been bugging me for years and I haven't been able to sort it out on my own, so hoping someone can bestow their wisdom on me.)
(edited for clarity and to amend an error)
I'm writing with what I'm sure is a naive question that I've struggled with for years in Stata. I've either not found or had a hard time understanding existing help documentation for this specific question, and if someone is able to simply help me with this code, I would be super grateful. I am using Stata BE 17.
I would like to create 24 new variables, one new variable for each month of a year (so, January 2022 through December 2023). Ideally, these variables would be named date1 - date24, and they would be labeled Jan2022 through Dec2023.
I can create 24 new date variables fairly easily as follows:
Code:
forvalues i = 1(1)24 { gen date`i' = . }
Code:
label var date1 "Jan2022" label var date2 "Feb2022" label var date3 "Mar2022"
However, if I write something like:
Code:
foreach x in jan feb mar ap may jun jul aug sep oct nov dec {forvalues i = 1(1)12 {label var date`i' = "`x'2022"}}
Is there a way to assign labels in a 1:1 way, *without* needing to hard-code each label by hand? (I understand this may be obvious to others who are more well-versed in data management in Stata--this problem has just been bugging me for years and I haven't been able to sort it out on my own, so hoping someone can bestow their wisdom on me.)
(edited for clarity and to amend an error)
Comment