BookmarkSubscribeRSS Feed
srdhoble
Calcite | Level 5

Hi Team,

               Can someone please help me interpret what below sas code is doing.Thanks!

 

%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.;

4 REPLIES 4
Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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);

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

DanielSantos
Barite | Level 11

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; 
* add 14 days; 
else if symget('ENRLMNTDY') eq '15' then START_DT=intnx('day',"&START_DT1"d,14); 
* add -12*&PERIODS-&ROUNOUT to &RUNDATE and adjust at month's end;
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 985 views
  • 3 likes
  • 5 in conversation