In my dataset, in which the data of a variable, Num_Month has been entered as 1, 2, 3,- - - 12. I would like to convert them as Jan, Feb, Mar. - - - Dec.
I tried
data want; set have;
format Month Monname3.;
run;
This is not working. Please help
SAS Date formats require SAS Date values. SAS Date values are the count of days since 1/1/1960 stored in a numerical variable. SAS Date formats then make such counts human readable as dates (without changing the value).
The mdy() function allows you to create such a SAS date value. The put() function used with a date format will then allow us to write this SAS Date value formatted as a string to a new variable.
data demo;
num_month=5;
month_name=put(mdy(num_month,1,1960),monname3.);
run;
data want;
month_name = '18Jan2022'd;
month_name2 = mdy(1,1,2022);
format month_name month_name2 monname. ;
put month_name = month_name2 =;
run;
@Barkat wrote:
I should have mentioned that, I wanted the Month_name as date not as character.
You may need to clarify your expectations then. This is where providing clear sample input data and expected output helps YOU.
data demo;
num_month=5;
month_name=mdy(num_month,1,1960);
format month_name monname.;
run;
If you want to convert a number between 1 and 12 into a DATE you need a YEAR and DAY value. You might be able to default to the first day of the month but where are you going to get the year?
@Barkat wrote:
I should have mentioned that, I wanted the Month_name as date not as character.
A date identifies a specific day within a specific month within a specific year, so you need to define day and year first before you go on. For your purposes, it will be OK to assume day as 1, but year(s) is/are needed.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.