Help using Base SAS procedures

How to find the last date of a month.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How to find the last date of a month.

i have a CHARACTER VARIABLE in which dates are partially written, ie. '2007-09', '2010-10' etc. Now i have to find the last date of the month ie. in 2007-09, last date in the month 09 added to the new variable representing like 2007-09-30 etc. Can some one help me in doing this. Message was edited by: sreeram

Accepted Solutions
Solution
‎11-09-2016 04:04 PM
Occasional Contributor
Posts: 6

Re: How to find the last date of a month.

[ Edited ]

Editor's Note:  Thanks to sreeram for showing the optional fourth argument to the INTNX function that allows you to specify the alignment when incrementing the date.  To find the last day of the month, the alignment should be set to 'END' or 'E'.  This will increment the starting date so that it falls on the last day of the month. 

Instead of concatenating the '01' to the starting value, the ANYDTDTE informat can be used to create a SAS date from just the year and month.  The resulting SAS date will default to the first day of the month.  This slight modification has been made to sreeram's original code.


 

data a;     
aeendtc1='2007-09';
aeendtc2=input(aeendtc1,anydtdte7.);
lastDay=intnx ('month',aeendtc2,0,'E');
mydate=put(lastday,yymmdd10.);
run;
proc print;
  format aeendtc2 lastday yymmddd10.;
run;


aeendtc1=strip(aeendtc)||"-01";
aeendtc2=input(aeendtc1,yymmdd10.);
lastDay=intnx ('month',aeendtc2,0,'E');
mydate=put(lastday,yymmdd10.);

 

View solution in original post


All Replies
Solution
‎11-09-2016 04:04 PM
Occasional Contributor
Posts: 6

Re: How to find the last date of a month.

[ Edited ]

Editor's Note:  Thanks to sreeram for showing the optional fourth argument to the INTNX function that allows you to specify the alignment when incrementing the date.  To find the last day of the month, the alignment should be set to 'END' or 'E'.  This will increment the starting date so that it falls on the last day of the month. 

Instead of concatenating the '01' to the starting value, the ANYDTDTE informat can be used to create a SAS date from just the year and month.  The resulting SAS date will default to the first day of the month.  This slight modification has been made to sreeram's original code.


 

data a;     
aeendtc1='2007-09';
aeendtc2=input(aeendtc1,anydtdte7.);
lastDay=intnx ('month',aeendtc2,0,'E');
mydate=put(lastday,yymmdd10.);
run;
proc print;
  format aeendtc2 lastday yymmddd10.;
run;


aeendtc1=strip(aeendtc)||"-01";
aeendtc2=input(aeendtc1,yymmdd10.);
lastDay=intnx ('month',aeendtc2,0,'E');
mydate=put(lastday,yymmdd10.);

 

Super Contributor
Super Contributor
Posts: 365

Re: How to find the last date of a month.

Hello Sreeram,
[pre]
This is a solution:
data i;
input dt0 $;
datalines;
2007-09
2007-10
run;
data r;
set i;
dt=INPUT(CATS(dt0,"-01"),ANYDTDTE10.);
dr=INTNX("MONTH",dt,1,"BEGINNING")-1;
format dt date7. dr YYMMDD10.;
run;
[/pre]
Sincerely,
SPR
Valued Guide
Posts: 2,177

Re: How to find the last date of a month.

> i have a CHARACTER VARIABLE in which dates are
> partially written, ie. '2007-09', '2010-10' etc. Now
> i have to find the last date of the month ie. in
> 2007-09, last date in the month 09 added to the new
> variable representing like 2007-09-30 etc. Can some
> one help me in doing this.
>

intnx( 'month', input( cats(your_char_var, '-01'), yymmdd10.), 0, 'ending' )
🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 17510 views
  • 0 likes
  • 3 in conversation