## Combinations and Permutations Made Easy in SAS Studio 4.3

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

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.

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:

 Anita Beth Cara Dunlea Eva Finn Giovanni Hannah Izzy Jeff

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.

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:

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.

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

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

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

## Combinatorics and Permutations Review

If order matters, we must use permutations.

Permutations without replacement:

as in our egg placeholder example, when we cared who sat in which place at the window table. Permutations with replacement:

where 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:

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:

## 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:

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:

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!

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.

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

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.

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