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!
I think there are three problems that I can see with your 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);
Can you post the data about matrix forecast[i,j] and bernoulli_p[i,j] ? and the output matrix you want ?
I think there are three problems that I can see with your 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);
For more information about Ian's excellent suggestion, see the article "Generate binary outcomes with varying probability."
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.