DATA Step, Macro, Functions and more

Ne comparson operator resulting in all observations = 1

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Ne comparson operator resulting in all observations = 1

Hi, 
I am attempting to create a new dataset in which a new ethnicity variable 'nonmapac' is derived. This variable will eventually be used for comparisons.  However, when I run the program, all the observations = 1 (which is not the case). I'm not sure where I am going wrong. The code is as below:

data new;
set primhd.clients;
nonmapac=0;
if ethnicgp ne 21
or ethnicg1 ne 21
or ethnicg2 ne 21
or ethnicg3 ne 21
or ethnicgp ne 30
or ethnicg1 ne 30
or ethnicg2 ne 30
or ethnicg3 ne 30
or ethnicgp ne 31
or ethnicg1 ne 31
or ethnicg2 ne 31
or ethnicg3 ne 31
or ethnicgp ne 32
or ethnicg1 ne 32
or ethnicg2 ne 32
or ethnicg3 ne 32
or ethnicgp ne 33
or ethnicg1 ne 33
or ethnicg2 ne 33
or ethnicg3 ne 33
or ethnicgp ne 34
or ethnicg1 ne 34
or ethnicg2 ne 34
or ethnicg3 ne 34
or ethnicgp ne 35
or ethnicg1 ne 35
or ethnicg2 ne 35
or ethnicg3 ne 35
or ethnicgp ne 36
or ethnicg1 ne 36
or ethnicg2 ne 36
or ethnicg3 ne 36
or ethnicgp ne 37
or ethnicg1 ne 37
or ethnicg2 ne 37
or ethnicg3 ne 37
then nonmapac=1;
run;


Accepted Solutions
Solution
‎09-17-2017 10:17 PM
Super User
Posts: 9,870

Re: Ne comparson operator resulting in all observations = 1

[ Edited ]

Short (condensed) excerpt from your code:

if ethnicgp ne 21
or ethnicgp ne 30

You can clearly see that one of those conditions MUST be true, therefore the whole condition will always be true.

I guess you wanted it to be "and".

Also see how you can easily simplify the code:

if
  ethnicgcp not in(21,30,31,32,33,34,35,36,37) and
  ethnicgcp1 not in(21,30,31,32,33,34,35,36,37) and
  ethnicgcp2 not in(21,30,31,32,33,34,35,36,37) and
  ethnicgcp3 not in(21,30,31,32,33,34,35,36,37)
then nonmapac = 1;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 6,626

Re: Ne comparson operator resulting in all observations = 1

You need to think through what conditions should result in 1 vs. 0.  Just these two conditions alone will always give you 1:

 

if ethnicgp ne 21 or ethnicgp ne 30 then nonmapac = 1;

 

Every observation, regardless of the incoming variables, will get a 1 since one comparison or the other (or both) must be true.  It might be as simple as changing OR to AND, or it might be more complex ... it depends on what you are intending as the result.

Solution
‎09-17-2017 10:17 PM
Super User
Posts: 9,870

Re: Ne comparson operator resulting in all observations = 1

[ Edited ]

Short (condensed) excerpt from your code:

if ethnicgp ne 21
or ethnicgp ne 30

You can clearly see that one of those conditions MUST be true, therefore the whole condition will always be true.

I guess you wanted it to be "and".

Also see how you can easily simplify the code:

if
  ethnicgcp not in(21,30,31,32,33,34,35,36,37) and
  ethnicgcp1 not in(21,30,31,32,33,34,35,36,37) and
  ethnicgcp2 not in(21,30,31,32,33,34,35,36,37) and
  ethnicgcp3 not in(21,30,31,32,33,34,35,36,37)
then nonmapac = 1;

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 7

Re: Ne comparson operator resulting in all observations = 1

Posted in reply to KurtBremser

Yes, it seemed all that was needed was to change 'or' to 'and'.
Thanks for the help!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 138 views
  • 2 likes
  • 3 in conversation