BookmarkSubscribeRSS Feed
maureen
Calcite | Level 5

Hello

I want to generate a random sample from Binomial distribution (it will be included inside a loop) and another one from Poisson distribution.

How can I do this?

Thanks for your help

2 REPLIES 2
IanWakeling
Barite | Level 11

In SAS/IML there are essentially two ways.  The random sample can be generated one at a time with the usual Base SAS functions.

  call streaminit(1234);

  do i = 1 to 10;

    b = rand('binomial', 0.05, 20);  /* p=0.05  n=20 */

    p = rand('poisson', 5);          /* lambda=5 */

    print b p;

  end;

However the RANDGEN call is usually the best option.  In general it is more efficient not to use a loop and generate the random numbers all at the same time.

  b = j(10, 1);

  p = j(10, 1);

  call randseed(4321);

  call randgen(b, 'binomial', 0.05, 20);

  call randgen(p, 'poisson', 5);

  print b p;

Rick_SAS
SAS Super FREQ

Ian's answer is correct. I will add that if you are only going to simulate ONE sample (that is, not inside a loop), you can use the newer RANDFUN function in SAS/IML 12.3: Convenient functions vs. efficient subroutines: Your choice - The DO Loop

Since you mention the binomial and Poisson distribution together, I wonder whether you are forming a compound distribution? If so, see this article: A different way to interpret the negative binomial distribution - The DO Loop

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
  • 2 replies
  • 1107 views
  • 0 likes
  • 3 in conversation