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.);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.