BookmarkSubscribeRSS Feed
Barkat
Pyrite | Level 9

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

6 REPLIES 6
Patrick
Opal | Level 21

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;

Patrick_0-1642456282410.png

 

 

Barkat
Pyrite | Level 9
I should have mentioned that, I wanted the Month_name as date not as character.
SASKiwi
PROC Star
data want;
  month_name = '18Jan2022'd;
month_name2 = mdy(1,1,2022); format month_name month_name2 monname. ; put month_name = month_name2 =; run;
Reeza
Super User

@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;
Tom
Super User Tom
Super User

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?

Kurt_Bremser
Super User

@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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 1949 views
  • 10 likes
  • 6 in conversation