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

Hi all,

 

I am struggling to generate a matrix, let's call it "forecast", such that each element forecast[i,j] is drawn from a different Bernoulli distribution with probability corresponding to another matrix element, say bernoulli_p[i,j]. The log displays no errors, but when I print the forecast matrix, it only has blank values. 

 

 

 

Here is the relevant part of the code I have been using:

 

do i=1 to n_farms;
do j=7 to n_months;
if avc[i,j]>0 then stoch_avc[i,j]=(avc[i,j]+u[i,j]);

if avc[i,j]=0 then exit_z[i,j]=.;
exit_z[i,j]=-2.929548+FALL[i,j]*(-0.027052)+Winter[i,j]*0.61746+spring[i,j]*(-0.075117)+
PRICE[i,j-1]*(-0.078526)+PRICE[i,j-2]*(-0.004963)+PRICE[i,j-3]*0.117736+PRICE[i,j-4]*(-0.042244)+PRICE[i,j-5]*0.001874+PRICE[i,j-6]*(-0.069283)+
AVC[i,j-1]*(-0.552019)+AVC[i,j-2]*(-0.017645)+AVC[i,j-3]*0.615848+AVC[i,j-4]*(-0.173533)+AVC[i,j-5]*0.174981+AVC[i,j-6]*0.043233;

if exit_z[i,j]^=. then bernoulli_p[i,j]=cdf('normal',exit_z[i,j]);
if exit_z[i,j]=. then bernoulli_p[i,j]=.;

call randseed(datetime());
forecast=j(n_farms,n_months,.);
call randgen(forecast[i,j],"bernoulli",bernoulli_p[i,j]);

end;
end;

 

 

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
IanWakeling
Barite | Level 11

I think there are three problems that I can see with your code.

 

  • Did you intend the second loop to run from 7?  If n_months is less than 7 then the second do loop will not iterate at all.
  • As the statement 'forecast=j(n_farms,n_months,.)' is inside the nested loops, it will set all the elements to missing on every iteration, so it needs to be moved outside of the loops.
  • The first parameter in CALL RANDGEN should be the name of matrix which is to be entirely filled with random numbers in one call.  It will not work when you try to set individual elements.  Make a single call to RANDGEN at the end of the code.

 

Putting all of this together the code will look something like this:

do i=1 to n_farms;
  do j=1 to n_months;
    < code to set each element of the matrix bernoulli_p here >;
  end;
end;

call randseed(1729);
forecast=j(n_farms,n_months,.);
call randgen(forecast,'Bernoulli',bernoulli_p);

 

View solution in original post

3 REPLIES 3
Ksharp
Super User

Can you post the data about matrix forecast[i,j] and bernoulli_p[i,j] ?  and the output matrix you want ?

IanWakeling
Barite | Level 11

I think there are three problems that I can see with your code.

 

  • Did you intend the second loop to run from 7?  If n_months is less than 7 then the second do loop will not iterate at all.
  • As the statement 'forecast=j(n_farms,n_months,.)' is inside the nested loops, it will set all the elements to missing on every iteration, so it needs to be moved outside of the loops.
  • The first parameter in CALL RANDGEN should be the name of matrix which is to be entirely filled with random numbers in one call.  It will not work when you try to set individual elements.  Make a single call to RANDGEN at the end of the code.

 

Putting all of this together the code will look something like this:

do i=1 to n_farms;
  do j=1 to n_months;
    < code to set each element of the matrix bernoulli_p here >;
  end;
end;

call randseed(1729);
forecast=j(n_farms,n_months,.);
call randgen(forecast,'Bernoulli',bernoulli_p);

 

Rick_SAS
SAS Super FREQ

For more information about Ian's excellent suggestion, see the article "Generate binary outcomes with varying probability."

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