SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

I've one field called 'Calendar_Month' and it is in date9. Format and I want to convert that field to character and the value should appear like YYYY-MM.

 

Ex:

 

AS IS:

 

Calendar_Month (numeric with date9. format)

07Jan2020

 

TO BE:

Calendar_Month (Need in character)

2020-01

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

You can use the YYMMd format:

str=put(date,yymmd7.);

The "d" in the format name modifies the output to use a dash separator.

View solution in original post

9 REPLIES 9
David_Billa
Rhodochrosite | Level 12
Could you please tell me how to convert the field which has SAS date value (07JAN2020) to character field YYYY-MM (2020-01) in one step?
s_lassen
Meteorite | Level 14

You can use the YYMMd format:

str=put(date,yymmd7.);

The "d" in the format name modifies the output to use a dash separator.

PeterClemmensen
Tourmaline | Level 20

like this?

 

data test;
    dt='07JAN2020'd;
    dtnew=put(dt, yymmd.);
run;
FreelanceReinh
Jade | Level 19

Hello @David_Billa,

 

Use the YYMMD. format:

data want(drop=_cm);
set have(rename=(Calendar_Month=_cm));
Calendar_Month=put(_cm,yymmd.);
run;
David_Billa
Rhodochrosite | Level 12
Can't we do it without rename statement?
FreelanceReinh
Jade | Level 19

You want the new character variable to have the same name as the original numeric variable. The type of an existing variable cannot be changed in place, hence we need two separate variables and different variables must have different names. So, at some point we need to change the old name to a new name (that's what I did with the RENAME= dataset option) or vice versa.

David_Billa
Rhodochrosite | Level 12
I meant to say rename option only.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 9 replies
  • 52432 views
  • 5 likes
  • 5 in conversation