BookmarkSubscribeRSS Feed
rogeralfa111
Fluorite | Level 6

Can any one explain how can we get last date of each month for several years without using intnx function.

because intnx was introduced in 9.2 and above.

i want to know how previous coders code in this particular situation.

 

6 REPLIES 6
Kurt_Bremser
Super User

Soemthing like

select (month);
  when (1,3,5,7,8,10,12) day = 31;
  when(4,6,9,11) day = 30;
  when (2) do;
    if mod(year,4) ne 0 and (mod(year,400) = 0 or mod(year,100) ne 0)
    then day = 29;
    else day = 28;
  end;
end;

Untested, posted from my tablet.

Patrick
Opal | Level 21

@rogeralfa111 wrote:

Can any one explain how can we get last date of each month for several years without using intnx function.

because intnx was introduced in 9.2 and above.


I assume this is an academic question. 

 

The intnx() function existed with certainty already in versions prior to 9.2. No more sure if it was already V6 but certainly since V8.<n>

Here a link to V9.1.3 docu that includes intnx(): SAS 9.1.3 Language Reference: Dictionary, Fifth Edition 

...or here a link to SAS V8 docu I found: https://www.sfu.ca/sasdoc/sashtml/lgref/z0212700.htm 

 

Below a lazy approach that doesn't need separate logic for the different number of days per month or for dealing with leap years.

data work.last_day_of_month;
  format date date9.;
  do date='01jan1900'd to today();
    if day(date+1)=1 then
      do;
        output;
        date+27;
      end;
  end;
run;

 

...and of course with a given date you can just use the following test: day(date+1)=1

ballardw
Super User

There are a number of books on common numeric calculations and dates is one of them.

However be a bit careful with FEBRUARY and years as some references didn't consider the year 2000, which was an exception to the common 100/400 year determination of Leap Years (hence a problem called Y2K). Be very careful of any code relying on 2-digit years because that is a symptom of problems to come.

Reeza
Super User

Historically, I've also created a date dimension and queried the table.

https://gist.github.com/statgeek/9606118

 

EDIT: the holiday portion would need to be updated (clearly very old code)

 

 

Ksharp
Super User
data have;
do year=2020 to 2023;
 do month=1 to 12;
   date=mdy(month,1,year);
   last_day=mdy(ifn(month=12,1,month+1),1,ifn(month=12,year+1,year))-date;
   last_date=mdy(month,last_day,year);
   output;
 end;
end;
format last_date date yymmdd10.;
run;
mkeintz
PROC Star

For each year/month, get the date value for the first day of next month.  Then subtract 1:

 

data have;
  do year=2020 to 2023;
    do month=1 to 12;
      nxt_date1=mdy(mod(month,12)+1,1,year+(month=12));  *Next month first date *;
      last_date=nxt_date1-1;
      last_day=day(last_date);
      output;
    end;
  end;
  format nxt_date1 date9.  last_date date9.;
run;

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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