Bogdan
Bogdan

Reputation: 119

Interpretation of double inequality in SAS

I wrote

do while (A<=B<=C);...;end

and I did not obtain what I expected (which is A<=B and B<=C). So how does SAS interpret the expression A<=B<=C ? Note: SAS did not give an error for A<=B<=C.

Upvotes: 1

Views: 298

Answers (2)

DomPazz
DomPazz

Reputation: 12465

This works as you expect in a Data Step. It does not work in PROC IML.

1189  data _null_;
1190  a = 10;
1191  b = 10;
1192  c = 15;
1193
1194  do while(a<=b<=c);
1195      put b=;
1196      b = b + 1;
1197      /*Abort if this runs away*/
1198      if b > 20 then
1199          stop;
1200  end;
1201  run;

b=10
b=11
b=12
b=13
b=14
b=15
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

1203  proc iml;
NOTE: IML Ready
1204  a = 10;
1205  b = 10;
1206  c = 15;
1207  file log;
1208
1209  do while (a<=b<=c);
1210      put "B=" b;
1211      b = b+1;
1212
1213      if b>20 then stop;
1214  end;
B=       10
B=       11
B=       12
B=       13
B=       14
B=       15
B=       16
B=       17
B=       18
B=       19
B=       20
1215  quit;
NOTE: Exiting IML.
NOTE: PROCEDURE IML used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

IML has slightly different logical syntax than Base SAS. In IML, use

do while ( (a<=b) & (b<=c));

Upvotes: 2

stevepastelan
stevepastelan

Reputation: 1304

I believe it evaluates left to right:

(A <= B) <= C

A <= B evaluates to 0 or 1. This value is then compared to C.

Upvotes: 3

Related Questions