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

## Is it possible to use the LOC function in SAS/IML within a loop

Hi everyone,

I have my data saved in an excel file, which I have imported into SAS/IML and read the data into columns.

I have 2 columns as shown in the file attached:

Example:

data: 10 12 15 20 -50 50 30 15 12 16 16 20 -20 70 40 18 (16 observations)

blocks: 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 (2 block 1 and 2 with 8 observations in each block).

I would like to generate separate columns for each set of data within a block. i.e I want to have the first 8 observations in an array maybe called X1, then the remaining 8 observations to be saved in another array called X2.

For data with more than 2 blocks say n, I should have X1, X2,....., Xn arrays.

Thanks,

DJ

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: Is it possible to use the LOC function in SAS/IML within a loop

Are you going to access all the arrays simultaneously, or do you want to process them one by one?

If you want to process them one-by-one, then this is BY-group processing, which you can accomplish in SAS/IML by using the UNIQUE-LOC technique:

``````proc iml;
data={ 10 12 15 20 -50 50 30 15 12 16 16 20 -20 70 40 18}`;
blocks={ 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 };

u = unique(blocks);         /* 2. Unique values (levels) of categorical variable. */
s = j(1, ncol(u));          /* 3. Allocate vector to hold results */
do i = 1 to ncol(u);        /* 4. For each level... */
idx = loc(blocks=u[i]);  /* 5. Find observations in level */
X = data[idx];
/* now process these observations */
*print (sum(x));
end;

``````

If you think you want to use separate matrices, X1, X2, ..., I advise you to rethink the problem. Although it is possible to do what you describe, in my experience (20+ years of IML programming), it is usually easier to deal with one matrix (rows of DATA, indexed by BLOCK) than to create a lot of matrices with separate names. In the recent versions of IML, you can use lists for this, but I recall from one of your previous posts that you are using an old version of SAS/IML that does not support lists.

Without lists, you would need to use programming tricks that are better avoided by someone who is new to the language.

Since you have posted multiple posts on the topic of designing experiments, I feel compelled to point out that SAS supports pre-written procedures to help you design experiments. For some examples, see Bilenas (2005) or Lawson's book (2010).

In addition, many people find that JMP software is an excellent tool for the design and analysis of experiments.

2 REPLIES 2
Super User

## Re: Is it possible to use the LOC function in SAS/IML within a loop

Better post it at IML forum . @Rick_SAS  is there.

``````data have;
set sashelp.class;
if mod(_n_,5)=1 then group+1;
keep group weight height;
run;

proc iml ;
use have;
read all var {weight height} into x;
read all var {group};
close;

do i=1 to max(group);
temp='x'+left(char(i));
call valset(temp,x[loc(group=i),]);
print (value(temp))[label=('x'+left(char(i)))] ;
end;

quit;``````
SAS Super FREQ

## Re: Is it possible to use the LOC function in SAS/IML within a loop

Are you going to access all the arrays simultaneously, or do you want to process them one by one?

If you want to process them one-by-one, then this is BY-group processing, which you can accomplish in SAS/IML by using the UNIQUE-LOC technique:

``````proc iml;
data={ 10 12 15 20 -50 50 30 15 12 16 16 20 -20 70 40 18}`;
blocks={ 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 };

u = unique(blocks);         /* 2. Unique values (levels) of categorical variable. */
s = j(1, ncol(u));          /* 3. Allocate vector to hold results */
do i = 1 to ncol(u);        /* 4. For each level... */
idx = loc(blocks=u[i]);  /* 5. Find observations in level */
X = data[idx];
/* now process these observations */
*print (sum(x));
end;

``````

If you think you want to use separate matrices, X1, X2, ..., I advise you to rethink the problem. Although it is possible to do what you describe, in my experience (20+ years of IML programming), it is usually easier to deal with one matrix (rows of DATA, indexed by BLOCK) than to create a lot of matrices with separate names. In the recent versions of IML, you can use lists for this, but I recall from one of your previous posts that you are using an old version of SAS/IML that does not support lists.

Without lists, you would need to use programming tricks that are better avoided by someone who is new to the language.

Since you have posted multiple posts on the topic of designing experiments, I feel compelled to point out that SAS supports pre-written procedures to help you design experiments. For some examples, see Bilenas (2005) or Lawson's book (2010).

In addition, many people find that JMP software is an excellent tool for the design and analysis of experiments.

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