Hi,
I have a text date in the format '10Jan2011', I need to find the last day of the month, for the given date. How do I convert the text date into sas date and then find the last day of the month.
Thank You for all your help
Shri
If your inputs are macro variables, and you only need another macro variable showing the end of the month, why not use something like:
%let SEL_MONTH=February;
%let SEL_YEAR=01Jan2011;
%let endmonth = 01%sysfunc(substr(&sel_month,1,3))%sysfunc(substr(&sel_year,6,4));
%let endmonth = %sysfunc(intnx(month,"&endmonth"d,0,E),date9.);
%put &endmonth;
I think you are looking for:
data have;
informat date $9.;
input date;
cards;
10Jan2011
;
data want;
set have (rename=(date=textdate));
format date last_day_of_month date9.;
date=input(textdate,date9.);
last_day_of_month=intnx('month',date,0,"E");
run;
If you have it as a macro variable then you can use it as date literal.
%let textdate=10JAN2011;
%let endmonth = %sysfunc(intnx(month,"&textdate"d,0,E),date9.);
%put &textdate &endmonth;
10JAN2011 31JAN2011
Tom/ Art
Both your responses are very helpful, but I am getting 2 parameters from the prompt, a month (text value from selected from a drop down menu) and year (a date value formatted as a year) Now I have to concatenate these to values to get the month and year and then find the last day of the month.
For eg.
user selects January for the month and 2011 for the year
then I need to get 1/31/2011
Here is the code I have
%let SEL_MONTH=February;
%let SEL_YEAR=01Jan2011;
data _null_;
length fmonth $15. dmonth $15.;
call symput('dyear',put("&sel_Year"d,year4.));
dmonth="'"||"01"||substr("&sel_Month",1,3)||"&dyear"||"'";
call symput('dmonth',(dmonth));
ddate=input('&dmonth', date9.);
format ddate date9.;
call symput('ddate',ddate);
lastDay=intnx('Month' ,"&dmonth"d,0,E);
call symput('lastday',"'"||put(lastday,mmddyyS10.)||"'");
run;
%put &sel_Month;
%put &dYear;
%put &lastday;
%put &ddate;
%put &dmonth;
I am not sure what I am doing wrong, but I am not getting the last day of the month.
Shri
If your inputs are macro variables, and you only need another macro variable showing the end of the month, why not use something like:
%let SEL_MONTH=February;
%let SEL_YEAR=01Jan2011;
%let endmonth = 01%sysfunc(substr(&sel_month,1,3))%sysfunc(substr(&sel_year,6,4));
%let endmonth = %sysfunc(intnx(month,"&endmonth"d,0,E),date9.);
%put &endmonth;
Thanks Art, for the Macro suggestion, it is very helpful and has helped in what I have been trying to do.
Shri
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!
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.
Ready to level-up your skills? Choose your own adventure.