Hi,
I am trying to figure out how to format a character variable of a date that appears in the character format "8-Jun-2020" to a numeric mmddyy10 variable: "06/08/2020". I tried to use "substr" and "put", but the issue is that some dates appear as "22-Jun-2020", so the day part of the variable takes up 2 spaces instead of one (22 vs. 8).
Things I've tried:
data import; set import; birth_date_new = catx("-", put(birth_date, mmddyy10.)); run;
AND
data import; set import; birth_date_new= catx("-", substr(birth_date, 3,3),substr(birth_date, 1,1),substr(birth_date, 7,4)) OR birth_date_new = catx("-", substr(birth_date,3,3), substr(birth_date,1,2), substr(birth_date,8,4)); format birth_date mmddyyyy10.; run;
Let me know if you can help, thanks!
Clare
Don't do this:
data import;
set import;
If something untoward happens in the data step, the dataset is destroyed and you need to re-create it. Sometimes this might mean you have to look for a backup.
Do this to convert your date value:
birth_date_new = input(birth_date,date11.);
format birth_date_new mmddyy10.;
If your date variable is indeed character to start with this should work:
data test;
date = '01-12-2020';
date_new = input(date, ddmmyy10.);
format date_new mmddyyd10.;
put _all_;
run;
Don't do this:
data import;
set import;
If something untoward happens in the data step, the dataset is destroyed and you need to re-create it. Sometimes this might mean you have to look for a backup.
Do this to convert your date value:
birth_date_new = input(birth_date,date11.);
format birth_date_new mmddyy10.;
That worked! Thank you so much!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.