<?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: using rand function to generate random numbers based on probabilities in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329603#M271809</link>
    <description>&lt;P&gt;If I may offer some friendly advice, I suggest&lt;/P&gt;
&lt;P&gt;1. Talk to your advisor/professor. &amp;nbsp;He/She wants you succeed.&lt;/P&gt;
&lt;P&gt;2. Consider&amp;nbsp;changing to the model I proposed. In that model, you would count how many times each ball has EVER appeared (regardless of whether it was the first, second, ... or fifth ball). &amp;nbsp;That is a standard probability model in which the probability of drawing each ball is constant and the draws are independent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you attempt the simpler project, it will still be challenging and you will still learn a lot about SAS programming and simulation. However, the simpler problem will be more tractable for someone with your level of experience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck!&lt;/P&gt;</description>
    <pubDate>Thu, 02 Feb 2017 21:34:51 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2017-02-02T21:34:51Z</dc:date>
    <item>
      <title>using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329514#M271797</link>
      <description>&lt;P&gt;I am new to sas programming and am having difficulty trying to perform a certain function. &amp;nbsp;I want to generate 4 random numbers from a given range, without replacement, based on given probabilities that each number will be chosen, and I want to perform 2000 repetitions of this. &amp;nbsp;I have been searching trying to find a way to do this, however everywhere just says to put rand("normal") or rand("uniform") but this will not perform my intended task. &amp;nbsp;I also know the theta and sigma for each of the 4 numbers based on 1800 actual past observations. &amp;nbsp;Any help on how I might perform this would be great.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;p.s. I am using sas university edition&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 17:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329514#M271797</guid>
      <dc:creator>eubankm</dc:creator>
      <dc:date>2017-02-02T17:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329525#M271798</link>
      <description>&lt;P&gt;What kind of range you do need, provide some example.&lt;/P&gt;
&lt;P&gt;What is your theta supposed to represent from which distribuion? if from a Normal distribution you can specify the parameter&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x=rand('NORMAL',theta,sigma); if theta is your mean and sigma the standard deviation that you want have your sample represented from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is also rand('TABLE'). You provide a list of probabilities.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x=rand('TABLE', 0.1,0.2,0.5,0.2); would returen a 1 with probability .1, a 2 with probability .2, a 3 with probability .5 or a 4 with probability .2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;p= 1/6;&lt;/P&gt;
&lt;P&gt;x=rand('TABLE',p,p,p,p,p,p); does a good job of simulating a 6-sided die.&lt;/P&gt;
&lt;P&gt;There are multiple ways to map the 1,2,3, to other values if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need result ranges sometimes you have to recalculate but specific approaches may depend on what you are attempting.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 18:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329525#M271798</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-02-02T18:33:05Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329535#M271799</link>
      <description>Thank you that is helpful. The data set is normally distributed, however the distribution is skewed so the probability of each number being chosen is not equal, therefore the 6-sided die example would not work.&lt;BR /&gt;&lt;BR /&gt;I have a table created with the range of numbers available to be chosen for the first number listed out in one column and the probability of each of those numbers being chosen in a 2nd column. I have done this for each of the 4 drawings. If I used rand('table', 0.1,0.2,0.5,0.2) for example but the numbers available starts with 15 and goes to 40, would this return 15 with a probability of 0.1 etc.?&lt;BR /&gt;&lt;BR /&gt;I feel like rand('Normal',theta,sigma) would not work for this since the distribution is skewed and not perfectly normal, but please correct me if I'm wrong.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Thu, 02 Feb 2017 19:09:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329535#M271799</guid>
      <dc:creator>eubankm</dc:creator>
      <dc:date>2017-02-02T19:09:17Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329560#M271800</link>
      <description>&lt;P&gt;Are you talking about a mixture of normal distributions? In a mixture, the parameters are chosen with a specified probability, then a random value is drawn from the appropriate normal&amp;nbsp;distribution. &amp;nbsp;See &lt;A href="http://blogs.sas.com/content/iml/2011/09/21/generate-a-random-sample-from-a-mixture-distribution.html" target="_self"&gt;"Generate a random sample from a mixture distribution"&lt;/A&gt;&amp;nbsp;for a discussion and SAS code.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 20:23:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329560#M271800</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-02T20:23:16Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329568#M271801</link>
      <description>I am doing research for one of my statistics classes regarding the lottery and underlying patterns among the random drawing of the numbers. I have analyzed the winning numbers of the last 1800 drawings and have found the mean number drawn for each ball as well as the stdev. I then found the probability that each number is drawn for each ball (odds of a 1 being drawn for ball 1 and so on). Now I am trying to randomly generate 5 numbers based on those probabilities to see if a more clear pattern emerges.</description>
      <pubDate>Thu, 02 Feb 2017 20:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329568#M271801</guid>
      <dc:creator>eubankm</dc:creator>
      <dc:date>2017-02-02T20:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329575#M271802</link>
      <description>&lt;P&gt;If the i_th ball was drawn k_i times, then the empirical probability for &amp;nbsp;is&amp;nbsp;p_i = k_i / (5*1800).&lt;/P&gt;
