DATA Step, Macro, Functions and more


Frequent Contributor
Posts: 87


Hi, I have the below code written. Macro bus_str_m1 and current_ui_m1 resolves to '31-Jun-2014' when dd = 31July2014. However '31-Jun-2014' is an invalid date, I would like it to be '30-Jun-2014'. How would I re-write the code if I want the variables work the same on all other days except for month end or invalid dates? Is there a way SAS can automatically check for invalid dates?

data _null_;

               CALL SYMPUT('yyyymmdd', put(today(), yymmddn8.) );

                CALL SYMPUT('yyyymm', put(today(), yymmn6.) );


data _null_;
                dd = day(today());
        MONTH_STR_M1    = intnx('month',input(put(&yyyymm, 6.), yymmn6.),       0);
        MONTH_END_M1    = intnx('month',input(put(&yyyymm, 6.), yymmn6.),       1);
        MONTH_END_P1    = intnx('month',input(put(&yyyymm, 6.), yymmn6.),       -1);
        MONTH_CURR_UI   = intnx('month',input(put(&yyyymm, 6.), yymmn6.),       -13);


        CALL SYMPUT('bur_str_m1', "'"||put(dd,z2.)||"-"||PUT(MONTH_END_P1, monname3.)||"-"||put(MONTH_STR_M1,year4.)||"'");   
        CALL SYMPUT('bur_end_m1', "'"||put(dd,z2.)||"-"||PUT(MONTH_STR_M1, MONNAME3.)||"-"||PUT(MONTH_END_M1,YEAR4.)||"'");
        CALL SYMPUT('current_ui_m1', "'"||put(dd,z2.)||"-"||PUT(MONTH_CURR_UI, MONNAME3.)||"-"||PUT(MONTH_CURR_UI,YEAR4.)||"'");
        CALL SYMPUT('pre_yyyymm', PUT(MONTH_END_P1,yymmn6.));



%put &bur_str_m1 &bur_end_m1 &current_ui_m1 &pre_yyyymm;

'31-Jun-2014' '31-Jul-2014' '31-Jun-2013' 201406



Valued Guide
Posts: 3,208

Re: dates

1/ Do not mix up the different dates doing a calculation on your own.

The intnx is meant to get a correct date calculated to specific interval. see alignment: SAS(R) 9.4 Functions and CALL Routines: Reference, Second Edition

2/ getting back to a formatted date, just use sas the correct formats on a date (date/time) value, no need to compose something yourself.

---->-- ja karman --<-----
Occasional Contributor
Posts: 12

Re: dates

Use the intnx function to return the same day of the previous month:


SAS designed this very intelligently for month's with 31 days.  In the event that the previous month does not have as many days, it will default to the end of the month.  So running this with a current date of 3/29/2014 would return 2/28/2014 as the same day from the previous month.

Then, rather than build your own string, just use the date11 format:

data _null_;
                dd = day(today());

               pd = intnx('month',dd,-1,'s');

              CALL SYMPUT('bur_str_m1', "'"||put(pd,date11.)||"'");   


Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation