BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rogersaj
Obsidian | Level 7

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

3 REPLIES 3
Reeza
Super User

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;

 

rogersaj
Obsidian | Level 7

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

ballardw
Super User

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.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 3 replies
  • 9754 views
  • 2 likes
  • 3 in conversation