DATA Step, Macro, Functions and more

MONTH function not working properly

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

MONTH function not working properly

Hi, 

I have to create a variable which extract the month from a other existing variable, but the results does not represent reality.

DATA NEW;
 set work.new;
 MOIS=MONTH(DATERELEVE);
 RUN;

SAS.PNG

 

PS: The data is imported from a xlsx files.


Accepted Solutions
Solution
‎10-08-2017 01:30 PM
Super User
Super User
Posts: 7,941

Re: MONTH function not working properly

[ Edited ]

The MONTH() function should work fine, but it won't return a string. It will return the number of the month.

216   data test;
217    date=today();
218    format date date9.;
219    month=month(date);
220    put date= month=;
221   run;

date=08OCT2017 month=10

Perhaps you tried to attach the MONNAME. format to your new month number variable? If so then it should show JANUARY for every value since MONNAME is meant to be used with dates and the date values of 1 to 12 are all in January of 1960.

261   data _null_;
262    do month=1 to 12;
263      put month= 'date9=' month date9. ' monname=' month monname.;
264    end;
265   run;

month=1 date9=02JAN1960 monname=  January
month=2 date9=03JAN1960 monname=  January
month=3 date9=04JAN1960 monname=  January
month=4 date9=05JAN1960 monname=  January
month=5 date9=06JAN1960 monname=  January
month=6 date9=07JAN1960 monname=  January
month=7 date9=08JAN1960 monname=  January
month=8 date9=09JAN1960 monname=  January
month=9 date9=10JAN1960 monname=  January
month=10 date9=11JAN1960 monname=  January
month=11 date9=12JAN1960 monname=  January
month=12 date9=13JAN1960 monname=  January

 

View solution in original post


All Replies
Solution
‎10-08-2017 01:30 PM
Super User
Super User
Posts: 7,941

Re: MONTH function not working properly

[ Edited ]

The MONTH() function should work fine, but it won't return a string. It will return the number of the month.

216   data test;
217    date=today();
218    format date date9.;
219    month=month(date);
220    put date= month=;
221   run;

date=08OCT2017 month=10

Perhaps you tried to attach the MONNAME. format to your new month number variable? If so then it should show JANUARY for every value since MONNAME is meant to be used with dates and the date values of 1 to 12 are all in January of 1960.

261   data _null_;
262    do month=1 to 12;
263      put month= 'date9=' month date9. ' monname=' month monname.;
264    end;
265   run;

month=1 date9=02JAN1960 monname=  January
month=2 date9=03JAN1960 monname=  January
month=3 date9=04JAN1960 monname=  January
month=4 date9=05JAN1960 monname=  January
month=5 date9=06JAN1960 monname=  January
month=6 date9=07JAN1960 monname=  January
month=7 date9=08JAN1960 monname=  January
month=8 date9=09JAN1960 monname=  January
month=9 date9=10JAN1960 monname=  January
month=10 date9=11JAN1960 monname=  January
month=11 date9=12JAN1960 monname=  January
month=12 date9=13JAN1960 monname=  January

 

Valued Guide
Posts: 531

Re: MONTH function not working properly

This is spot on @Tom. I bet my left arm you are correct. It also illustrates the danger to assume that some function is "not working properly". One does SAS more justice by stating that something is "not behaving as expected" which leaves some room for user error. Month() is ancient. It was in V5.08 when I started. It has gone through some proper testing by now.

 

If only I got a eurocent for every time a user called me to tell me that they found a bug....

New Contributor
Posts: 2

Re: MONTH function not working properly

Ok thanks a lot!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 253 views
  • 2 likes
  • 3 in conversation