BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
data noofdays;
do i=1 to 12;
st='01jan2010'd ;
ed='01dec2010'd;
eom=intnx('month',st,ed,'e');
numdays=day(eom);
output;
end;
format st ed eom:date9.;
run;

count each month number of days in do loop

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Just use the DAY() function to find the day of month of the last day of the month.

data noofdays;
  do year=2010 to 2010;
    do month=1 to 12 ;
      eom=intnx('month',mdy(month,1,year),0,'end');
      numdays=day(eom);
      output;
    end;
  end;
  format eom date9.;
run;
Obs    year    month          eom    numdays

  1    2010       1     31JAN2010       31
  2    2010       2     28FEB2010       28
  3    2010       3     31MAR2010       31
  4    2010       4     30APR2010       30
  5    2010       5     31MAY2010       31
  6    2010       6     30JUN2010       30
  7    2010       7     31JUL2010       31
  8    2010       8     31AUG2010       31
  9    2010       9     30SEP2010       30
 10    2010      10     31OCT2010       31
 11    2010      11     30NOV2010       30
 12    2010      12     31DEC2010       31

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

What is your question?

--
Paige Miller
maguiremq
SAS Super FREQ

As mentioned in the previous posts, if you're using a loop, you need to include that in your INTNX() call.

 

Since iteration value is represented by `i`, you need to include that in the third argument.

 

data noofdays;
do i=1 to 12;
st='01jan2010'd ;
ed='01dec2010'd;
/* Putting the `i` in the third argument */
eom=intnx('month',st,i,'e');
numdays=day(eom);
output;
end;
format st ed eom:date9.;
run;

This will increment the loop each time by the iteration value specified for a specified interval. 

 

Looking at your original output, you will see that you get dates that are far away in the future. This is because your third argument is a SAS date which is internally represented as a number.

BrahmanandaRao
Lapis Lazuli | Level 10

Hi Mag

Thank you very much for solution

PGStats
Opal | Level 21

MDY : create a SAS date from year, month and day; INTNX : count the number of date intervals between SAS dates; INTNX : find a SAS date a given number of intervals from a given date. Combine those to your purpose :

 

data nbOfDays;
year = 2010;
do month = 1 to 12;
    numdays = 1 + intck("DAY", mdy(month, 1, year), intnx("MONTH", mdy(month, 1, year), 0, "END"));
    output;
    end;
run;

proc print data=nbOfDays; run;

PGStats_0-1683483413324.png

 

PG
BrahmanandaRao
Lapis Lazuli | Level 10

Hi PGstats,

Thank you very much for solution

Tom
Super User Tom
Super User

Just use the DAY() function to find the day of month of the last day of the month.

data noofdays;
  do year=2010 to 2010;
    do month=1 to 12 ;
      eom=intnx('month',mdy(month,1,year),0,'end');
      numdays=day(eom);
      output;
    end;
  end;
  format eom date9.;
run;
Obs    year    month          eom    numdays

  1    2010       1     31JAN2010       31
  2    2010       2     28FEB2010       28
  3    2010       3     31MAR2010       31
  4    2010       4     30APR2010       30
  5    2010       5     31MAY2010       31
  6    2010       6     30JUN2010       30
  7    2010       7     31JUL2010       31
  8    2010       8     31AUG2010       31
  9    2010       9     30SEP2010       30
 10    2010      10     31OCT2010       31
 11    2010      11     30NOV2010       30
 12    2010      12     31DEC2010       31

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 658 views
  • 4 likes
  • 6 in conversation