BookmarkSubscribeRSS Feed
aiabamba
Fluorite | Level 6

I am having an issue with a certain piece of my code:

 

data RESULTS;
set TESTDATA;
if RULE NE 'ABC' AND STATUS NE 1;

run;

 

When I run this,  if the RULE is NE 'ABC' and the STATUS is EQ 2, the data does not show up because it only looks at the 1st statement. Please help!

10 REPLIES 10
tarheel13
Rhodochrosite | Level 12

can you post some sample data as datalines? also, for AND, both conditions have to be true. 

japelin
Rhodochrosite | Level 12

I think code is correct...

data testdata;
  input RULE $ STATUS;
datalines;
ABC 1
AAA 1
AAA 2
ABC 2
;
run;

data RESULTS;
set TESTDATA;
if RULE NE 'ABC' AND STATUS NE 1;
run;

Result:

2022-01-31_01h12_58.png

 

Show up what data you are using.

 

aiabamba
Fluorite | Level 6

Here is an example:

 

IDRULESTATUS  
123ABC1 This should not show up and it doesn't
123ABC2 This does not show up when it should
tarheel13
Rhodochrosite | Level 12

the second one will not show up because you put rule ne 'ABC'. since rule='ABC', it will not show up.

aiabamba
Fluorite | Level 6

Is there a way for the 2nd line to show up? I need it to apply BOTH conditions. I already tried putting the statements in parenthesis.

tarheel13
Rhodochrosite | Level 12

your logic is wrong. my point was that as long as you have if rule ne 'ABC', then the second one will never show up. the parentheses will not fix that either. if you need the second one to show, then you must get rid of rule ne ABC. 

aiabamba
Fluorite | Level 6

I see. Interesting enough, I have other rules and the statements worked. It is only this one particular line that does work. 

 

data TEST;
set RESULTS;
if RULE NE ‘Rule 1’; - This works
if RULE NE ‘Rule 2’ and status NE 1; - Does not work
if RULE NE 'Rule 3’ and status NE 1; - Works
if RULE NE ‘Rule 4’ and status NE 1; - Works
run;

tarheel13
Rhodochrosite | Level 12

I don't think you understand how AND works or else you have written your code according to incorrect logic. It's not entirely clear to me what you're trying to do but if you want the second one to be included based on sample data you provided, then you would just do if status ne 1. 

aiabamba
Fluorite | Level 6

I do. This code actually worked before and I just needed a re-run. I wanted to create a sub-set data based on the text contained under the RULE column and the status. It worked for some statements but not for a particular statement.

 

Thank you for your inputs. I will try and find a way to fix it. 

Tom
Super User Tom
Super User

If you think it worked before perhaps the data is different?  Maybe the value that looks like 1 is not exactly 1?

 

If is hard to tell what you want to happen.  But here is a way for you to examine the impact the tests you are doing.  Let's make a dummy dataset with some values of RULE and STATUS.

data results;
  do rule='Rule 1','Rule 2','Other';
    do status=1,2;
      output;
    end;
  end;
run;

Now let's create some binary 1/0 variables from the various conditions you are testing.

data TEST;
  set RESULTS;
  r1= rule ne 'Rule 1';
  r2= rule ne 'Rule 2';
  r3= rule ne 'Rule 3';
  s1= status ne 1;

  test1= r1 and s1;
  test2= r2 and s1;
  test3= r3 and s1;
run;

So the R1-R3 variables are the various RULE NE xxx conditions.  The S1 variable test if STATUS is no one.

Then the TEST1-TEST3 show the impact of using AND with one of the R variables and the S variables.

So TEST1 was what you original code was testing.

Obs     rule     status    r1    r2    r3    s1    test1    test2    test3

 1     Rule 1       1       0     1     1     0      0        0        0
 2     Rule 1       2       0     1     1     1      0        1        1
 3     Rule 2       1       1     0     1     0      0        0        0
 4     Rule 2       2       1     0     1     1      1        0        1
 5     Other        1       1     1     1     0      0        0        0
 6     Other        2       1     1     1     1      1        1        1

So which of these 6 observations did you actually want to select?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 1239 views
  • 3 likes
  • 4 in conversation