BookmarkSubscribeRSS Feed
kbry1180
Calcite | Level 5

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.)

14 REPLIES 14
ballardw
Super User

With or without draw?

Or Texas Hold-em?

Or other version, Dr Pepper perhaps?

PeterClemmensen
Tourmaline | Level 20

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?

kbry1180
Calcite | Level 5
I am considering doing probabilities across different games. Perhaps run simulations using 5 card stud rules, 5 card (single draw), and seven card stud.
Ksharp
Super User

Calling @Rick_SAS

 

Check RANDPERK() in IML .

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

Rick_SAS
SAS Super FREQ

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!

 

 

 

 

kbry1180
Calcite | Level 5

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.  

kbry1180
Calcite | Level 5

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;
kbry1180
Calcite | Level 5
More specifically, does anyone have suggestions on how to program SAS to recognize straights using the code listed?
mkeintz
PROC Star

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

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
jltroup10
Calcite | Level 5

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.

 

 

 

Reeza
Super User

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

You may need to try a different computer.

jltroup10
Calcite | Level 5

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 14 replies
  • 2956 views
  • 4 likes
  • 8 in conversation