BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
smorkoc
Fluorite | Level 6

Hi everyone,
I'm pretty new in using SAS. I am trying to write a code to show my month variable which is shown like JAN2018 FEB2018 MAR2018

APR2018  MAY2018 JUN2018 AUG2018 SEP2017 OCT2017 NOV2017 DEC2017.. 

What I am trying to do is I want show JAN2018= '1'   FEB2018='2' MAR2018='3'  .......  and so on.. 

my data name is csh

my variable is month in date not string or num..

 

Could you help me how can I convert the month variable to numbers from 1 to 12?

 

thanks

 

Suna Morkoc

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You've gotten the right advice.  You need to put it together in the proper places.

 

You already have a data set CSH.  It already contains valid SAS dates in a variable named MONTH.  You don't need to input anything.  You just need to read the existing SAS data set:

 

data want;

set csh;

monthnum = month(month);

run;

View solution in original post

9 REPLIES 9
Tom
Super User Tom
Super User

If MONTH is an actual date value, but it has the MONYY7. format attached then just use the MONTH() function to find the month number in the year.

monthnum = month(MONTH);

If MONTH is character then why not just convert it to a date first and then apply the function?

monthnum=month(input('01'||month,date9.));

 

smorkoc
Fluorite | Level 6

Thanks for your help. Should I use format statement then? 

smorkoc
Fluorite | Level 6

here is the code I used but it did not work. 

 

data csh;
input month;
monthnum = month(MONTH);
cards;
Jan2018
Feb2018
Mar2018
Apr2018
May2018
Jun2018
Jul2018
Aug2018
Sep2017
Oct2017
Nov2017
Dec2017
;
run;

 

The output is like 

     Month   Monthnum

1     .                .

2     .                .

3     .                .

4     .                .

5     .                .

6     .                .

7     .                .

8     .                .

9     .                .

10   .                .

11   .                .

12  .                 .

Reeza
Super User

You aren’t reading the date as a SAS date, I’m guessing you have errors in the code shown as well. 

 

Informat date anydtdte.; 

 

Add the line above BEFORE your input statement. 

smorkoc
Fluorite | Level 6

And when I just tried this code ;

data csh1;
set csh;
format Month monthnum;
run;

 

I think this is right one but the output shows 

 

21063
21063
21063
21093
21093
21093
21124
21124
21124
21154
21154
21154
21185
21185
21185
21216
21216
21216
21244
21244
21244
21275
21275
21275
21305
21305
21305

.

.

.

.

.

so on. 

I guess it is because SAS takes the first date as 1960 or sth. 

Astounding
PROC Star

You've gotten the right advice.  You need to put it together in the proper places.

 

You already have a data set CSH.  It already contains valid SAS dates in a variable named MONTH.  You don't need to input anything.  You just need to read the existing SAS data set:

 

data want;

set csh;

monthnum = month(month);

run;

smorkoc
Fluorite | Level 6

Thank you so much! It worked.

 

Reeza
Super User

Do you want a new variable?

In SAS you can either create a new variable or use it as shown and grouped as needed in summaries. 

 


@smorkoc wrote:

Hi everyone,
I'm pretty new in using SAS. I am trying to write a code to show my month variable which is shown like JAN2018 FEB2018 MAR2018

APR2018  MAY2018 JUN2018 AUG2018 SEP2017 OCT2017 NOV2017 DEC2017.. 

What I am trying to do is I want show JAN2018= '1'   FEB2018='2' MAR2018='3'  .......  and so on.. 

my data name is csh

my variable is month in date not string or num..

 

Could you help me how can I convert the month variable to numbers from 1 to 12?

 

thanks

 

Suna Morkoc


 

smorkoc
Fluorite | Level 6

I thought it would be much better to create a new variable because once I'm done with converting I need to group them as season like '12' '2' '3' = winter , '4' '5' '6' =spring etc..


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 19680 views
  • 6 likes
  • 4 in conversation