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!
can you post some sample data as datalines? also, for AND, both conditions have to be true.
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:
Show up what data you are using.
Here is an example:
ID | RULE | STATUS | ||
123 | ABC | 1 | This should not show up and it doesn't | |
123 | ABC | 2 | This does not show up when it should |
the second one will not show up because you put rule ne 'ABC'. since rule='ABC', it will not show up.
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.
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.
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;
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.
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.
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?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.