Contributor
Posts: 20

# Macro Code Explanation

[ Edited ]

Hi Team,

%let start_dt1=%sysfunc(inputn(%SYSFUNC(INTNX(MONTH, &RUN_DATE.,%EVAL(-12*&PERIODS.-&RUNOUT.),B),yymmdd8.), yymmdd8.), date9.);

%macro my_function();
%global start_dt;

%if %sysevalf(&ENRLMNTDY. = 1) %then
%do;
%let start_dt = &start_dt1.;
%end;
%else %if %sysevalf(&ENRLMNTDY. = 15) %then
%do;
%let start_dt = %SYSFUNC(INTNX(DAY,"&start_dt1."d,14),DATE9.);
%end;
%else %if %sysevalf(&ENRLMNTDY. = "Last") %then
%do;
%let start_dt = %sysfunc(inputn(%SYSFUNC(INTNX(MONTH, &RUN_DATE.,%EVAL(-12*&PERIODS.-&RUNOUT.),E),yymmdd8.), yymmdd8.), date9.);
%end;

%mend my_function;

%my_function();

%let end_dt=%sysfunc(inputn(%SYSFUNC(INTNX(MONTH, &RUN_DATE.,%EVAL(-1-&RUNOUT.),E),yymmdd8.), yymmdd8.), date9.);
%put &start_dt. &end_dt.;

Super User
Posts: 9,925

## Re: Code Explanation help needed

Actually, this code "does" nothing. It only sets macro variables to certain values and puts those values into the log.

Since we don't know the values of &periods, &runout, &run_date and &enrlmntdy, we can't even test that macro code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 7,941

## Re: Code Explanation help needed

[ Edited ]

Do you not understand the purpose of the code? Or the details of the complex statements?

The purpose seems to be to calculate start and end periods given a RUN_DATE, PERIODS, RUNOUT and ENRLMNTDY values.

As to the more complex statement, perhaps they would be easier to understand if the unneeded complexity is removed.

Change

``````%let start_dt1=%sysfunc(inputn(%SYSFUNC(INTNX(MONTH, &RUN_DATE.,%EVAL(-12*&PERIODS.-&RUNOUT.),B),yymmdd8.), yymmdd8.), date9.);
``````

to

``%let start_dt1=%sysfunc(intnx(month,&run_date,-12*&periods-&runout,b),date9);``

So this statement is saying give me the first date of the month that is &PERIODS years and &RUNOUT months before the date that is in RUN_DATE.  And format the values as text string in ddMMMyyyy format.

Similarly later on inside of the macro you are getting the LAST day of the same month using the same logic as before.

``````%let start_dt = %sysfunc(inputn(%SYSFUNC(INTNX(MONTH, &RUN_DATE.,%EVAL(-12*&PERIODS.-&RUNOUT.),E),yymmdd8.), yymmdd8.), date9.);
``````

But since you already calculated the first of the month you could just convert it to :

``%let start_dt = %sysfunc(intnx(month,"&start_dt1"d,0,e),date9);``

Super User
Posts: 9,441

## Re: Macro Code Explanation

The purpose of this code is to make me rapidly hit the delete key.  It does virtually nothing except sit there as a mass of obfuscated code sucking up valuable programming hours.

Super Contributor
Posts: 474

## Re: Macro Code Explanation

[ Edited ]

Hi.

Unless there is the need of repeatabilty, no need of macro coding there.

All it's doing is conditionally setting a START_DT macro variable, based on some other macro variables.

But it's really bad macro coding, and it could be done with much simpler/cleaner data step code:

``````data _null_;
* set 1;
if symget('ENRLMNTDY') eq '1' then START_DT=1;
else if symget('ENRLMNTDY') eq '15' then START_DT=intnx('day',"&START_DT1"d,14);
else if upcase(symget('ENRLMNTDY')) eq 'LAST' then START_DT=intnx('month',&RUN_DATE,-12*&PERIODS-&ROUNOUT,'end');
call symput('START_DT',put(START_DT,date9.)); * format and set macro var;
quit;``````

Daniel Santos @ www.cgd.pt

Discussion stats
• 4 replies
• 217 views
• 3 likes
• 5 in conversation