BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sks
Fluorite | Level 6 sks
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

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;

Tom
Super User Tom
Super User

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

sks
Fluorite | Level 6 sks
Fluorite | Level 6

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

art297
Opal | Level 21

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;

sks
Fluorite | Level 6 sks
Fluorite | Level 6

Thanks Art, for the Macro suggestion, it is very helpful and has helped in what I have been trying to do.

Shri

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!

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.

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
  • 5 replies
  • 1121 views
  • 0 likes
  • 3 in conversation