DATA Step, Macro, Functions and more

Date Conversion

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Date Conversion

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


Accepted Solutions
Solution
‎12-28-2015 02:14 PM
Super User
Posts: 5,091

Re: Date Conversion

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


All Replies
Super User
Posts: 5,260

Re: Date Conversion

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

Data never sleeps
Solution
‎12-28-2015 02:14 PM
Super User
Posts: 5,091

Re: Date Conversion

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.

Frequent Contributor
Posts: 137

Re: Date Conversion

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 timeSmiley Sad

 

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

Charlotte

Super User
Posts: 5,091

Re: Date Conversion

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.

 

 

New Contributor
Posts: 2

Re: Date Conversion

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 318 views
  • 3 likes
  • 4 in conversation