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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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