turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- Analytics
- /
- Stat Procs
- /
- Randomly generate an outcome that has a certain pr...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-16-2016 07:25 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-16-2016 08:45 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-16-2016 07:42 AM

You want exact probabilities?

Look into either rand('Bernoulli', probability)

or randtable()

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-16-2016 08:06 AM

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;
```

:

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-16-2016 08:40 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-16-2016 08:45 AM

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?"