useR
useR

Reputation: 3082

SAS yyyymmdd to last date of month

I updates the yyyymmdd variable e.g. 20141123 manually. and transforms 20141123 to 23NOV2014. I face some problem when I try to find the last day of the month which is 30NOV2014 and store it as enddate. My approach is using intnx(). While

data _null_;
    format yyyymmdd $8.;
    yyyymmdd = '20141123';

    yyyy = put(substr(yyyymmdd, 1, 4), $4.);
    yy = put(substr(yyyymmdd, 3, 2), $2.);
    mm = put(substr(yyyymmdd, 5, 2), $2.);
    dd = put(substr(yyyymmdd, 7, 2), $2.);

    date = put(mdy(mm, dd, yyyy), date9.);

    enddate = put(intnx('month',date,1,'end'), date9.);
run;

Thanks for your help.

Upvotes: 0

Views: 4365

Answers (1)

DomPazz
DomPazz

Reputation: 12465

A few things.

  1. input() converts characters to numeric.

  2. You are forcing SAS to convert characters of YYYY, MM, DD, and date to numeric variables. I'm sure you saw the NOTES to that effect in the LOG.

  3. You are getting the last day of the next month. Current + 1. You want Current + 0.

Try this

data _null_;
format yyyymmdd $8.;
yyyymmdd = '20141123';

yyyy = input(substr(yyyymmdd, 1, 4), $4.);
yy = input(substr(yyyymmdd, 3, 2), $2.);
mm = input(substr(yyyymmdd, 5, 2), $2.);
dd = put(substr(yyyymmdd, 7, 2), $2.);

date = mdy(mm, dd, yyyy);

put date date9.;
enddate = put(intnx('month',date,0,'end'), date9.);
put enddate;
run;

Upvotes: 4

Related Questions