DATA Step, Macro, Functions and more

Selecting multiple numerical values

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Selecting multiple numerical values

I'm sure there's a simple answer to this, but how do I specify several numerical values that I'm interested in?

For example, for variable "hivstatus" I want to select values 0,1,2 to mark those as "Known HIV status."

I tried this but it didn't work:

if hivstatus = in('0','1','2') then  status = 1;

 

Similarly, how do I select the opposite (ie, NOT 0,1,2)?

Thank you!

 

 


Accepted Solutions
Solution
‎07-20-2016 01:23 PM
Super User
Posts: 19,767

Re: Selecting multiple numerical values

[ Edited ]

Your code:

if hivstatus = in('0','1','2') then  status = 1;

 

SAS Code, assuming character variables. If this is numeric variable you don't need the quotation marks.

if hivstatus IN ('0','1','2') then  status = 1;

The corrresponding NOT would be:

 

if hivstatus NOT IN ('0','1','2') then  status = 0;

 

EDIT: You've said numeric variables so the code should be:

if hivstatus IN ( 0, 1,  2 ) then  status = 1;

 

View solution in original post


All Replies
Solution
‎07-20-2016 01:23 PM
Super User
Posts: 19,767

Re: Selecting multiple numerical values

[ Edited ]

Your code:

if hivstatus = in('0','1','2') then  status = 1;

 

SAS Code, assuming character variables. If this is numeric variable you don't need the quotation marks.

if hivstatus IN ('0','1','2') then  status = 1;

The corrresponding NOT would be:

 

if hivstatus NOT IN ('0','1','2') then  status = 0;

 

EDIT: You've said numeric variables so the code should be:

if hivstatus IN ( 0, 1,  2 ) then  status = 1;

 

Occasional Contributor
Posts: 12

Re: Selecting multiple numerical values

Reeza, you're my hero! It worked after I removed the equal sign and quotation marks.

Super User
Posts: 11,335

Re: Selecting multiple numerical values

An alternate approach for some purposes could be to use a custom format.

proc format;
value HIVStatus
0,1,2 = 'Text description'
3,4   = 'Other text'
.     = 'Missing'
;

proc freq data=have;
   tables HIVstatus;
   format HIVstatus HIVStatus. ;
run;

Would display the frequency table with the text assigned by the Format HIVStatus. This is a useful tool as you can apply different display values just by changing the format for a specific report procedure. For example you could have a long and short version of text for the individual or groups of codes or create different groups entirely. Many of the analysis and graphing procedures will honor the formatted value. So you don't have to add a new variable when wanting to use a new assignment.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 312 views
  • 2 likes
  • 3 in conversation