<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: generate random characters in an array in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325920#M3239</link>
    <description>&lt;P&gt;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&amp;nbsp;random_numbers setting your loop up backwards as this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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] &amp;lt;= 1 then schedule[i,j] = "A";
	    if random_numbers[i,j] &amp;lt;= 2/3 then schedule[i,j] = "B";
       if random_numbers[i,j] &amp;lt;= 1/3 then schedule[i,j] = "C";

	 end;
  end;

print schedule;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;, doing this in a data step is much simpler &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Jan 2017 09:51:34 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2017-01-19T09:51:34Z</dc:date>
    <item>
      <title>generate random characters in an array</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325877#M3236</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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] &amp;lt;= 1/3 then schedule[i,j] = "A";
       if 1/3 &amp;lt; random_numbers[i,j]  &amp;lt;= 2/3 then schedule[i,j] = "B";
       if 2/3 &amp;lt;random_numbers[i,j]  &amp;lt;= 1 then schedule[i,j] = "C";


	end;
  end;

print schedule;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But as a result all of the character array's elements are C...&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 05:26:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325877#M3236</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-19T05:26:52Z</dc:date>
    </item>
    <item>
      <title>Re: generate random characters in an array</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325916#M3238</link>
      <description>&lt;P&gt;Well, looking at your if statements:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;       if&lt;/SPAN&gt; random_numbers&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;j&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;lt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; schedule&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;j&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"A"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
       &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;lt;&lt;/SPAN&gt; random_numbers&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;j&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;  &lt;SPAN class="token operator"&gt;&amp;lt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; schedule&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;j&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"B"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
       &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;lt;&lt;/SPAN&gt;random_numbers&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;j&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt;  &lt;SPAN class="token operator"&gt;&amp;lt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; schedule&lt;SPAN class="token punctuation"&gt;[&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;j&lt;SPAN class="token punctuation"&gt;]&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;"C"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Each one of these is evaluated each time - as there is no else statement - and something which is &amp;lt;= 1/3 or 0.33, will also be &amp;lt;= 1. &amp;nbsp;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.&lt;/P&gt;
&lt;P&gt;Do note, it would be simpler to do this in a datastep:&lt;/P&gt;
&lt;PRE&gt;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 &amp;lt;= n{j} &amp;lt; 4 then schedule{j}="A";
      else if 4 &amp;lt;= n{j} &amp;lt; 8 then schedule{j}="B";
      else schedule{j}="C";
    end;
   output;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 09:24:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325916#M3238</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-01-19T09:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: generate random characters in an array</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325920#M3239</link>
      <description>&lt;P&gt;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&amp;nbsp;random_numbers setting your loop up backwards as this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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] &amp;lt;= 1 then schedule[i,j] = "A";
	    if random_numbers[i,j] &amp;lt;= 2/3 then schedule[i,j] = "B";
       if random_numbers[i,j] &amp;lt;= 1/3 then schedule[i,j] = "C";

	 end;
  end;

print schedule;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;, doing this in a data step is much simpler &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 09:51:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325920#M3239</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-01-19T09:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: generate random characters in an array</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325925#M3240</link>
      <description>&lt;P&gt;There are several problems with your code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The RANDGEN call should be used to&amp;nbsp;completely fill a matrix with random numbers in one go.&amp;nbsp;&amp;nbsp;So instead of attempting to write the numbers one at a time, you should&amp;nbsp;write :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call randgen(random_numbers,"uniform");&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;somewhere before the two do&amp;nbsp;loops.&amp;nbsp; Also&amp;nbsp;syntax of the form &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 2/3 &amp;lt;  x  &amp;lt;= 1 then . . .&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN class="token keyword"&gt;is not operating the same wasy that it might in a data step.&amp;nbsp;&amp;nbsp; IML first evaluates &amp;nbsp;2/3 &amp;lt; x&amp;nbsp; as either&amp;nbsp;true(1) or false(0), and since both alternatives are less than or equal to 1, the whole statement is always true.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In IML there&amp;nbsp;very often other more efficient ways of getting what you want without having to write loops.&amp;nbsp;For example the SAMPLE function will give you what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; call randseed(123);
 x = sample('A':'C', 9, 'Replace', {1 1 1}/3);
 y = shape(x,3,3);
 print y;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 10:32:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/325925#M3240</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2017-01-19T10:32:43Z</dc:date>
    </item>
    <item>
      <title>Re: generate random characters in an array</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/326153#M3243</link>
      <description>&lt;P&gt;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: &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/creating-worker-schedules/m-p/325852#M72509" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/creating-worker-schedules/m-p/325852#M72509&lt;/A&gt; 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&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jan 2017 01:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/generate-random-characters-in-an-array/m-p/326153#M3243</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-20T01:14:15Z</dc:date>
    </item>
  </channel>
</rss>

