DATA Step, Macro, Functions and more

sas date

Not applicable
Posts: 1

sas date

i have a dataset with a character variable 20080824. How can i convert this to date9. format?

Super Contributor
Posts: 339

Re: sas date

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;


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.


Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation