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;
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;
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.
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;
Yes, it seemed all that was needed was to change 'or' to 'and'.
Thanks for the help!
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!
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.
Ready to level-up your skills? Choose your own adventure.