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 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1242 views
  • 4 likes
  • 6 in conversation