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
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?
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
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.
Hi @Rick_SAS
thank you for you reply.
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
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.
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.