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!
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;
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;
Reeza, you're my hero! It worked after I removed the equal sign and quotation marks.
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.