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

I have the following simulation code to produce 1000 sets of data similar to "work.final" where the independent variables do not change in all the 1000 sets.

 

But I would like to add another condition. For each of the 1000 datasets, the frequency of the value 10, which is a simulated value of y should be 20%, the same way...the frequency of value 15 should be 10%.

 

%let NumSamples = 1000; 
data s_new;
call streaminit(6578);
set work.final; 
do SampleID=1 to &NumSamples; 
   mu = exp(1.5360 + 0.0140*x1 - 0.1016*x2 + 0.6216*x3);
   k = 1/0.5345;
   p = 1/(1+0.5345*mu);
   y = rand('negbinomial', p, k);
   output;
end;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Do you want those frequencies to be exactly 20 and 10% or will something like this following do?

 

%let NumSamples = 1000; 
data s_new;
call streaminit(6578);
do SampleID=1 to &NumSamples; 
   mu = exp(1.5360 + 0.0140*x1 - 0.1016*x2 + 0.6216*x3);
   k = 1/0.5345;
   p = 1/(1+0.5345*mu);
   y = rand('negbinomial', p, k);
   a=rand('Uniform');
   if a<0.2 then y=10;
   else if 0.2<=a<=0.3 then y=15;
end;
run;

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Do you want those frequencies to be exactly 20 and 10% or will something like this following do?

 

%let NumSamples = 1000; 
data s_new;
call streaminit(6578);
do SampleID=1 to &NumSamples; 
   mu = exp(1.5360 + 0.0140*x1 - 0.1016*x2 + 0.6216*x3);
   k = 1/0.5345;
   p = 1/(1+0.5345*mu);
   y = rand('negbinomial', p, k);
   a=rand('Uniform');
   if a<0.2 then y=10;
   else if 0.2<=a<=0.3 then y=15;
end;
run;
john1111
Obsidian | Level 7

This would not work with the model I'm using on the simulated data. I previously tried rounding off some values of y (as shown below) to achieve the percentage...but I noticed that it was proving very difficult to get the exact percentage. I would really appreciate any incite that would get me the exact frequency of 10 and 15 to 20% and 10% respectively.

 

if (5 <= y <= 12) & rand("Bernoulli", 0.2) then
      y10 = round(y, 10);

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 639 views
  • 0 likes
  • 2 in conversation