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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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