BookmarkSubscribeRSS Feed
westbestern
Obsidian | Level 7

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?

8 REPLIES 8
Reeza
Super User

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?


 

westbestern
Obsidian | Level 7
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?

Reeza
Super User
Show the actual error please, it usually has enough information to debug the issue.
Quentin
Super User

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.

westbestern
Obsidian | Level 7

That was it! Thank you!

Reeza
Super User
Interestingly not in your first post though....
ballardw
Super User
Which is why I tend to use the mnemonic LE and GE for most of these comparisons. Then I don't get the order wrong.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 8 replies
  • 1625 views
  • 6 likes
  • 4 in conversation