I originally had my data look like this
if index(upcase(tests), 'HEP') then Hepcheck = 1;
else if index(upcase(tests), 'HBSAG') then HEpCheck = 1;
else if index(upcase(remarks), 'HEP') then Hepcheck = 1;
else if index(upcase(remarks), 'HBSAG') then HepCheck = 1;
else Hepcheck = 0;
I tried to condense the code:
data program.ds2054;
set sheet.ds2054;
if index(upcase(tests), 'HEP C' or 'HEPATITIS C') then hepcheck = 0;
else if index(upcase(tests), 'HEP' or 'HBSAG' or 'HBV') then Hepcheck = 1 ;
Else if index(upcase(remarks), 'HEP C' or 'HEPATITIS C') then hepcheck =0;
Else if index(upcase(remarks), 'HEP' or 'HBSAG' or 'HBV') then hepcheck =1;
else Hepcheck = 0;
However I get this error now and no data shows up even though i had a lot of data the other way
The result of the boolean expression OR is a numeric value, not a character string. False results are coded as 0 and true as 1.
So while this tests if HEP C is in the TESTs
if index(upcase(tests), 'HEP C' ) then hepcheck = 0;
This
if index(upcase(tests), 'HEP C' or 'HEPATITIS C') then hepcheck = 0;
is testing if 0 with 11 leading spaces is in tests.
Just test each word independently.
data program.ds2054;
set sheet.ds2054;
if index(upcase(tests), 'HEP C' ) then hepcheck = 0;
else if index(upcase(tests), 'HEPATITIS C') then hepcheck = 0;
etc.
Depends on what you are actually doing.
Are you trying to test the whole values of the variable TESTS? If so then you could use the IN operation instead of the INDEX() function. Then you could list multiple values to test for in a single expression.
if upcase(tests) in ('HEP C' 'HEPATITIS C') then ...
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.