SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

question about array

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

question about array

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


Accepted Solutions
Solution
a week ago
Super User
Posts: 5,610

Re: question about array

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


All Replies
Super User
Super User
Posts: 8,177

Re: question about array

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.

Super User
Posts: 5,610

Re: question about array

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.

Contributor
Posts: 25

Re: question about array

Posted in reply to Astounding

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;
Contributor
Posts: 25

Re: question about array

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;
PROC Star
Posts: 1,178

Re: question about array

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

Contributor
Posts: 25

Re: question about array

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.
Super User
Posts: 20,226

Re: question about array

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;
Solution
a week ago
Super User
Posts: 5,610

Re: question about array

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.

☑ This topic is solved.

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

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