BookmarkSubscribeRSS Feed
Cooksam13
Fluorite | Level 6

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

 

 

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
71:38 71:49 72:43 72:52 72:63 73:32 73:43 74:32 74:41 74:52 75:44 75:55 76:44 76:53 76:64
80:39 80:53 81:32 81:46 82:42 82:56
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
71:46 72:60 73:40 74:49 75:23 75:52 76:23 76:61 80:50 81:43 82:22 82:53
NOTE: Variable OtherClassBDetails is uninitialized.
NOTE: Invalid numeric data, 'HEP C' , at line 71 column 38.
NOTE: Invalid numeric data, 'HEPATITIS C' , at line 71 column 49.
NOTE: Invalid numeric data, 'HEP' , at line 72 column 43.
3 REPLIES 3
Tom
Super User Tom
Super User

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.
Cooksam13
Fluorite | Level 6
thank you for your help! so is there a way to condense this string at all? or should I just leave it as is?
Tom
Super User Tom
Super User

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 ...

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!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 3 replies
  • 510 views
  • 0 likes
  • 2 in conversation