Help using Base SAS procedures

Poker Simulation

Reply
Occasional Contributor
Posts: 12

Poker Simulation

Im new to SAS and I am trying to do something fairly simple.  I want to create a deck of playing cards and shuffle that deck.  Then I want to produce 5 card poker hands and find the probabilities of producing certain hands (eg flush, straight flush, etc.)

Super User
Posts: 13,583

Re: Poker Simulation

With or without draw?

Or Texas Hold-em?

Or other version, Dr Pepper perhaps?

PROC Star
Posts: 1,283

Re: Poker Simulation

have you given any thought to what kind of poker is played or do you just want to draw 5 cards eg from a deck and see what hand you have?

Occasional Contributor
Posts: 12

Re: Poker Simulation

I am considering doing probabilities across different games. Perhaps run simulations using 5 card stud rules, 5 card (single draw), and seven card stud.
Super User
Posts: 10,784

Re: Poker Simulation

Calling @Rick_SAS

 

Check RANDPERK() in IML .

Check Rick's blog. there are many blogs about it .

SAS Super FREQ
Posts: 4,243

Re: Poker Simulation

Other references include

Simulate Blackjack: https://communities.sas.com/t5/SAS-IML-File-Exchange/Simulating-a-Blackjack-Game/ta-p/289668

and

Value of Poker Hands:https://communities.sas.com/t5/SAS-Procedures/Allocating-Values-to-Poker-Hands/m-p/146459#M38870

 

My personal view: This project is not particularly simple. If you are just looking for a project to get started in SAS, there are simpler simulations. For example, craps and blackjack have simpler rules. You can simulate craps in about 20-30 lines of SAS code. For a first project, you might even try to simulate a game related to flipping coins. Good luck!

 

 

 

 

Occasional Contributor
Posts: 12

Re: Poker Simulation

In our class, we created a deck and shuffled cards.  Then we did a very simplified poker game where we dealt five cards and kept track of only matching cards(e.g. pair, 3 of a kind and 4 of a kind).  I understand programming rules for the higher ranked hands (e.g. straight, flush, etc.) is a little trickier.  With that being said, I may forgo using five card draw rules so I can spend more time on creating code for the higher hands.  Then just do a simple 5 card and 7 card stud simulation i.e. no draws.  

I also considered doing a blackjack simulation but thought maybe some of the rules would be more complicated than a poker simulation.  I could be wrong.  

Occasional Contributor
Posts: 12

Re: Poker Simulation

Here is the code we used to create a standard 52 card deck.  We shuffled using proc sort and dealt a five card hand using proc surveryselect.  Finally, we used proc freq to keep track of "kind", i.e. one of a kind, two of a kind (pair), and three of a kind (trips).  Does anyone have suggestions on how to program SAS to find the frequency of the higher ranked hands (straights, flushes, royal flush, etc.)?

%let seed=1210; 
%let decksize=52;
%let handsize=5;
%let nsims=10;
%let nhands=1;

data _null_;
ncards=&handsize*&nhands;
call symput("ncards", ncards);
run;

data deck;
call streaminit(&seed);
do suit="C","D","H","S";
do value="2","3","4","5","6","7","8","9","T","J","Q","K","A";
order=rand("uniform");
output;
end;
end;
run;

proc sort data=deck;
by order;

/*proc print data=deck;
run;*/ 

proc surveyselect data=deck
method=srs
seed=&seed
sampsize=&ncards
rep=&nsims
out=cards;
run;

/*proc print data=cards;
run; */

/*data hands;
set cards;
hand=ceil(_n_/&handsize);
run;*/

/*proc print data=hands;
run;*/


proc freq data=cards;
tables value/out=freqval;
by replicate;
run;

/*proc print data=freqval;
run;*/

proc means data=freqval;
var count;
by replicate;
output out=maxnum max=kind;
run;

proc freq data=maxnum;
tables kind;
run;
Occasional Contributor
Posts: 12

Re: Poker Simulation

More specifically, does anyone have suggestions on how to program SAS to recognize straights using the code listed?
Trusted Advisor
Posts: 1,345

Re: Poker Simulation

Recognizing straights:

 

  1.    Instead of do val="2","3",..... "K","A", why not make all the values into numbers, as in
          do val=2 to 14
       If you want to see the letters J, Q, K, and A then just apply a format to val.

  2. Let's say you have your hand recorded in 5 variables: card1 through card5, and define
        array crd{5} card1-card5
    Then for a straight I believe it is sufficient if
    1. range(of crd{*})=4
        and
    2. var(of crd{*})=2.5

 

New Contributor
Posts: 3

Re: Poker Simulation

Rick_SAS,

 

I am interested to view the blackjack reference you provided however I am unable to view as it states access denied--You do not have sufficient privileges for this resource or its parent to perform this action. Could you please DM or share another way. Thank you.

 

 

 

Super User
Posts: 23,771

Re: Poker Simulation

Posted in reply to jltroup10

Both of Rick's link work fine for me. Are both not working?

You may need to try a different computer.

New Contributor
Posts: 3

Re: Poker Simulation

The first link (blackjack) doesn't work. The second link works fine for me. 

Super User
Posts: 23,771

Re: Poker Simulation

Posted in reply to jltroup10
Ask a Question
Discussion stats
  • 14 replies
  • 451 views
  • 4 likes
  • 8 in conversation