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.

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.
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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 757 views
  • 6 likes
  • 4 in conversation