i have a dataset with a character variable 20080824. How can i convert this to date9. format?
You can't convert directly a created variable from alpha to numeric other than when you input it so you will need to create a new variable and, once satisfied with the result, drop the old one.
format newdatevar date9.;
newdatevar = input(trim(curdatevar), YYMMDD8.);
once convinced that the result is good, you add
data mydata(rename=(newdatevar=curdatevar);
set mydata;
format newdatevar date9.;
newdatevar = input(trim(curdatevar), YYMMDD8.);
drop curdatevar;
run;
So that the newly created variable can bear the same variable name as the old character variable.
Can be done in a datastep, a few procedures or with proce SQL, the syntax is slightly different but the concept is the same, use input to read a char string as a given format, here YYMMDD8. and make sure that you specify the format of your variable as otherwise you will have the integer representation of the number of day since 01/01/1960 of that date (as it is stored) instead of your date9.
Vincent
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.