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

hiyya,

 

I am trying to do an if statement with multiple clauses but i keep on getting an error or my new variable only follows the last else statement. please help I know they are all not unknown

here is the code:

data new.step;
set new.HepBV;
length result $40.;
if HbsAg = 'Non-Reactive' and Anti_HBs_surface = 'Reactive' and Anti_Hbc_Total = 'Non-Reactive'
then result = "Immune due to Vaccination";

else if HbsAg = 'Non-Reactive' and Anti_HBs_surface = 'Reactive' and Anti_Hbc_Total = 'Reactive'
then result = "Immune due to Resolved Infection";

else if HbsAg = 'Reactive' and Anti_HBs_surface = 'Ron-Reactive' and Anti_Hbc_Total = 'Reactive' and IgM_anti_HBc = 'Reactive'
then result = "Acute infection";

else if HbsAg = "Reactive" and Anti_HBs_surface = "Non-Reactive" and Anti_Hbc_Total = "Reactive" and IgM_anti_HBc = "Non-Reactive"
then result = "Chronic infection";

else if HbsAg = 'Non-Reactive' and Anti_HBs_surface = 'Non-Reactive' and Anti_Hbc_Total = 'Reactive'
then result = "several possible interpretation";

else if hebimm= "Y" then result = "Immune due to Vaccination";

else result = "Unknown";
RUN;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Sounds like none of the values you are looking for are in the data.

Check what values are actually in the data.

Use the $QUOTE format so that you can see if there are any leading spaces in the values (ODS outputs tend to remove leading spaces from the values they display).

proc freq data=new.HepBV order=freq ;
  tables HbsAg*Anti_HBs_surface*Anti_Hbc_Total / list missing;
  format HbsAg Anti_HBs_surface Anti_Hbc_Total $quote.;
run;

 

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Sounds like none of the values you are looking for are in the data.

Check what values are actually in the data.

Use the $QUOTE format so that you can see if there are any leading spaces in the values (ODS outputs tend to remove leading spaces from the values they display).

proc freq data=new.HepBV order=freq ;
  tables HbsAg*Anti_HBs_surface*Anti_Hbc_Total / list missing;
  format HbsAg Anti_HBs_surface Anti_Hbc_Total $quote.;
run;

 

ballardw
Super User

Any time you get an error, copy the entire procedure or data set code along with all notes, warning and errors from the LOG and then on the forum open a text box using the </> icon and paste the text.

The text box will preserve formatting of any diagnostic characters that SAS includes with many errors.

 

Since you did not provide a Length for the variable Result and if Result did not exist in the data set this code sets the length of the value to 24 because the first use is assigning the value "Immune due to Vaccination", which means any string longer than that is truncated. If Result already existed in the data set then the characteristics of the existing variable may mean values are truncated even more, or if Result was numeric would result in a bunch of messages about trying to convert character values to numeric and failing.

 

You can check your data to see if those first combinations appear in the data with the spelling you used by using:

 

Proc freq data=new.hepbv;
   tables HbsAg*Anti_HBS_surface*Anit_hbc_total / list missing;
run;

 

 

andreas_lds
Jade | Level 19

Are you sure that the variable "HbsAg" has the values "Reactive" and/or "Non-Reactive"? Maybe a format changing the way the value is shown. I would expect the variable to have values like 0 and 1.

 

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

Register now!

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
  • 1041 views
  • 0 likes
  • 4 in conversation