BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hongxianzhang
Calcite | Level 5

Does anyone know how to convert number format into date format?  For example, the following shows what my data looks like:

20060306
20040727
20040407
20010423

All of data is shown as the number format.  I want to convert them into date format.  For example, I want to change 20060306 into 03/06/2006.  I used

the code: date aaa; set aaa; format varialbename mmyydd8.;run;

This code does not work out.  Could anyone tell me how to do it?  Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
hongxianzhang
Calcite | Level 5

Editor's Note:  hongxianzhang's code below shows how to use the INPUT function along with the YYMMDDw.d informat, which was suggested by LinusH.  The INPUT function along with the YYMMDDw.d informat allow you to convert a numeric string in the form yymmdd into a SAS date.

 

 

DATA SET2;
  SET SET1;
  DATE2 = INPUT(PUT(DATE1,8.),YYMMDD8.);
  FORMAT DATE2 YYMMDD8.;
RUN;

 

 

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20

Use yymmdd8. informat

Data never sleeps
hongxianzhang
Calcite | Level 5

Editor's Note:  hongxianzhang's code below shows how to use the INPUT function along with the YYMMDDw.d informat, which was suggested by LinusH.  The INPUT function along with the YYMMDDw.d informat allow you to convert a numeric string in the form yymmdd into a SAS date.

 

 

DATA SET2;
  SET SET1;
  DATE2 = INPUT(PUT(DATE1,8.),YYMMDD8.);
  FORMAT DATE2 YYMMDD8.;
RUN;

 

 

RichardinOz
Quartz | Level 8

The INPUT function uses the INFORMAT, in this case YYMMDD, which has the same name as the format in the FORMAT statement in the following line.  Informats are used to read text data into (usually) numeric variables.  You used the informat correctly. 

Richard

Tom
Super User Tom
Super User

That is exactly how to use INFORMAT on an existing variable.

Looks like the variable is already numeric so you could store the date value into the original variable. 

If you want it to display the same then use YYMMDDN8, otherwise it will display two digit years and hyphens.  Or you could use YYMMDD10. to leave room for the hyphens.

DATA SET2;

  SET SET1;

  DATE1 = INPUT(PUT(DATE1,8.),YYMMDD8.);

  FORMAT DATE1 YYMMDD10.;

RUN;

MichelleHomes
Meteorite | Level 14

I recently wrote a blog post relating to the put and input function that you may find useful - Rhymes, mnemonics and tips in learning SAS - The SAS Training Post

As your original variable is numeric you use the put function to convert it to a character type and then use the input function using the YYMMDD8. (as that is the way your data looks) to create the numeric SAS date.

The format statement simply displays the SAS date number in the format you want, YYMMDD10.

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com

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
  • 5 replies
  • 170289 views
  • 8 likes
  • 5 in conversation