BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Vovik
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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."

View solution in original post

4 REPLIES 4
Vovik
Calcite | Level 5

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;

Rick_SAS
SAS Super FREQ

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."

Vovik
Calcite | Level 5

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?

Rick_SAS
SAS Super FREQ
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 */

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 4 replies
  • 1389 views
  • 2 likes
  • 2 in conversation