Dear all,
I would like to reformat the dates in my data set to a Stata recognized format. Some of these date variables allow for partial dates, which will need to stay in a string format. So I only want to reform variables that take a full date. Fortunately, I know from the metadata from my database which variables take a full date. I have saved these variables names in a global in the meantime. So far so good.
Now I want to loop through the variable names (not the observations) in my dataset and if that variable name occurs in my `varname_dates' global, then I want to reformat it to a Stata recognized date. I found another post that relates to my question
but I can't quite seem to get this code to work in combination with generating a new variable. See below the code I have written and the Stata feedback.
invalid syntax
gen not found
Would anyone be kind enough to explain what I am overseeing here? Or perhaps offer an alternative solution? I'm not an experienced guest on this forum yet, so if there is any way I can make my post more understandable, please do let me know.
Thank you and best wishes,
Moniek
I would like to reformat the dates in my data set to a Stata recognized format. Some of these date variables allow for partial dates, which will need to stay in a string format. So I only want to reform variables that take a full date. Fortunately, I know from the metadata from my database which variables take a full date. I have saved these variables names in a global in the meantime. So far so good.
Code:
levelsof varname_dates, local(varname_dates) clean global varname_dates `varname_dates'
HTML Code:
https://www.statalist.org/forums/forum/general-stata-discussion/general/1466177-if-variable-is-in-varlist
Code:
foreach k of varlist _all { if `:list `k' in `varname_dates'' gen `k'_new = date(`k', "DMY") order `k'_new, after(`k') format `k'_new %td drop `k' rename `k'_new `k' }
invalid syntax
gen not found
Would anyone be kind enough to explain what I am overseeing here? Or perhaps offer an alternative solution? I'm not an experienced guest on this forum yet, so if there is any way I can make my post more understandable, please do let me know.
Thank you and best wishes,
Moniek
Comment