Reputation: 13
I am trying to run the following code to reformat variables n(date1, date2) to dates from strings but am getting an error:
' invalid name r(198);
I am using stata 15.0 (upgrade not possible)
This is the code:
foreach var of varlist date1 date2 {
gen double_d'var' = clock('var', "YMD hms")
format d_'var' %tc
drop 'var'
rename d_'var' 'var'
}
Would be grateful for some help.
Upvotes: 1
Views: 9816
Reputation: 9460
Remove the underscore after double and use `’ quotes (the grave accent and the standard single quote, or apostrophe):
clear
set obs 1
gen date1 = "2020/12/01 00:00:00"
gen date2 = "2021/12/01 00:00:00"
foreach var of varlist date1 date2 {
gen double d_`var' = clock(`var', "YMD hms")
format d_`var' %tc
drop `var'
rename d_`var' `var'
}
Upvotes: 1