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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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