<?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: Randomly Select an Element of an Array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439864#M109786</link>
    <description>&lt;P&gt;The test that I tried with this logic will not populate the random_element column with the array element.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; 
	array _Example{*} $6. TEST01-TEST10 ; 
	r = ceil(dim(_Vendor) * ranuni(12345)); 
	random_element = _Example{r}; 
run; &lt;BR /&gt;&lt;/CODE&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Random_Element.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18793iA9FBDAB1349E76DF/image-size/large?v=v2&amp;amp;px=999" role="button" title="Random_Element.PNG" alt="Random_Element.PNG" /&gt;&lt;/span&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate your help, please bear with me as I am a new SAS user.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Feb 2018 21:27:19 GMT</pubDate>
    <dc:creator>lhsumdalum</dc:creator>
    <dc:date>2018-02-23T21:27:19Z</dc:date>
    <item>
      <title>Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439848#M109777</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have an array of length n, for example :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ARRAY _Example{*} 6$ EXA001: &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to randomly select an element of the array. Is there an easy or straight forward way of achieving this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The larger scope of this issue is that I want to assign a random element of the the array (map the element for example EXA004) to a dataset. Essentially, randomly assign one of the elements of the array to an ID variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any insight into this issue would be much appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 20:36:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439848#M109777</guid>
      <dc:creator>lhsumdalum</dc:creator>
      <dc:date>2018-02-23T20:36:08Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439851#M109778</link>
      <description>&lt;P&gt;Assuming that the syntax for the array statement gets fixed ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array _Example {*} whatever goes here;&lt;/P&gt;
&lt;P&gt;r = ceil( dim(_Example) * ranuni(12345) ) ;&lt;/P&gt;
&lt;P&gt;randomly_selected_element = _Example{r};&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The variable R will be a randomly selected integer, minimum is 1 and maximum is number of elements in the array.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 20:50:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439851#M109778</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-23T20:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439852#M109779</link>
      <description>&lt;P&gt;1. Generate a random uniform variable between 1 and 6. This assumes you're using SAS 9.4 TS1M4+, if you don't, use the approach outlined &lt;A href="https://blogs.sas.com/content/iml/2015/10/05/random-integers-sas.html" target="_self"&gt;here&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rand_int = rand('Integer', 1, 6);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. Use that index in the array to map the value over.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;value = _example(rand_int);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/192948"&gt;@lhsumdalum&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have an array of length n, for example :&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ARRAY _Example{*} 6$ EXA001: &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to randomly select an element of the array. Is there an easy or straight forward way of achieving this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The larger scope of this issue is that I want to assign a random element of the the array (map the element for example EXA004) to a dataset. Essentially, randomly assign one of the elements of the array to an ID variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any insight into this issue would be much appreciated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 20:51:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439852#M109779</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-23T20:51:02Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439855#M109780</link>
      <description>&lt;P&gt;Is it possible to replace the hard-coded upper bound of 6 with the unspecified dimension of the array? To continue your example, as such:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rand_int = rand('Integer', 1, dim(_Example));&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Feb 2018 20:56:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439855#M109780</guid>
      <dc:creator>lhsumdalum</dc:creator>
      <dc:date>2018-02-23T20:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439858#M109783</link>
      <description>&lt;P&gt;I don't know, did it work?&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/192948"&gt;@lhsumdalum&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Is it possible to replace the hard-coded upper bound of 6 with the unspecified dimension of the array? To continue your example, as such:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rand_int = rand('Integer', 1, dim(_Example));&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 21:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439858#M109783</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-23T21:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439864#M109786</link>
      <description>&lt;P&gt;The test that I tried with this logic will not populate the random_element column with the array element.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test; 
	array _Example{*} $6. TEST01-TEST10 ; 
	r = ceil(dim(_Vendor) * ranuni(12345)); 
	random_element = _Example{r}; 
run; &lt;BR /&gt;&lt;/CODE&gt;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Random_Element.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18793iA9FBDAB1349E76DF/image-size/large?v=v2&amp;amp;px=999" role="button" title="Random_Element.PNG" alt="Random_Element.PNG" /&gt;&lt;/span&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate your help, please bear with me as I am a new SAS user.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 21:27:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439864#M109786</guid>
      <dc:creator>lhsumdalum</dc:creator>
      <dc:date>2018-02-23T21:27:19Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439870#M109788</link>
      <description>&lt;P&gt;You don't have a SET statement. Don't you have an input dataset?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 21:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439870#M109788</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-23T21:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439873#M109790</link>
      <description>I do have an input dataset that I want to map the elements of the value column in the above example to. However, I needed to create the array myself. I was planning on using a proc sql to join the elements of the value column to the existing data set.</description>
      <pubDate>Fri, 23 Feb 2018 21:46:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439873#M109790</guid>
      <dc:creator>lhsumdalum</dc:creator>
      <dc:date>2018-02-23T21:46:33Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly Select an Element of an Array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439876#M109792</link>
      <description>&lt;P&gt;You lost me now.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I still suspect you need to add the SET statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;OR if you're creating a new data set from scratch you need a loop with an output statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to 1000; *number of records;
x= .... ;
output;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Feb 2018 21:54:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Randomly-Select-an-Element-of-an-Array/m-p/439876#M109792</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-23T21:54:24Z</dc:date>
    </item>
  </channel>
</rss>

