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. I'm wondering if you can use a where statement within an array.  I'm looking for a certain diagnosis within an array but only where sex=female. Could someone post a sample code conditioning on a binary predictor?  

 

Thanks so much!!!

Laura

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

To do what you describe requires a small change.  Right now you have:

 

do i=1 to 50;

 

Instead, you would need:

 

if sex=1 then do i=1 to 50;

 

Because your logic contains an OUTPUT statement, nothing would ever get output for other values of sex.

View solution in original post

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Could you post some example test data describing what you mean?  Its impossible for us to guess what your working with.  If its a where clause rather than just code in a datastep, maybe doing something like:

where index(catx(',',of arr:),"female")>0;

So join all the values in the array together and then search for a substring of that.

Astounding
PROC Star

There are many statements that can be used with an array, but a WHERE statement is not possible.

 

The WHERE statement has to examine data values within the incoming SAS data set, before those values get read into memory.  Any arrays that get defined within the DATA step don't even exist at that point.

 

As @RW9 mentioned, if you give a more explicit example of what you are trying to accomplish, there may be some other way to get there.

lmyers2
Obsidian | Level 7

I agree, thanks for being willing to look at my code. I want the below array to run only when another binary variable is sex=1. I'm not sure if I can put a where statement in the do statement. When I tried, it failed so there must be another option but I can't find it on the SAS arrays document. Here it is:

 

data work.workingfile; set final.finalfile; 
array DIAGNOSIS [50] DIAGNOSIS1-DIAGNOSIS50;
do i=1 to 50;
if DIAGNOSIS [i] > ' ' then do;
NEW_DIAGNOSIS = DIAGNOSIS [i];
output;
end;
end;
keep NEW_DIAGNOSIS;
run;
lmyers2
Obsidian | Level 7

Sorry, am trying to post again because it looked like it posted before as one long line of code...Again the goal is to do this array when another binary variable sex=1. 

data work.workingfile; set final.finalfile; 
array DIAGNOSIS [50] DIAGNOSIS1-DIAGNOSIS50; 
do i=1 to 50; if DIAGNOSIS [i] > ' ' then do; 
NEW_DIAGNOSIS = DIAGNOSIS [i]; 
output; 
end;
end;
keep NEW_DIAGNOSIS;
run;
TomKari
Onyx | Level 15

It looks like your problem falls under the general "convert wide to skinny" file problem. Have you considered using PROC TRANSPOSE, which might remove the need to create a data step and to use arrays?

 

Tom

lmyers2
Obsidian | Level 7
Yes, but for all other analysis except this, it's easier to have it in the wide format and use arrays so really I just need to figure this out once.
Reeza
Super User

Two options, one is a WHERE statement the second is a condition on your loop.

 

data work.workingfile; 
set final.finalfile; 

where sex='Female';


array DIAGNOSIS [50] DIAGNOSIS1-DIAGNOSIS50; 
do i=1 to 50;
    if  not missing(DIAGNOSIS [i]) then do; 
              NEW_DIAGNOSIS = DIAGNOSIS [i]; 
             output; 
    end;
end;

keep NEW_DIAGNOSIS;

run;

Second:

data work.workingfile; 
set final.finalfile; 

where sex='Female';


array DIAGNOSIS [50] DIAGNOSIS1-DIAGNOSIS50; 
if sex='Female' then do i=1 to 50;
    if  not missing(DIAGNOSIS [i]) then do; 
              NEW_DIAGNOSIS = DIAGNOSIS [i]; 
             output; 
    end;
end;

keep NEW_DIAGNOSIS;

run;
Astounding
PROC Star

To do what you describe requires a small change.  Right now you have:

 

do i=1 to 50;

 

Instead, you would need:

 

if sex=1 then do i=1 to 50;

 

Because your logic contains an OUTPUT statement, nothing would ever get output for other values of sex.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1645 views
  • 0 likes
  • 5 in conversation