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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

View solution in original post

4 REPLIES 4
Reeza
Super User

You want exact probabilities? 

 

Look into either rand('Bernoulli', probability)

 

or randtable()

Rick_SAS
SAS Super FREQ

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;

:

 

brophymj
Quartz | Level 8

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 

Rick_SAS
SAS Super FREQ

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

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!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

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