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. ?
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.
Use a combination of put() and input() functions.
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.
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
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.
This seems simple on the surface but I could not figure it out. This was a GREAT help.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: