2

I am trying to use call randgen within proc IML to create 10 random numbers that follow a Weibull distribution with certain parameters. Here is the code I am using (obviously there will be more than just one loop but I am just testing right now):

do i = 1 to 1;
    Call randgen(Rands[i,1:Ntimes], 'Weibull', alpha[i], beta[i]);
    print (rands[1,1:Ntimes]);
    print (alpha[i]) (beta[i]);
end;

For this example Ntimes = 10, alpha[i] = 4.5985111, and beta[i] = 131.79508. My issue is that each of the 10 iterations/random numbers comes back as 1. I used the rweibull function in R with the same parameters and got results that made sense so I am thinking it has something to do with SAS or my code rather than an issue with the parameters. Am I using the Randgen call correctly? Does anyone know why the results would be coming out this way?

Joe
  • 62,789
  • 6
  • 49
  • 67
kelchenk
  • 23
  • 2

2 Answers2

0

This works:

proc iml;
 alpha=j(10);
 beta=j(10);
 alpha[1]=4.59;
 beta[1] = 131.8;
 Ntimes=10;
 rands = j(1,10);
 print (rands);
 do i = 1 to 1;
    Call randgen(Rands, 'WEIB', alpha[1],beta[1]);
    print (rands);
 end;
quit;

I don't think you can use Rands[1:Ntimes] that way. I think you would want to assign it to a temporary matrix and then assign that matrix's results to a larger matrix.

IE:

allRands=j(10,10);
do i = 1 to 10;
    Call randgen(Rands, 'WEIB', alpha[1],beta[1]);
    print (rands);
    allRands[i,1:10]=Rands;
end;
print(allRands);
Joe
  • 62,789
  • 6
  • 49
  • 67
  • 1
    To expand on Joe's remarks, never pass a temporary variable as an argument to a routines that is going to return results in that argument. See the article http://blogs.sas.com/content/iml/2013/01/30/temporary-variables/ – Rick Aug 27 '14 at 12:06
0

Actually, unless you are using an ancient version of SAS/IML, you don't need any loops. Since SAS/IML 12.3, the RANDGEN subroutine accepts a vector of parameters. In your case, define a vector for the alpha and beta parameters. Let's say there are 'Nparam' parameters. Then allocate an N x Nparam matrix to hold the results. With a single call to RANDGEN, you can fill the matrix so that the i_th column is a sample of size N from Weibull(alpha[i], beta[i]), as shown in the following example:

 proc iml;
 Nparam = 8; N = 1000;

 alpha= 1:Nparam;  /* assign parameter values */
 beta = 10 + (Nparam:1);
 rands = j(N,Nparam);
 call randgen(rands, 'WEIB', alpha,beta);  /* SAS/IML 12.1 */

 /* DONE. The i_th column is a sample from Weibul(alpha[i], beta[i]) 
    TEST IT: Compute the mean of each sample: */
 mean = mean(rands); std = std(rands);
 print (alpha//beta//mean//std)[r={"alpha" "beta" "mean" "std"}];
 /* TEST IT: Plot the distribution of each sample (SAS/IML 12.3) */  
 title "First param"; call histogram(rands[,1]);
 title "Last param";  call histogram(rands[,Nparam]);
Rick
  • 1,210
  • 6
  • 11