Statistical programming, matrix languages, and more

Help with sub setting output data sets

Accepted Solution Solved
Reply
Highlighted
New Contributor
Posts: 3
Accepted Solution

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,169

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

View solution in original post


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,169

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?

SAS Super FREQ
Posts: 4,169

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.

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

Discussion stats
  • 4 replies
  • 278 views
  • 2 likes
  • 2 in conversation