I am trying to create a new dataset from an existing one where I only keep certain coded variables. For example, I only want people who are HIV negative AND have engaged in condomless sex or heroin use or sex work, etc.
Here is what I have so far:
data hivneg;
set olddata;
where HIV1 = 2; run;
data hivneg;
where SexBehav3a<=1 or SexBehav4a<=1 or heroin_use=1;
run;
Why doesn't this work? What is the best way to code this?
I think you're missing the SET statement and also suspect one of those ORs should be an AND.
data hivneg; SET oldData; where (SexBehav3a<=1 or SexBehav4a<=1) AND heroin_use=1; run;
@westbestern wrote:
I am trying to create a new dataset from an existing one where I only keep certain coded variables. For example, I only want people who are HIV negative AND have engaged in condomless sex or heroin use or sex work, etc.
Here is what I have so far:
data hivneg; set olddata; where HIV1 = 2; run; data hivneg; where SexBehav3a<=1 or SexBehav4a<=1 or heroin_use=1; run;
Why doesn't this work? What is the best way to code this?
data hivneg;
set olddata;
where (HIV1=2) AND (SexBehav3a=<1 or SexBehav4a=<1 or SexBehav5a=<1);
run;
I tried doing this but it does not work. It says there's a syntax error. Are you able to put multiple "or" statements in that line?
Yes, multiple ORs are allowed.
In the code you posted:
where (HIV1=2) AND (SexBehav3a=<1 or SexBehav4a=<1 or SexBehav5a=<1);
You're using =< but you intend <=
where (HIV1=2) AND (SexBehav3a<=1 or SexBehav4a<=1 or SexBehav5a<=1);
If that doesn't work, please post the log, as @Reeza mentioned.
Also note that SAS uses binary logic (true/false) rather than trinary (true/false/null), and a missing value sorts as a low value. So SexBehav3a<=1 will be true if SexBehav3a has a missing value.
That was it! Thank you!
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.