DATA Step, Macro, Functions and more

question about coding an array

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

question about coding an array

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;

 


Accepted Solutions
Solution
‎01-04-2018 10:52 AM
Super User
Posts: 9,582

Re: question about coding an array

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
PROC Star
Posts: 1,328

Re: question about coding an array

[ Edited ]

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;

 

 

Contributor
Posts: 44

Re: question about coding an array

Posted in reply to novinosrin

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.

PROC Star
Posts: 1,328

Re: question about coding an array

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;
Super User
Posts: 13,046

Re: question about coding an array


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.

 

Super User
Super User
Posts: 9,211

Re: question about coding an array

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;

 

Solution
‎01-04-2018 10:52 AM
Super User
Posts: 9,582

Re: question about coding an array

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 132 views
  • 0 likes
  • 5 in conversation