BookmarkSubscribeRSS Feed
fengyuwuzu
Pyrite | Level 9
%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 :

 

image.png

 

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

 

7 REPLIES 7
Reeza
Super User

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 :

 

image.png

 

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

 


 

 

Reeza
Super User

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);
fengyuwuzu
Pyrite | Level 9

Thank you so much. Now I understand.

PaigeMiller
Diamond | Level 26
data try;
        x1=&HazardRate1.; x2=&HazardRate2.;
        y1=1.0/(&HazardRate1.); y2=1/(&HazardRate2.);
        z1=1/x1; z2=1/x2;
RUN;
--
Paige Miller
ballardw
Super User

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.

Tom
Super User Tom
Super User

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 );
heffo
Pyrite | Level 9

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

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 7 replies
  • 747 views
  • 0 likes
  • 6 in conversation