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

Hello,

 

I'm using SAS 9.4 full edition. For some reason, SAS is not recognizing my character array for when sepsis=1. I get all observations as sepsis=0. Please note that even though the variable appears as a number, it is a character variable so I included the dollar sign and quotation marks. Can someone find an error in my coding?

 

Thanks
Laura

 

data; set;
array DXarray [*] $ DX1-DX70;
do a=1 to 70;
sepsis=0;
if DXarray[a]='99591' then sepsis=1;
end; run;

proc freq; tables sepsis; run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Proper formatting of code goes a long way in finding the errors:

data; /* no dataset name here! */
set; /* and no source dataset here! */
array DXarray [*] $ DX1-DX70;
do a=1 to 70;
  sepsis=0; /* ???? */
  if DXarray[a]='99591' then sepsis=1;
end;
run;

You can see that sepsis is always set to zero inside the loop, and could only end up as 1 if DX70 contains your value.

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

Why are you assigning sepsis=0 in every iteration of the loop in your code-

 

data; set;
array DXarray [*] $ DX1-DX70;
do a=1 to 70;
sepsis=0; /*This doesn't help*/
if DXarray[a]='99591' then sepsis=1;
end; run;

What you prolly need is 

 

data; set;
array DXarray [*] $ DX1-DX70;
do a=1 to 70;
/*sepsis=0; This doesn't help*/
if DXarray[a]='99591' then sepsis=1;
else sepsis=0; end; run;

 

 

lmyers2
Obsidian | Level 7

Thanks for the suggestion but it still reads all observations as sepsis=0, and I've used this formatting before where you set the variable=0 for everyone and then make if/then statements.

novinosrin
Tourmaline | Level 20

Ok everybody has pointed out why you assign sepsis=0 in ever iteration of your loop.. Could your char variables in array have leading and trailing blanks that may cause issues? i am wondering

 

data; set;
array DXarray [*] $ DX1-DX70;
do a=1 to 70;
/*sepsis=0;*/
if strip(DXarray[a])='99591' then sepsis=1; /*notice the change here*/
end; run;
ballardw
Super User

@lmyers2 wrote:

Thanks for the suggestion but it still reads all observations as sepsis=0, and I've used this formatting before where you set the variable=0 for everyone and then make if/then statements.


Show which code you ran.

 

Likely the issue is that used:

if DXarray[a]='99591' then sepsis=1;else sepsis=0;

Which tests ALL values of the array and would only have sepsis 1 if the LAST element of the array meets the condition.

 

Either use the Sepsis=0; before the array without an else to set the value only when true for an element or:

do a = 1 to 70;         
   if DXarray[a]='99591' then do;
     sepsis=1;
     leave;
   end;
   else sepsis=0;
end;

The LEAVE instruction will exit the do loop the first time that 99591 is found. Side bonus: the array index a will have the element of the array that the value was first found if of use.

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

There are a few questions.  As you haven't shown us any data in the form of a datastep, what data is in the dataset you are using, do the variables dx1-70 already exist?  If so, is there a string which is exactly 99591?

 

Some other bits:

- this: "data; set;" is really bad coding, what does it output, what does it input?

- if dx1-70 exist, why do you stipulate the $, they will already be $ will they not?

- Use indents and new lines to make your code readable.

 

Maybe an update:

data want;
  set have;
  array dx{70};
  sepsis=0;
  do i=1 to 70;
    if dx{i}='99591' then sepsis=1;
  end;
run;

proc freq data=want;
  tables sepsis;
run;

 

Kurt_Bremser
Super User

Proper formatting of code goes a long way in finding the errors:

data; /* no dataset name here! */
set; /* and no source dataset here! */
array DXarray [*] $ DX1-DX70;
do a=1 to 70;
  sepsis=0; /* ???? */
  if DXarray[a]='99591' then sepsis=1;
end;
run;

You can see that sepsis is always set to zero inside the loop, and could only end up as 1 if DX70 contains your value.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 810 views
  • 0 likes
  • 5 in conversation