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
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.
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.
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.
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;
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;
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
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;
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.
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!
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.