turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

02-03-2018 09:24 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Vovik

02-03-2018 11:47 AM - edited 02-03-2018 11:50 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Vovik

02-03-2018 09:25 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Vovik

02-03-2018 11:47 AM - edited 02-03-2018 11:50 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Vovik

02-05-2018 02:35 PM

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
## Re: Help with sub setting output data sets

Options

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Vovik

02-05-2018 03:36 PM

```
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 */
```