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

I'm trying to use the ExpandGrid function on several vertical vectors without knowing the number of vectors when the code is written, The example below shows a use case that creates 4 matrices (from A to D) each consisting of the cartesian product of two vectors. However, at run time there may be more (or fewer) matrices to create  or vectors in each matrix. I know ExpandGrid requires between 2 and 15 vectors, so I'll make sure these limits are not exceeded.  

How can this be written dynamically?

 

 

proc iml;
Var_1_A = {1, 4, 5, 7};
Var_1_B = {3, 4, 2};
Var_1_C = {10, 11};

Var_2_A = {11, 0, 2};
Var_2_B = {13, 2, 2, 5, 9};
Var_2_C = {10, 11};

/* Hard coded*/
CP_A = ExpangrGrid(Var_1_A, Var_2_A);
CP_B = ExpangrGrid(Var_1_B, Var_2_B);
CP_C = ExpangrGrid(Var_1_C, Var_2_C);
CP_D = ExpangrGrid(Var_1_D, Var_2_D);

quit;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

One way is to use indirect assignment to iterate over the names of the variables that hold the data:

 

proc iml;
X1_1 = {1, 4, 5, 7};
X1_2 = {3, 4, 2};
X1_3 = {10, 11};

X2_1 = {11, 0, 2};
X2_2 = {13, 2, 2, 5, 9};
X2_3 = {10, 11};

/* create array of variable names for X and Y */
Vars1 = "x1_1":"x1_3";
Vars2 = "x2_1":"x2_3";

/* Loop over names of arrays. Use indirect assignment to 
   retreive information into temporary arrays X and Y
   https://blogs.sas.com/content/iml/2011/03/23/indirect-assignment-how-to-create-and-use-matrices-named-x1-x2-xn.html
*/
do i = 1 to ncol(Vars1);
   X = value( Vars1[i] );
	Y = value( Vars2[i] );
   G = expandgrid(X, Y);
	/* do something with G */
	msg = "Grid has " + char(nrow(G),2) + " pairs";
	print i msg;
end;

View solution in original post

8 REPLIES 8
Rick_SAS
SAS Super FREQ

Do you need all grids SIMULTANEOUSLY, or do you just need to loop over all combinations, generate the grid, do something with the grid, and then go on to the next combination?

Rick_SAS
SAS Super FREQ

One way is to use indirect assignment to iterate over the names of the variables that hold the data:

 

proc iml;
X1_1 = {1, 4, 5, 7};
X1_2 = {3, 4, 2};
X1_3 = {10, 11};

X2_1 = {11, 0, 2};
X2_2 = {13, 2, 2, 5, 9};
X2_3 = {10, 11};

/* create array of variable names for X and Y */
Vars1 = "x1_1":"x1_3";
Vars2 = "x2_1":"x2_3";

/* Loop over names of arrays. Use indirect assignment to 
   retreive information into temporary arrays X and Y
   https://blogs.sas.com/content/iml/2011/03/23/indirect-assignment-how-to-create-and-use-matrices-named-x1-x2-xn.html
*/
do i = 1 to ncol(Vars1);
   X = value( Vars1[i] );
	Y = value( Vars2[i] );
   G = expandgrid(X, Y);
	/* do something with G */
	msg = "Grid has " + char(nrow(G),2) + " pairs";
	print i msg;
end;
Ksharp
Super User

If could make two datasets.

 

data x;
input id value1;
cards;
1 1
1 4
1 5
1 7
2 3
2 4
2 2
3 10
3 11
;
run;

data y;
input id value2;
cards;
1 11
1 0
1 2
2 13
2 2
2 2
2 5
2 9
3 10
3 11
;
run;

proc sql;
select x.*,value2
 from x,y
  where x.id=y.id;
quit;
acordes
Rhodochrosite | Level 12
How Do I grow the expandgrid arguments conditionally?
Sometimes I need
Expandgrid(X1_1, X1_2)
And some condition met I want
Expandgrid(X1_1, X1_2, X1_3)
Rick_SAS
SAS Super FREQ
if condition1 then 
   g = Expandgrid(X1_1, X1_2);
else
   g = Expandgrid(X1_1, X1_2, X1_3);
acordes
Rhodochrosite | Level 12
Ok, but is there a way to execute a iml code line thorough a concatenated string or by the use of a macro variable?
Rick_SAS
SAS Super FREQ

Yes. I suggest CALL EXECUTE. You can build up the EXPANDGRID arguments dynamically:

 

proc iml;
x1 = 1:2;
x2 = 1:3;
x3 = -1:1;

condition = 0; /* set =1 for true */
str = "g = expandgrid(x1, x2";
if condition then 
   stmt = str + ", x3);";
else 
   stmt = str + ");";
call execute(stmt);
print g;

 

 

 

acordes
Rhodochrosite | Level 12
Thank you Rick

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 8 replies
  • 1527 views
  • 5 likes
  • 4 in conversation