DATA Step, Macro, Functions and more

Text Date to SAS Date

Accepted Solution Solved
Reply
Contributor sks
Contributor
Posts: 42
Accepted Solution

Text Date to SAS Date

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


Accepted Solutions
Solution
‎10-18-2011 03:10 PM
PROC Star
Posts: 7,363

Text Date to SAS Date

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


All Replies
PROC Star
Posts: 7,363

Text Date to SAS Date

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;

Super User
Super User
Posts: 6,502

Text Date to SAS Date

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

Contributor sks
Contributor
Posts: 42

Text Date to SAS Date

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

Solution
‎10-18-2011 03:10 PM
PROC Star
Posts: 7,363

Text Date to SAS Date

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;

Contributor sks
Contributor
Posts: 42

Text Date to SAS Date

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

Shri

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 214 views
  • 0 likes
  • 3 in conversation