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

Although the RANDGEN function should work on a vector of parameters, it doesn't give me the intended results. Even after generating a full matrix of parameter p in the code below by repeating the row vector (effectively a N*M matrix), the results clearly suggest that p is being taken as a scalar i.e. only the first element of p=0.1 from the matrix is used for whole matrix. I want to generate random numbers from a different distribution in each column.


%let N = 100;
%let M= 3;
proc iml;
call randseed(123);
x = j(&N,&M);
y = j(&N,&M);
p= j(1, &M);/*specify row vector*/
p= {.1 .2 .5}; /*Probability*/
p_mat = repeat(p, &N); /*Repeat probability parameter for j(&N,&M) matrix*/
call randgen(x, "Binomial",p_mat,&N );
x=x/&N;
mean=mean(x);
max=max(x);
print p,mean,max;
Quit;

 

RESULT:

 

p

0.10.20.5

mean

0.10140.10010.1075

max

0.2
1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Read the documenation for the RANDGEN subroutine, which states:

All parameters must be the same length. You cannot specify a scalar for one parameter and a vector for another. If you pass in parameter vectors that do not satisfy one of the above conditions, then the first element of each parameter is used. 

 

proc iml;
call randseed(123);
x = j(&N,&M);
p = {.1 .2 .5}; /*Probability*/
N = repeat(&N, 1, &M);
call randgen(x, "Binomial", p, N);
x=x/&N;
mean=mean(x);
print p, mean;

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

Read the documenation for the RANDGEN subroutine, which states:

All parameters must be the same length. You cannot specify a scalar for one parameter and a vector for another. If you pass in parameter vectors that do not satisfy one of the above conditions, then the first element of each parameter is used. 

 

proc iml;
call randseed(123);
x = j(&N,&M);
p = {.1 .2 .5}; /*Probability*/
N = repeat(&N, 1, &M);
call randgen(x, "Binomial", p, N);
x=x/&N;
mean=mean(x);
print p, mean;
abbaskashif
Calcite | Level 5

Thanks a million. It worked.

 

 

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
  • 724 views
  • 1 like
  • 2 in conversation