BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
newboy1218
Quartz | Level 8

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;

 

Capture.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

3 REPLIES 3
ballardw
Super User

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.

Tom
Super User Tom
Super User

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.);

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1095 views
  • 1 like
  • 3 in conversation