Solved
New Contributor
Posts: 3

# Help with sub setting output data sets

Novis, trying to learn IML.  In the program below, I am trying to pick the LBN values which satisfies low - high range, create output dataset test1  as subset of test which satisfy this condition in most efficient way (without listing all of the variables).

Thanks.

proc iml ;
use test;

read all var _NUM_ into x[c=NumVar];
close test;

r_x=nrow(x);
c_x=ncol(x);

/* Lab date variable d */
lbn = x[, 'd'];

/*Upper Lower limits for week 100 boundaries*/
low = x[, 'const'] + 2;
high = x[, 'const'] + 15;

outc = x[,'s'];
/* outc = j(nrow(s), 1); */
idx = loc (lbn <= high & lbn >= low );

y=j(nrow());
if ncol (idx)> 0 then y[idx]=x;

print x , y;
print satisfy "Criteria Met";
print (lbn[idx])[label='lab date'] , (low[idx]) [label='lowb'],
(high[idx]) [label='highb'], [label='test'];

create TEST1 from y ;
append from y;
close test1;

quit;

Accepted Solutions
Solution
‎02-05-2018 08:36 AM
SAS Super FREQ
Posts: 4,172

## Re: Help with sub setting output data sets

[ Edited ]

You have read all the variables into X. So use the LOC function to find the rows that satisfy the condition, then use the index to subset the rows of X:

``````idx = loc (lbn <= high & lbn >= low );
y = x[idx,];   /* subset rows, include all columns */
create TEST1 from y[c=NumVar];
append from y;
close test1;``````

When you leave the column index blank (as above), it means "use all columns." For more information about subscripting matrices, see "Extracting elements from matrix."

All Replies
New Contributor
Posts: 3

## Re: Help with sub setting output data sets

Original test data:

data test (drop=v);

p=1; d=2; z=3; const=11; s=0;
output;

do v=1 to 15 by 1;
const=11;
p+3; d+5; z+7;
output;

end;

run;

Solution
‎02-05-2018 08:36 AM
SAS Super FREQ
Posts: 4,172

## Re: Help with sub setting output data sets

[ Edited ]

You have read all the variables into X. So use the LOC function to find the rows that satisfy the condition, then use the index to subset the rows of X:

``````idx = loc (lbn <= high & lbn >= low );
y = x[idx,];   /* subset rows, include all columns */
create TEST1 from y[c=NumVar];
append from y;
close test1;``````

When you leave the column index blank (as above), it means "use all columns." For more information about subscripting matrices, see "Extracting elements from matrix."

New Contributor
Posts: 3

## Re: Help with sub setting output data sets

Suppose I would like to set values of s to 1 where conditions are met, and leave them as 0 otherwise , and keep all the original observations from test. I tried this, but only was getting 1 value of s =0?

Highlighted
SAS Super FREQ
Posts: 4,172

## Re: Help with sub setting output data sets

``````z = j(nrow(x), 1, 0);  /* vector with all rows set to 0 */
z[idx] = 1;   /* change these rows to 1 */
x[,'s'] = z;   /* if you want to overwrite original data */``````
☑ This topic is solved.