BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hansi_12345
Fluorite | Level 6
data work.test_out;
set work.test_in;
if value < 0 and item= 1 or item = 2  Or  item = 3 Or item =4 Then document= 4;
else if value > 0 and item = 5 or item = 6  Or  item = 7 Or item =8 Then document= 3;
else if value < 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 3;
else if value > 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 4;
else document= ' ';
run;

Hi SAS-Experts,

my code is like above and i have following problem:

 

When i have e.g. (+) 41 as value with Item-Nr. 10 i get  document-Nr. 3 but i expect 4 for document.

The question is, where my mistake is. Maybe i have to clamp something but i dont know what.

 

Would be nice, if u could help me.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
else if value < 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 3;

In SAS (and I think  most computer languages), AND takes precedence over OR. In other words, the AND is evaluated first, and so your line above is really interpreted as if you had written the line below:

 

else if (value < 0 and item = 9) or item = 10  Or  item = 11 Or item =12 Then document= 3;

because AND is evaluated before OR is evaluated. So when Item=10, it fails the test (value<0 and item=9) but then it passes the next test which is or item=10, and so DOCUMENT=3 is the result. So, what you should be writing (and it is always a good idea to use parentheses to make it much more visible what you are trying to achieve regardless of the order or precendence)

 

else if value < 0 and (item = 9 or item = 10 or item = 11 or item =12) then document= 3;

 

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
else if value < 0 and item = 9 or item = 10  Or  item = 11 Or item =12 Then document= 3;

In SAS (and I think  most computer languages), AND takes precedence over OR. In other words, the AND is evaluated first, and so your line above is really interpreted as if you had written the line below:

 

else if (value < 0 and item = 9) or item = 10  Or  item = 11 Or item =12 Then document= 3;

because AND is evaluated before OR is evaluated. So when Item=10, it fails the test (value<0 and item=9) but then it passes the next test which is or item=10, and so DOCUMENT=3 is the result. So, what you should be writing (and it is always a good idea to use parentheses to make it much more visible what you are trying to achieve regardless of the order or precendence)

 

else if value < 0 and (item = 9 or item = 10 or item = 11 or item =12) then document= 3;

 

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hello @Hansi_12345,

 

You can also avoid the chains of "OR" expressions by using the IN operator:

data work.test_out;
set work.test_in;
if value < 0 and item in (1:4) or value > 0 and item in (9:12) then document= 4;
else if value > 0 and item in (5:8) or value < 0 and item in (9:12) then document= 3;
else document= .;
run;

Note that I replaced the incorrect character missing ' ' by a numeric missing value . in the last ELSE statement. Also, remember that a missing value in variable value would satisfy the condition value < 0, not only negative values. To exclude missing values, you can write

.z < value < 0

(using the largest special missing value .z).

PaigeMiller
Diamond | Level 26

All excellent points from @FreelanceReinh 

 

I would modify his code to improve readability only by adding parentheses as follows:

 

if (value < 0 and item in (1:4)) or (value > 0 and item in (9:12)) then document= 4;
else if (value > 0 and item in (5:8)) or (value < 0 and item in (9:12)) then document= 3;

To me this makes the code just a little more clear when I read it (or when others read it). It's a "style" thing, obviously it works without adding the extra parentheses.

--
Paige Miller
Hansi_12345
Fluorite | Level 6

I dont know where i can answer to all of you at once. So Thanks to all of you for your fast answers 😄

Hansi_12345
Fluorite | Level 6

I have a similar problem like yesterday but now with ...and... and. 

I realy dot now why i am always struggling with this, in vba i have not such problems....

My Code is like below:

 

data work.xyz;
if (a = 4 and b = 11108 and c = 6) Then d = 111;
else if (a = 3 and b = 11219 and c = 6) Then d = 112;
else if (a= 4 and b = 11223 and c = 6) Then d = 113;
else if (a = 3 and b = 11223 and c = 6) Then d = 114;
else if (a = 3 and b = 11203 and c = 6) Then d = 115;
else if (a = 4 and b = 11205 and c = 6) Then d = 116;
else if (a = 3 and b = 11206 and c = 6) Then d = 117;
else if (a = 4 and b = 11200 and c = 6) Then d = 118;
else if (a = 4 and b = 11319 and c = 7) Then d = 119;
else if (a = 4 and b = 11319 and c = 8) Then d = 120;
else if (a = 3 and b = 11319 and c = 7) Then d = 121;
else if (a = 3 and b = 11319 and c = 8) Then d = 122;
else if (a = 4 and b = 11323 and c = 7) Then d = 123;
else if (a = 4 and b = 11323 and c = 8) Then d = 124;
else if (a = 3 and b = 11323 and c = 7) Then d = 125;
else if (a = 3 and b = 11323 and c = 8) Then d = 126;
else if (a = 3 and b = 11303 and c = 7) Then d = 127;
else if (a = 3 and b = 11303 and c = 8) Then d = 128;
else if (a = 4 and b = 11305 and c = 7) Then d = 129;
else if (a = 4 and b = 11305 and c = 8) Then d = 130;
else if (a = 3 and b = 11306 and c = 7) Then d = 131;
else if (a = 3 and b = 11306 and c = 8) Then d = 132;
else if (a = 4 and b = 11300 and c = 7) Then d = 133;
else if (a = 4 and b = 11300 and c = 8) Then d = 134;
else d = 555;
    set work.zyx;
run;

I placed the brackets at different places but i didn't find the right one.

 

If i have for a the value 3, for b the value 11319 and for c the value 7, I expect to get 121 as value but what i get is 555.

 

Thanks for your help.

 

Kurt_Bremser
Super User

Keep in mind that all variables (that are not implicitly or explicitly retained) are set to missing at the start of the data step iteration. This means that you have only missing values when you evaluate your condition.

Place the SET statement first in the data step.

For illustration, see these codes:

data test1;
if age = 13;
set sashelp.class;
run;

data test2;
set sashelp.class;
if age = 13;
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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1397 views
  • 4 likes
  • 4 in conversation