&lt;P&gt;You should create a SAS data set that has two coloumns: the ball number and the empirical probability.&lt;/P&gt;
&lt;P&gt;You then want to draw a sample (without replacement) of size 5 with those (unequal) probabilities.&lt;/P&gt;
&lt;P&gt;See the article &lt;A href="http://blogs.sas.com/content/iml/2016/02/15/essential-sampling-methods.html" target="_self"&gt;"Four essential sampling methods in SAS"&lt;/A&gt;&amp;nbsp;which gives the syntax for using PROC SURVEYSELECT or PROC&amp;nbsp;IML to sample according to this scheme. See the upper right corner of the table in the article for the syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 20:53:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329575#M271802</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-02T20:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329585#M271803</link>
      <description>That seems like more what I am trying to do. However, the probabilities that each ball number is drawn is unequal depending on the ball that is being drawn. Ex. Ball 1 has a 9 percent chance of being a 1 and a 0 percent chance of being a 45, and ball 5 has a 0 percent chance of being a 1 and a 12 percent chance of being a 45. This is the pattern that I am studying and want to replicate in a simulation.&lt;BR /&gt;&lt;BR /&gt;This is what is giving me a hard time. I have to break my original data set into subsamples based on which ball is being drawn to get the probabilities correct. Then I want to draw a sample of 5 balls, the first being based on the probabilities I have found for ball 1, the second being based on the probabilities of ball 2 etc.&lt;BR /&gt;&lt;BR /&gt;I am sorry I feel like an idiot for having this hard of a time understanding this.</description>
      <pubDate>Thu, 02 Feb 2017 21:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329585#M271803</guid>
      <dc:creator>eubankm</dc:creator>
      <dc:date>2017-02-02T21:03:02Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329588#M271804</link>
      <description>&lt;OL&gt;
&lt;LI&gt;Are you an undergraduate&amp;nbsp;or graduate&amp;nbsp;student?&lt;/LI&gt;
&lt;LI&gt;How experienced are you with SAS DATA step programming?&lt;/LI&gt;
&lt;LI&gt;How experienced are you at SAS/IML (PROC IML) programming?&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Thu, 02 Feb 2017 21:08:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329588#M271804</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-02T21:08:52Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329589#M271805</link>
      <description>1. undergraduate&lt;BR /&gt;2. Beginner&lt;BR /&gt;3. I have only seen SAS/IML in your blog</description>
      <pubDate>Thu, 02 Feb 2017 21:10:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329589#M271805</guid>
      <dc:creator>eubankm</dc:creator>
      <dc:date>2017-02-02T21:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329594#M271806</link>
      <description>&lt;P&gt;4. When is the project due?&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 21:22:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329594#M271806</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-02T21:22:03Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329599#M271807</link>
      <description>At the end of the Semester, late April. I have purchased the SAS Programming by Example book to help me learn more but I am afraid I will not learn what I need to complete this before it is due.</description>
      <pubDate>Thu, 02 Feb 2017 21:28:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329599#M271807</guid>
      <dc:creator>eubankm</dc:creator>
      <dc:date>2017-02-02T21:28:24Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329602#M271808</link>
      <description>&lt;P&gt;How many records do you have from the lottery where the rules did not change? I am thinking of the PowerBall where they have increased the numbers of the balls in both the main numbers and in the Power ball set? Since the total experience of the results is ever so much smaller than the 69 Choose 5 current possibilities I would be very surprised if many of the individual numbers have result selection rates near the 1/69.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is likely to be an interesting excercise.&lt;/P&gt;
