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

Hi,

 

I have a 3x3 character array which I want to populate with random occurences of the characters A, B or C. To do this, I created a parrallel 3x3 nuemeric array and withing each (i,j) element of that array I want to randomly generate a number form the uniform distributiom. If that number if within the first third, then the corresponding element of the character array is A etc.

 

Here is the code:

 

proc iml;

schedule = j(3,3," ");
random_numbers=j(3,3,.);
print schedule;
print random_numbers;
  do i = 1 to 3;
    do j = 1 to 3;
        
       call randgen(random_numbers[i,j],"uniform");
	   if random_numbers[i,j] <= 1/3 then schedule[i,j] = "A";
       if 1/3 < random_numbers[i,j]  <= 2/3 then schedule[i,j] = "B";
       if 2/3 <random_numbers[i,j]  <= 1 then schedule[i,j] = "C";


	end;
  end;

print schedule;

quit;

But as a result all of the character array's elements are C...

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Obviously there are 1000 ways to do this, but using your own solution as a starting point, you could simplify by vectorizing your creation of random_numbers setting your loop up backwards as this

 

proc iml;

schedule = j(3,3," ");
random_numbers=j(3,3,.);

call randgen(random_numbers,"uniform");

  do i = 1 to 3;
    do j = 1 to 3;
        
       if random_numbers[i,j] <= 1 then schedule[i,j] = "A";
	    if random_numbers[i,j] <= 2/3 then schedule[i,j] = "B";
       if random_numbers[i,j] <= 1/3 then schedule[i,j] = "C";

	 end;
  end;

print schedule;

quit;

But I agree with @RW9, doing this in a data step is much simpler 🙂

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, looking at your if statements:

       if random_numbers[i,j] <= 1/3 then schedule[i,j] = "A";
       if 1/3 < random_numbers[i,j]  <= 2/3 then schedule[i,j] = "B";
       if 2/3 <random_numbers[i,j]  <= 1 then schedule[i,j] = "C";

Each one of these is evaluated each time - as there is no else statement - and something which is <= 1/3 or 0.33, will also be <= 1.  hence if they do fulfil the first criteria it makes no difference as the third criteria will also be true and hence C overwrites it.

Do note, it would be simpler to do this in a datastep:

data want;
  call streaminit(4563456);
  array schedule{3} $1.;
  array n{3} 8.;
  do i = 1 to 3;
    do j=1 to 3;
      n{j}=ceil(10 * rand("Uniform"));
      if 1 <= n{j} < 4 then schedule{j}="A";
      else if 4 <= n{j} < 8 then schedule{j}="B";
      else schedule{j}="C";
    end;
   output;
  end;
run;

 

ilikesas
Barite | Level 11

Thanks RW9 for the code. Although the original question is about IML, data step is also very useful, especially that this question is actually a subquestion of my post: https://communities.sas.com/t5/Base-SAS-Programming/creating-worker-schedules/m-p/325852#M72509 where I want to create a random work schedule for employees given constraints. The first step that I needed is to generate a random matrix/array, and then optimize it according to the constraints 

PeterClemmensen
Tourmaline | Level 20

Obviously there are 1000 ways to do this, but using your own solution as a starting point, you could simplify by vectorizing your creation of random_numbers setting your loop up backwards as this

 

proc iml;

schedule = j(3,3," ");
random_numbers=j(3,3,.);

call randgen(random_numbers,"uniform");

  do i = 1 to 3;
    do j = 1 to 3;
        
       if random_numbers[i,j] <= 1 then schedule[i,j] = "A";
	    if random_numbers[i,j] <= 2/3 then schedule[i,j] = "B";
       if random_numbers[i,j] <= 1/3 then schedule[i,j] = "C";

	 end;
  end;

print schedule;

quit;

But I agree with @RW9, doing this in a data step is much simpler 🙂

IanWakeling
Barite | Level 11

There are several problems with your code.

 

The RANDGEN call should be used to completely fill a matrix with random numbers in one go.  So instead of attempting to write the numbers one at a time, you should write :

 

call randgen(random_numbers,"uniform");

somewhere before the two do loops.  Also syntax of the form

 

if 2/3 <  x  <= 1 then . . .

is not operating the same wasy that it might in a data step.   IML first evaluates  2/3 < x  as either true(1) or false(0), and since both alternatives are less than or equal to 1, the whole statement is always true.

 

In IML there very often other more efficient ways of getting what you want without having to write loops. For example the SAMPLE function will give you what you want.

 

 call randseed(123);
 x = sample('A':'C', 9, 'Replace', {1 1 1}/3);
 y = shape(x,3,3);
 print y;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1840 views
  • 5 likes
  • 4 in conversation