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
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;
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.));
Thanks for your help. Should I use format statement then?
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 . .
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.
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.
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;
Thank you so much! It worked.
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 MAR2018APR2018 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 cshmy 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
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.