&lt;P&gt;I think the rule you state here "1 has a 9 percent chance of being a 1 and a 0 percent chance of being a 45" is because you are examing the ordered result reported in summaries.&lt;/P&gt;
&lt;P&gt;If the order of balls drawn in a lottery like the PowerBall is in order as seen on TV , 23,7, 18,2,53 the summary reported in the data I have would be 2, 7, 18, 23, 53. So the 45 ball has a very small opportunity to be reported in the first postion of the ordered tuple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the question is are you concerned with combinations or permutations (without and with order)? The process you describe seems to describe a process that is somewhat permutation but using the probability of appearance in a combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest to start with a subset problem such as 10 balls and picking 2 where you can look at all of the possibilities and see results easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 21:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329602#M271808</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-02-02T21:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329603#M271809</link>
      <description>&lt;P&gt;If I may offer some friendly advice, I suggest&lt;/P&gt;
&lt;P&gt;1. Talk to your advisor/professor. &amp;nbsp;He/She wants you succeed.&lt;/P&gt;
&lt;P&gt;2. Consider&amp;nbsp;changing to the model I proposed. In that model, you would count how many times each ball has EVER appeared (regardless of whether it was the first, second, ... or fifth ball). &amp;nbsp;That is a standard probability model in which the probability of drawing each ball is constant and the draws are independent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you attempt the simpler project, it will still be challenging and you will still learn a lot about SAS programming and simulation. However, the simpler problem will be more tractable for someone with your level of experience.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck!&lt;/P&gt;</description>
      <pubDate>Thu, 02 Feb 2017 21:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329603#M271809</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-02-02T21:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329604#M271810</link>
      <description>Thanks, I think what you suggested along with what ballardw has stated below will probably fix my problem. I was viewing summaries of winning numbers off of lottery website which appear to put numbers in order from smallest to largest which is why I was getting so confused. I will try what you told me about PROC SURVEYSELECT and PROC IML from your blog.&lt;BR /&gt;&lt;BR /&gt;Thank you a lot for the help.</description>
      <pubDate>Thu, 02 Feb 2017 21:38:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329604#M271810</guid>
      <dc:creator>eubankm</dc:creator>
      <dc:date>2017-02-02T21:38:03Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329606#M271811</link>
      <description>I believe this fixes my problem. I was going by the summary which orders the numbers from smallest to largest which is what was causing my confusion.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Thu, 02 Feb 2017 21:39:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329606#M271811</guid>
      <dc:creator>eubankm</dc:creator>
      <dc:date>2017-02-02T21:39:45Z</dc:date>
    </item>
    <item>
      <title>Re: using rand function to generate random numbers based on probabilities</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329654#M271812</link>
      <description>&lt;P&gt;Here are two pieces for looking at a reduced problem similar to the lottery.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/* here is a quick way to simulate the possible universe considering
   order using balls with 1 to 6*/
data allposs;
   do ball1= 1 to 6;
      do ball2 = 1 to 6;
         if ball1 ne ball2 then output;
      end;
   end;
run;
/* for your other summary data exercise would be change these
results to the ordered sequence */

/* you will note immediately that this is 2 6-sided dice with
   the doubles removed
*/
/* simulating outcomes from above with a lottery*/
data sim;
   p1= 1/6;  /*probability of each number theoretically is 1/6 if fair for first ball draw*/
   array p2 {6} ; /* to hold probabilities for the second draw. Arrays are a short cut way
                  of referencing many values using and indexing value to tell which
                 one if interest at the time. This statement creates variable
                 p21 p22 p23 p24 p25 p26
                 */
   /* the 5 below says to conduct 5 trials, increase as interested*/
   do trial=1 to 5;
      /* select first draw*/
      ball1 = rand('TABLE',p1,p1,p1,p1,p1,p1);
      /* set the probability of drawing any ball except
         that already picked, since we know only one ball is picked
         we know the prob. Could exend to 1/(n - number of balls already selected
      */
      do i= 1 to dim(p2);
         if i ne ball1 then p2[i]=1/5;
         else p2[i]=0;
      end;
      /* select the second ball*/
      ball2 = rand('TABLE',of p2(*));
      output;
   end;
   /* remove the unwanted counter and the probability values*/
   drop i p:;
run;


&lt;/PRE&gt;
&lt;P&gt;That second data step could be extended with a bit more loop nesting and holding which ball to select in an array. The probability array, to use this way, would need to have one position for each possible ball so the array p2 {6} would change to {69} for current powerball.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Feb 2017 00:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-rand-function-to-generate-random-numbers-based-on/m-p/329654#M271812</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-02-03T00:30:06Z</dc:date>
    </item>
  </channel>
</rss>

