BookmarkSubscribeRSS Feed
kushsas
Calcite | Level 5

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

1 REPLY 1
Vince28_Statcan
Quartz | Level 8

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

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1358 views
  • 0 likes
  • 2 in conversation