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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

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

5 REPLIES 5
Tourmaline | Level 20

## Re: Date Conversion

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

Data never sleeps
PROC Star

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

Quartz | Level 8

## 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 time:(

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

Charlotte

PROC Star

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

Calcite | Level 5

## Re: Date Conversion

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

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