## Randomly generate an outcome that has a certain probability

Solved
Super Contributor
Posts: 259

# Randomly generate an outcome that has a certain probability

Hi I would like to create a field in my dataset called with two outcomes: "Win" or "Lose"

For each row in my dataset I have a Win_Prob and I would like this outcome field to populate based on the prob. So if the probability is 20% than if I ran the dataset 5 times it would populate "Win" one in 5 times.

Can this be done easily?

Thanks

Accepted Solutions
Solution
‎09-16-2016 08:52 AM
SAS Super FREQ
Posts: 3,839

## Re: Randomly generate an outcome that has a certain probability

Sorry, I should have mentioned that the CALL STREAMINIT call sets a "seed" for the random value stream.  If you want it to produce different values every time you run, you can use zero as a seed:

call streaminit(0);

You can read more about random number streams in the article "Random number streams in SAS: How do they work?"

All Replies
Super User
Posts: 20,731

## Re: Randomly generate an outcome that has a certain probability

You want exact probabilities?

Look into either rand('Bernoulli', probability)

or randtable()

SAS Super FREQ
Posts: 3,839

## Re: Randomly generate an outcome that has a certain probability

Because it is random, you won't get 1 win and 4 losses if you run the program 5 times, just like if you flip a coin twice you don't always see 1 head and 1 tail.  However, ON AVERAGE, you will see the specified probabilities if you repeat the experiment many many many times.

If you want the result of each individual trial, you can use the rand("Bernoulli") function. The following reads the probabilities from a data set and then repeats a Bernoulli trial (basically a coin flip with the given probability) five times:

``````data Prob;
input Win_Prob @@;
datalines;
0.2 0.5 0.9
;

data Wins;
call streaminit(1234);
set Prob;
do trial = 1 to 5;
Win = rand("Bernoulli", Win_Prob);
output;
end;
run;

proc freq data=Wins;
tables Win_Prob * Win / nocum nocol nopercent;
run;``````

If all you care about is the summary statistics (how many wins/losses were observed), you can use the Binomial distribution, which gives you the numbers of wins in N trials. In the following, I set N=50 so that you can see that about 20%, 50% and 90% of the trials are "successes":

``````data AllWins;
call streaminit(1234);
set Prob;
nTrials = 50;
NumWins = rand("Binomial", Win_Prob, nTrials);
run;

proc print data=AllWins;
run;``````

:

Super Contributor
Posts: 259

## Re: Randomly generate an outcome that has a certain probability

Thanks Rick

I tried the first piece of code and it works but when i run it again it the values "Win" field don't change. I thought it would generate different values each time you run the data step.

Thanks

Solution
‎09-16-2016 08:52 AM
SAS Super FREQ
Posts: 3,839

## Re: Randomly generate an outcome that has a certain probability

Sorry, I should have mentioned that the CALL STREAMINIT call sets a "seed" for the random value stream.  If you want it to produce different values every time you run, you can use zero as a seed:

call streaminit(0);

You can read more about random number streams in the article "Random number streams in SAS: How do they work?"

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 4 replies
• 252 views
• 0 likes
• 3 in conversation