I have a handful of datasets, all with a variable called "date_of_survey", which is a string, and I need to convert them all to date variables. However, some of them are formatted as 2022-03-24 and some are 24mar2022. Rather than pulling each dataset and using a date() command with the proper arguments, I am trying to learn how to use the capture command and the _rc scalar (which is equal to 109 following a type mismatch), to expedite this process. I am trying to execute something like this:
I continue to get type mismatch errors when I run the loop. Also, a few of my attempts have resulted in a "date variable already defined" message, which might be because I generate the date with the third line of code and then generate it again in the else if statement? Not sure. I can always just drop the date in the else if statement or leave the statement out altogether, but ultimately, that isn't my main problem.
Any help would be greatly appreciated! I'm relatively comfortable working with dates, but the capture and c(rc) business is a new (but quite helpful) trick for me.
HTML Code:
foreach sur in survey1 survey2 survey3 { use `sur', clear capture gen date = date(date_of_survey, "YMD") if c(rc) == 109 { gen date = date(date_of_survey, "DMY") } else if c(rc) == 0 { gen date = date(date_of_survey, "YMD") } }
Any help would be greatly appreciated! I'm relatively comfortable working with dates, but the capture and c(rc) business is a new (but quite helpful) trick for me.
Comment