Hi, I created some date variable using data step. But in the result dataset, those variables are shown as numeric rather than character. How can I switch them to character (and keep the format as Date9.)?
%let year = 2019;
data holiday;
nyd = mdy(1, 1, &year.); /*New Year's Day*/
format nyd date9.;
familyday = nwkdom(3, 2, 2, &year.); /*Family Day; 3rd Monday in February*/
format familyday date9.;
run;
Please describe why you would actually want those variables to be character? The format should work anywhere people need to see it.
None of the functions for working with dates will work with character values.
You cannot change a variable type once it is created.
You could create a character variable as
data holiday; nyd = mdy(1, 1, &year.); /*New Year's Day*/ format nyd date9.; familyday = nwkdom(3, 2, 2, &year.); /*Family Day; 3rd Monday in February*/ format familyday date9.; nydchar = put(nyd,date9.); familydaychar = put(familyday,date9.); run;
Once you have the character value then you would not be able to apply a DATE9. format as that only works for numeric values.
Please describe why you would actually want those variables to be character? The format should work anywhere people need to see it.
None of the functions for working with dates will work with character values.
You cannot change a variable type once it is created.
You could create a character variable as
data holiday; nyd = mdy(1, 1, &year.); /*New Year's Day*/ format nyd date9.; familyday = nwkdom(3, 2, 2, &year.); /*Family Day; 3rd Monday in February*/ format familyday date9.; nydchar = put(nyd,date9.); familydaychar = put(familyday,date9.); run;
Once you have the character value then you would not be able to apply a DATE9. format as that only works for numeric values.
Thank you!!
Why do you want a character variable?
And if you do why in that style? They will not sort right. The first of december will be before the third of august.
Use a format like YYMMDD10. instead. Then the values will sort properly.
nyd = put(mdy(1, 1, &year.),yymmdd10.);
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.