BookmarkSubscribeRSS Feed

Combinations and Permutations Made Easy in SAS Studio 4.3

Started ‎04-25-2018 by
Modified ‎04-25-2018 by
Views 3,684

SAS Studio 4.3 on Viya 3.3 lets you run code or use SAS Studio tasks to calculate permutations and combinations and to solve probability problems. This post explores a few of these capabilities in SAS Studio 4.3, including the classic birthday problem.1egg-name-draw-300x225.jpg

 

In my family we have many Easter traditions. One is that when you arrive for Easter dinner, you select a name on a slip of paper, and then you draw on an egg to represent that person. You then place the egg at one of the dinner place-settings as a place marker for that person. And we all search for our eggs to find out where to sit for Easter dinner.

 

Let’s say there is one table in the window seat with three place settings. For simplicity’s sake, let’s say there are 10 family members and thus 10 decorated egg “place markers.” We want to know how many different ways we could place the eggs at that window table. We have an egg for:

  1. Anita
  2. Beth
  3. Cara
  4. Dunlea
  5. Eva
  6. Finn
  7. Giovanni
  8. Hannah
  9. Izzy
  10. Jeff

 2arrangements-1024x323.jpg

 

We have 10 eggs, and 3 spots to choose from. We could write out all possible combinations, but this is not practical. We see that even just the first 3 eggs could be placed in 6 different arrangements!

 

Instead let’s use our handy dandy permutations formula. We note that where the eggs are placed matters (order matters) and there is no replacement, that is, we cannot place the same egg in two places.

 

3permform.png 

where n is the total number of eggs (10) and k is the number we will choose from the basket to place at the window seat table (3). Plugging our numbers into this formula, we get:4perm10choose3-1024x123.jpg

 

What if we don’t care where people sit and we just want to know how many possible groups of 3 people could be placed at the window seat, not caring which seat they occupy. Then order does not matter and we instead look at the combination.5combform.png

6comb10choose3-1024x106.jpg

 

Using SAS for Permutations and Combinations

 

We can calculate this out, or we can use SAS Studio to do this for us. We can type the code out and run it in SAS Studio, as shown below:

 

*replace n with total possible choices;
*replace k with the number you choose;
data _null_;
comb=comb(n,k);
perm=perm(n,k);
put perm=/ comb=;
run;

 7SAScode.png

 

8CodeResults.png

 

If we want to see all 120 possible combinations, we can use the SAS Studio Task for Combinations.

 

9CombTask.png

 

Let’s do a little review of combinations and permutations.

 

Combinatorics and Permutations Review

 

If order matters, we must use permutations.

 

Permutations without replacement:

 

3permform.png 

as in our egg placeholder example, when we cared who sat in which place at the window table. Permutations with replacement:10-n-to-the-k-300x69.jpg

 

11-comb-lock-really-perm-lock-150x150.jpgwhere we have n choices in each of k sets. An example of a permutation with replacement is a lock. For this lock, n is 10 (the number of choices in each column) and k is 5 (the number of columns). Although this might be called colloquially a combination lock, it would better be called a permutation lock.

 

In contrast, if order does NOT matter, we use combinations. Combination with no replacement:5combform.png

 

as in our egg placeholder example, when we didn't care what order folks sat in at the window table.

 

Combination with replacement

 

The formula for combination with replacement is:

 

12comb-w-replace-form-300x125.jpg

Birthday Problem

 

Let’s consider the classic birthday question. What is the likelihood that if there are 30 people at Easter dinner, that there is at least one shared birthday? We can frame this differently, by recalling that the probability of at least one shared birthday is 1 minus the probability that there are no shared birthdays.

 

Prob (at least one) = 1 – Prob (no shared birthday)

 

We can calculate the probability of no shared birthday:13-prob-no-bday-1024x109.jpg

 

We see our numerator will be 365×364×363… til we have 30 terms. Hey this looks familiar! We can use permutations to calculate this: Perm(365,30). We can calculate our denominator as 365 multiplied by itself 30 times which is simply 36530.

 

So we have:14-prob-bday-1024x197.jpg

 

i.e., greater than a 70% chance that two people will have the same birthday!

 

As it turns out, at my family Easter dinner, my nephew Will and my cousin Finn have the same birthday…which coincidentally also happens to be Flag Day!15-Max-259x300.jpg

 

Am I putting you to sleep? Don’t worry it’s my specialty. It’s a skill that comes in handy with my baby cousins, but perhaps it’s not as helpful when writing a technical article.

 

But wake back up because we are about to see how easily we can answer the birthday question with SAS Studio! Just go to Tasks, Combinatorics and Probability, Same Birthday Probability and enter 30 as the Number of people in a room. SAS Studio writes the code for you from the task.

 

16-bday-task-1024x683.png

 

And gives you results for 2 to 30 people in a room, voila!

 

17-bday-prob-results-cropped.jpg

 

This is why we love SAS. It makes our lives easier. It’s fun to understand the math behind the scenes, but it makes our lives easier if we can just rest assured that the math is done correctly, and that is the assurance that SAS provides.

 

See also that SAS Studio 4.3 also includes tasks for Dice Roll Simulation, Coin Toss Simulation, and Poker Hand Probability.

 

17-combinatorics-and-probability.png

 

Version history
Last update:
‎04-25-2018 09:24 PM
Updated by:
Contributors

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags