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

Hi,

 

Suppose I have the following code:

proc iml;
	class_1 ={1 0 2,
		  2 3 2};
	class_2 ={4 2 3,
		  2 6 2};
	div ={1	2 4,
 	      6	8 10};
	new_1 = class_1 / div;
	new_2 = class_2 / div;
	print new_1;
	print new_2;

I want to set up a loop to reduce the code. So something like 

	do i = 1 to 2;
		new_i = class_i / div;
		print new_i;
		end;

What's the proper way to extract the value of i and use it as an actual value?

 

Thank you.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

For your example in which all variables are the same size, I recommend "flattening" the data into row vectors and storing the matrices as rows in a matrix.  For your example, it would look like this:

 

nr = 2;  /* number of rows */
nc = 3;  /* number of cols */
nMatrices = 2;  /* total number of matrices being stored */

class = {
1 0 2  2 3 2,  /* first 2x3 matrix */
4 2 3  2 6 2}; /* second 2x3 matrix */

div ={1 2 4  6	8 10};  
new = class / div;

/* if you need to print or use as matrices, use SHAPE: */
do i = 1 to nMatrices;
   newM = shape(new, nr, nc);
   print newM;
end;

 

Although I don't recommend it, if you insist on using symbols that have numerical suffixes, you can use the VALUE and VALSET functions.

 

do i = 1 to 2;
   cName = "class_" + strip(char(i));
   c = value(cName);    /* get value of class_i */
   new = c / div;
   print new;
   /* if necessary, use VALSET to create new_i */
end;

View solution in original post

1 REPLY 1
Rick_SAS
SAS Super FREQ

For your example in which all variables are the same size, I recommend "flattening" the data into row vectors and storing the matrices as rows in a matrix.  For your example, it would look like this:

 

nr = 2;  /* number of rows */
nc = 3;  /* number of cols */
nMatrices = 2;  /* total number of matrices being stored */

class = {
1 0 2  2 3 2,  /* first 2x3 matrix */
4 2 3  2 6 2}; /* second 2x3 matrix */

div ={1 2 4  6	8 10};  
new = class / div;

/* if you need to print or use as matrices, use SHAPE: */
do i = 1 to nMatrices;
   newM = shape(new, nr, nc);
   print newM;
end;

 

Although I don't recommend it, if you insist on using symbols that have numerical suffixes, you can use the VALUE and VALSET functions.

 

do i = 1 to 2;
   cName = "class_" + strip(char(i));
   c = value(cName);    /* get value of class_i */
   new = c / div;
   print new;
   /* if necessary, use VALSET to create new_i */
end;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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
  • 1 reply
  • 680 views
  • 1 like
  • 2 in conversation