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
You can use the YYMMd format:
str=put(date,yymmd7.);
The "d" in the format name modifies the output to use a dash separator.
You can use the YYMMd format:
str=put(date,yymmd7.);
The "d" in the format name modifies the output to use a dash separator.
like this?
data test;
dt='07JAN2020'd;
dtnew=put(dt, yymmd.);
run;
Hello @David_Billa,
Use the YYMMD. format:
data want(drop=_cm);
set have(rename=(Calendar_Month=_cm));
Calendar_Month=put(_cm,yymmd.);
run;
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 wrote:
Can't we do it without rename statement?
In fact, @FreelanceReinh does not use a statement, but a dataset option. These are two different things.
I merged your two, basically identical, questions.
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.