- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data want;
month_name = '18Jan2022'd; month_name2 = mdy(1,1,2022);
format month_name month_name2 monname. ;
put month_name = month_name2 =;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.