BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
senac255
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

 

View solution in original post

3 REPLIES 3
Astounding
PROC Star

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.

Kurt_Bremser
Super User

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;

 

senac255
Fluorite | Level 6

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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
  • 3 replies
  • 1035 views
  • 2 likes
  • 3 in conversation