BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sreeram
Fluorite | Level 6
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
1 ACCEPTED SOLUTION

Accepted Solutions
sreeram
Fluorite | Level 6

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

3 REPLIES 3
sreeram
Fluorite | Level 6

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

 

SPR
Quartz | Level 8 SPR
Quartz | Level 8
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
Peter_C
Rhodochrosite | Level 12
> 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' )

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 121936 views
  • 4 likes
  • 3 in conversation