Pyrite | Level 9

## reciprocal problem of macro variables

``````%let HazardRated = log(2)/20;
%let HazardRate1 = log(2)/17;
%let HazardRate2 = log(2)/25;

data try;
x1=&HazardRate1.; x2=&HazardRate2.;
y1=1.0/&HazardRate1.; y2=1/&HazardRate2.;
z1=1/x1; z2=1/x2;
RUN;``````

I got :

why y1 ^= z1? y2 ^= z2? what is the problem? what happened to Y1 and Y2?

7 REPLIES 7
Super User

## Re: reciprocal problem of macro variables

Order of operations but for some reason still not matching your numbers.

Y1 = 1.0/log(2)/20 = 1 / 0.693147 / 20 = 1.442695/20 = 0.072135

Z1 = 1/ (log(2)/ 20) = 1 / (0.693147/20) = 1 / 0.03457 = 28.85

@fengyuwuzu wrote:
``````%let HazardRated = log(2)/20;
%let HazardRate1 = log(2)/17;
%let HazardRate2 = log(2)/25;

data try;
x1=&HazardRate1.; x2=&HazardRate2.;
y1=1.0/&HazardRate1.; y2=1/&HazardRate2.;
z1=1/x1; z2=1/x2;
RUN;``````

I got :

why y1 ^= z1? y2 ^= z2? what is the problem? what happened to Y1 and Y2?

Super User

## Re: reciprocal problem of macro variables

I'm guessing you want this, untested:

``````%let HazardRated = %sysevalf(%sysfunc(log(2))/20);
%let HazardRate1 = %sysevalf(%sysfunc(log(2))/17);
%let HazardRate2 = %sysevalf(%sysfunc(log(2))/25);``````
Pyrite | Level 9

## Re: reciprocal problem of macro variables

Thank you so much. Now I understand.

Diamond | Level 26

## Re: reciprocal problem of macro variables

``````data try;
x1=&HazardRate1.; x2=&HazardRate2.;
y1=1.0/(&HazardRate1.); y2=1/(&HazardRate2.);
z1=1/x1; z2=1/x2;
RUN;``````
--
Paige Miller
Super User

## Re: reciprocal problem of macro variables

Consider:

```data try;
x1=&HazardRate1.;
x2=&HazardRate2.;
y1=1.0/(&HazardRate1.);
y2=1/(&HazardRate2.);
z1=1/x1;
z2=1/x2;
RUN;```

or consider

```data example;
x1 = 1/2/3;
x2 =(1/2)/3;
x3 = 1/(2/3);
run;```

when you have multiple operators with the same level of priority, such as division, and no parentheses to control order the evaluation is done from left to right.

So your code is equivalent to X1, which evaluates in the order indicated by X2 in my Example data set. The order you likely intended is the X3 version.

Super User

## Re: reciprocal problem of macro variables

The macro variables just contain text.

So for Y1 you are running:

``y1=1.0/log(2)/17;``

But for Z1 because you calculated X1 already you are running this instead.

``z1=1.0 / ( log(2)/17 );``
Pyrite | Level 9

## Re: reciprocal problem of macro variables

@Reeza is correct that it is because of parenthesis.

You are getting the calculation of 1.0 divided by log(2) and then that result divided by 17.

Also, I think it confuses people when your example contains three different variables and two examples.

``````%let HazardRate1 = log(2)/17;

data try;
x1=&HazardRate1.;
y1=1.0/&HazardRate1.;
y1_1=1.0/(&HazardRate1.);  *Force the calculation of this part first.;
z1=1/x1;
RUN;``````

Discussion stats
• 7 replies
• 747 views
• 0 likes
• 6 in conversation