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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.