BookmarkSubscribeRSS Feed
embee47
Calcite | Level 5
As a SAS novice I need someone to spell out exactly, in layman's terms, what is happening in the bold bits here:

rsubmit;
%let shift_today=intnx('month',today(),-1);
%let month_criteria=(year(&shift_today)*100+month(&shift_today));
endrsubmit;

I 'think' it is saying shift_today is a month and is always one month before the current month. So shift_today would be October?

The month_criteria I don't get!

any help would be massively appreciated! Thanks
2 REPLIES 2
Peter_C
Rhodochrosite | Level 12
these statements are not executed in the lines you show here.
These lines prepare strings which would execute validly in other contexts when the code refers to &shift_today or &month_criteria

The first of these statements will derive a date (number of days since 1960) for the first of the month before the datetime when that syntax is finally executed.
The second is an attempt (which might work) to derive the date string YYYYMM for the month before the month when that &month_criteria is resolved/executed.

it could be done so much more reliably, and understandably (imho)

Normally a macro variable &something is a convenient way of re-using a constant.
The risk with using &month_criteria more than once in a sas program occurs when the sas program starts just before midnight at the end of a month - and there is a chance (=risk) that a subsequent use of &month_criteria in the same sas program would execute after that midnight - and so return a different "month". That is not normally what one would want.

Rather than store syntax in &month_criteria, store the value, with
%let month_criteria = %sysfunc( intnx( month, "&sysdate"d, -1), yymmN6 ) ;
where &sysdate is the built-in string for the start date of the SAS system which runs the program.

Then all references to &month_criteria will result in the same string.

and if you are invoking my code in a server that runs for many days- and so may have historic start date, replace "&sysdate"d with %sysfunc(date())

Of course if the purpose of the statement
%let month_criteria=(year(&shift_today)*100+month(&shift_today));
is to confuse, or to prepare an explanation
it may achieve its purpose.
but I don't know what the author had in mind

peterC
embee47
Calcite | Level 5
Peter, it is a piece of code in a program I have inherited and I just wanted to get my head around exactly what it was doing. I think your explanation plus a little bit of playing around has given me enough of an insight now to understand why it is there.

Thanks for your time!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 869 views
  • 0 likes
  • 2 in conversation