BookmarkSubscribeRSS Feed

Hi,

 

how do I offset this below so that the start= will always be the first month of the year (this year it’s 01jan2016 but next year I need it to say 01jan2017 and so on) and then have the stop= as the previous month? The idea is I want to automate this to prevent user error by keying in the incorrect month.

 

%macro scants(start=01jan2016,stop=01apr2016);

  %local i;

    %do i=-%sysfunc(intck(month,"&start."d,%sysfunc(today()))) %to -%sysfunc(intck(month,"&stop."d,%sysfunc(today())));

      %let runmonth=%sysfunc(intnx(month,%sysfunc(today()),&i.),yymmn6.);

      %let month=%sysfunc(intnx(month,%sysfunc(today()),&i.),monyy5.);

       %let month2=%sysfunc(intnx(month,%sysfunc(today()),&i.),date9.);

       %put &month2.;

 

 

%let startcomp = %sysfunc(intnx(month,"&month2"d,0,b),date9.); %put &startcomp.;

%let endcomp = %sysfunc(intnx(month,"&month2"d,0,e),date9.); %put &endcomp.;

1 REPLY 1
Kurt_Bremser
Super User

Oh my, what an eyesore.

 

For $DEITY's sake, just do your calculations in a data step and use call symput() at the end to store the results in macro variables.

 

data _null_;
today = date();
lastmonth = intnx('month',today,-1,'begin');
yearstart = intnx('year',today,0,'begin');
call symput('stop',put(lastmonth,5.));
call symput('start',put(yearstart,5.));
run;

Note that it is not necessary to format the dates, the raw values can easily be used in comparisons with other date values.

If you want to call a macro repeatedly, calculate (or read) the parameters in a data step and use call execute() to call the macro.

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
  • 1 reply
  • 700 views
  • 1 like
  • 2 in conversation