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

I have a SAS data set with a numeric variable (not date) with values like 20140815.  How can I turn this into a SAS date number that I willl eventually want in the format of MMDDYY8.  ?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

More specifically, the conversion would look like this:

 

newvar = input(put(oldvar,8.), yymmdd8.);

 

Then apply any date format you would like.  Any of these examples (and more) would be possible:

 

format newvar date9.;

format newvar yymmdd10.;

format newvar mmddyy8.;

format newvar mmddyys10.;

 

Experiment, till you find a format that you like.

 

Good luck.

View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20

Use a combination of put() and input() functions.

Data never sleeps
Astounding
PROC Star

More specifically, the conversion would look like this:

 

newvar = input(put(oldvar,8.), yymmdd8.);

 

Then apply any date format you would like.  Any of these examples (and more) would be possible:

 

format newvar date9.;

format newvar yymmdd10.;

format newvar mmddyy8.;

format newvar mmddyys10.;

 

Experiment, till you find a format that you like.

 

Good luck.

CharlotteCain
Quartz | Level 8

Hi Bob, May i request you to please enlighten me on the differences between applying $w. format and just like 8. format without the dollar sign. Also,  does applying an x amount of width caters to values less than the number of bytes in the value?

This confuses me big time:(

 

Sorry for the bother and merry christmas and happy new year to you,

Charlotte

Astounding
PROC Star

Charlotte,

 

The $w formats expect to find character strings as the incoming values.  Leave out the dollar sign and the software expects a numeric as the incoming value.

 

If you specify a width that is wider than the numeric value:

 

newvar = put(12345678, 10.);

 

You get a leading blank (or as many leading blanks as needed) to fill out the resulting string.  Note that the PUT function always returns a character string regardless of whether the format is numeric or character.

 

When in doubt, experiment.  You can always check your results with something like this:

 

newvar2 = '*' || newvar || '*';

put newvar2=;

 

People who are trying to learn are never a bother.  Merry Christmas to you as well.

 

 

Sharon421
Calcite | Level 5

This seems simple on the surface but I could not figure it out.  This was a GREAT help.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1054 views
  • 3 likes
  • 4 in conversation