BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
M_ishk_a
Calcite | Level 5

Please tell me where the following piece of code says not to add 88 if month<>12. I thought it says add 88*multiplied by(dec of each year)

%macro cohort1(mth);

  /**DETERMINING FIRST AND LAST MONTH OF PERFORMANCE**/

  data _null_;

     call symput ("start_mon", &mth+1 + 88*(substr("&mth.",5,2) = 12));

     call symput ("end_mon", &mth+100 );

  run;

  %put &start_mon &end_mon;

%mend;

%cohort1(200512);Explanation if statement in arithmetic calculation

1 ACCEPTED SOLUTION

Accepted Solutions
arnouxvr
Fluorite | Level 6

Explanation is as follows:

The line of code

call symput ("start_mon", &mth+1 + 88*(substr("&mth.",5,2) = "12"));

is saying the following


if (substr("&mth.",5,2) = "12") then call symput ("another_start_mon", &mth + 1 + 88);

else call symput ("another_start_mon", &mth+1);

how the shorten line of codes works as follows:

SAS evaluates quantities inside parentheses before performing any operations.

Let’s call the macro with the following value %cohort1(200512).

SAS will first evaluate the parentheses (substr("&mth.",5,2) = "12")) in this case &mth is 12 so the value inside the parameter would be true or 1.

So the expression is actually &mth+1 + 88*(1).

&mth will the evaluate &mth plus 1 plus (88 * 1)

Let’s call the macro with the following value %cohort1(200601) in this case &mth is not 12 so the value inside the parameter would be false or 0.

So the expression is actually &mth+1 + 88*(0).

&mth will the evaluate &mth plus 1 plus (88 * 0)

Fruther reading: http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780367.htm#a0007... see Numeric Comparisons

View solution in original post

1 REPLY 1
arnouxvr
Fluorite | Level 6

Explanation is as follows:

The line of code

call symput ("start_mon", &mth+1 + 88*(substr("&mth.",5,2) = "12"));

is saying the following


if (substr("&mth.",5,2) = "12") then call symput ("another_start_mon", &mth + 1 + 88);

else call symput ("another_start_mon", &mth+1);

how the shorten line of codes works as follows:

SAS evaluates quantities inside parentheses before performing any operations.

Let’s call the macro with the following value %cohort1(200512).

SAS will first evaluate the parentheses (substr("&mth.",5,2) = "12")) in this case &mth is 12 so the value inside the parameter would be true or 1.

So the expression is actually &mth+1 + 88*(1).

&mth will the evaluate &mth plus 1 plus (88 * 1)

Let’s call the macro with the following value %cohort1(200601) in this case &mth is not 12 so the value inside the parameter would be false or 0.

So the expression is actually &mth+1 + 88*(0).

&mth will the evaluate &mth plus 1 plus (88 * 0)

Fruther reading: http://support.sas.com/documentation/cdl/en/lrcon/62955/HTML/default/viewer.htm#a000780367.htm#a0007... see Numeric Comparisons

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 854 views
  • 1 like
  • 2 in conversation