- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for your help. Should I use format statement then?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 . .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much! It worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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..