## how to rearrange data in a specific order in SAS/IML?

Hi everyone,

I have been given a code to generate a matrix of 0's and 1's

``````proc iml;
start AllZeroOneComb(n);
if n=1 then return ( {0,1} );
cmd = "m = expandgrid(";
do i = 1 to n-1;
cmd = cmd + "0:1, ";
end;
cmd = cmd + "0:1);";
*print cmd;
CALL EXECUTE(cmd);
return (m);
finish;

do n = 1 to 3;
m = AllZeroOneComb(n);
print m;
end;``````

this program output the following:

in the case of n=3,

0 0 0

1 0 0

0 1 0

1 1 0

0 0 1

1 0 1

0 1 1

1 1 1

I would like my data to appear in the following order instead;

0 0 0

1 0 0

0 1 0

0 0 1

1 1 0

1 0 1

0 1 1

1 1 1

Does anyone know how to implement that?

Thanks,

DJ

6 REPLIES 6

## Re: how to rearrange data in a specific order in SAS/IML?

I'd like to take a step back ... why is all of this being done in PROC IML? Each of your examples that you have been working on in the last week or so could be much easier done without using PROC IML, which is (in a manner of speaking) an entirely different language. Everything you have done in PROC IML, you are working very hard at, and there are much simpler ways of doing these things.

Also, if you are working on a factorial experiment, what difference does it make what order these things are in?

--
Paige Miller

## Re: how to rearrange data in a specific order in SAS/IML?

I have been given the task of finding the ANOVA table for a factorial design with 2 levels for each factor. I know that using PROC IML for this purpose is not convenient at all however it is not my choice.

I have been given a number of data sets with different number of factors in each data set the least is two and maximum is 4.

I can easily do the calculations for the data with one block only, however when the number of blocks exceeds 1 it gets quite complicated.

But now with the help I got in the community from such nice and helpful people like you I am almost done with program.

My main issue now is to construct the ANOVA table!! because I have used yates's method to do the calculations I had to put my main factors and interactions between the factors in a specific order:

1

A

B

AB

C

AC

BC

ABC

and so on. For the ANOVA table I need them to be in the following order:

A

B

C

AB

AC

BC

ABC

Thats why I asked whether there is a possibility to change the order using the same code.  it seems impossible to do that usiing the same code Do you have any other suggestions?

Thanks,

DJ

## Re: how to rearrange data in a specific order in SAS/IML?

For n=3, it looks like you want to swap the 3rd and 4th rows. Anticipating that you next question might be "how do I do it for n=4 and n=5, let me direct you to a resource for reordering the rows of a matrix according to any measure that you want to use

In general, create a vector of ranks, then use row subscripts to permute the rows according to the given ranks:

``````proc iml;
m = {
0 0 0,
1 0 0,
0 1 0,
1 1 0,
0 0 1,
1 0 1,
0 1 1,
1 1 1
};

rank = {1 2 3 5 4 6 7 8};
z = m[rank,];     /* rearrange the rows */
print z;
``````

Create a similar vector of ranks for n=4 and n=5.

## Re: how to rearrange data in a specific order in SAS/IML?

It does work however I wanted to attach other columns to the matrix of o's and 1's and I want to sort them depending on the changes applied to the matrix of 0's and 1's!!

Thanks

DJ

## Re: how to rearrange data in a specific order in SAS/IML?

Great! I feel confident that you can solve the related problem by using the many tips, techniques, and resources that we've offered. Give it a go. If you still have questions after you have made several programming attempts, you can post the program that doesn't completely work and people will suggest ways to revise/improve it.  Ksharp
Super User

## Re: how to rearrange data in a specific order in SAS/IML?

``````proc iml;
x=expandgrid(0:1,0:1,0:1);
s=x[,+]||x;
call sortndx(idx,s,1:4,2:4);
want=x[idx,];
print x,want;
quit;
``````
From The DO Loop