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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Tom
Super User Tom
Super User

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

 

jklaverstijn
Rhodochrosite | Level 12

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....

KTR44
Calcite | Level 5

Ok thanks a lot!

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 3612 views
  • 2 likes
  • 3 in conversation