# 34.1 - Random Sampling without Replacement

34.1 - Random Sampling without ReplacementRandomly selecting records from a large data set may be helpful if your data set is so large as to prevent or slow processing, or if one is conducting a survey and needs to select a random sample from some master database. When you select records randomly from a larger data set (or some master database), you can achieve the sampling in a few different ways, including:

**sampling without replacement**, in which a subset of the observations are selected randomly, and once an observation is selected it cannot be selected again.**sampling with replacement**, in which a subset of observations are selected randomly, and an observation may be selected more than once.**selecting a stratified sample**, in which a subset of observations are selected randomly from each group of the observations defined by the value of a stratifying variable, and once an observation is selected it cannot be selected again.

In this section, we'll investigate sampling without replacement. Then, in the next two sections, we'll investigate sampling with replacement and selecting a stratified sample. Throughout the three sections we'll work with a contrived mailing list. We'll use the list under the guise of being a large catalog mail-order company wanting to conduct a random survey of a subset of our customers. The actual list we'll use is admittedly (much) smaller than what we would be working with in practice. Our teeny-tiny mailing list is, of course, used merely for the purpose of illustrating some random sampling techniques in SAS.

## Example 34.1

The mailing list with which we will be working is contained in a permanent SAS data set called mailing. The following SAS code simply prints the mailing list:

```
OPTIONS PS = 58 LS = 80 NODATE NONUMBER;
LIBNAME stat482 'C:\Simon\Stat482wc\sp09\10random\sasndata';
PROC PRINT data=stat482.mailing;
title 'Sample Dataset: Mailing List';
RUN;
```

First, click the mailing data set in order to save the data set to a convenient location on your computer. Then, after you launch the SAS program, edit the LIBNAME statement so that it reflects the location in which you saved the data set. Run * * the program and review the resulting output in order to familiarize yourself with the data set.

## Approximate-Sized Samples

When using a computer program, such as SAS, to randomly select a subset of observations from some larger data set, there are two approaches we can take. We could tell SAS to randomly select a percentage, say 30%, of the observations in the data set. Or, we could tell SAS to randomly select an exact number, say 25, of the observations in the data set. With the former approach, we cannot be guaranteed that the subset data set will achieve a specific size. We consider such samples then an "approximate-sized sample." In general, to obtain an approximate-sized sample, one selects *k*% of the observations from the original data set.

## Example 34.2

The following program illustrates how to use a SAS data step to obtain an **approximate-sized random sample without replacement**. Specifically, the program uses the **ranuni** function and a WHERE statement to tell SAS to randomly sample approximately 30% of the 50 observations from the permanent SAS data set *mailing*:

```
DATA sample1A (where = (random le 0.30));
set stat482.mailing;
random = ranuni(43420);
RUN;
PROC PRINT data=sample1A NOOBS;
title1 'Sample1A: Approximate-Sized Simple Random Sample';
title2 'without Replacement';
RUN;
```

Launch and run * * the SAS program. Then, review the resulting output to see the random sample that SAS selected from the *mailing* data set. You should note a couple of things. First, the people that appear in the random sample appear to be fairly uniformly distributed across the 50 possible *Num* values. Also, the final random sample contains 20 of the 50 observations in the *mailing* data set. At 40% (20 out of 50), this is a little higher than the 30% sample we were asking for, but it should not be surprising as it is an artifact of the method used. Finally, note that the variable *random* contains only values that are smaller than 0.30, as should be expected in light of the WHERE= option attached to the DATA statement.

Okay, now how does the program work? Before we answer the question, note that the technique we use is a technique commonly used by statisticians. It will work in any program, not just SAS. Now, for the answer ... the *random* assignment statement tells SAS to use the **ranuni** function to generate a (pseudo) random number between 0 and 1 and to assign the resulting number to a variable called *random*. The number 43420 that appears in the parentheses of the **ranuni** function is specified by the user and is called the **seed**. In general:

- The seed must be a nonnegative number less than 2,147,483,647.
- A given seed always produces the same results. That is, using the same seed, the
**ranuni**function would select the same observations. - If you choose 0 as the seed, then the computer clock time at execution is used. In this case, it is very unlikely that the
**ranuni**function would produce the same results. It should be noted, that it is common practice when conducting research to use a non-zero seed, so that the results could be reproduced if necessary. - The
**ranuni**function can be used without assigning it to another variable. We assigned the value to the variable called*random*just so we could print the results.

Now, because the numbers generated by the **ranuni** function are uniformly distributed across the numbers between 0 and 1, we should expect about 30% of the random numbers to be less than 0.30. That's where the WHERE= option on the DATA statement comes into play. If the random number generated is less than or equal to 0.30, then the observation is selected for inclusion in the sample. Since the *mailing* data set has 50 observations, about 30% of the observations should be selected to create a sample of approximately 15 people. Because the selection depends on the values of the numbers generated, the sample cannot be guaranteed to be of a certain size.

You might want to change the seed a few times to see how it affects the sample. If you use the seed 1, for example, you'll see that the new random sample contains 15 observations, not 20 as in our first sample. You might also want to change the proportion 0.30 to various other numbers between 0 and 1 to see how it affects the size of the sample.

## Example 34.3

The following code illustrates an alternative way of randomly selecting an **approximate-sized random sample without replacement**. Specifically, the program uses the SURVEYSELECT procedure to tell SAS to randomly sample approximately 30% of the 50 observations from the permanent SAS data set *mailing*:

```
PROC SURVEYSELECT data = stat482.mailing
out = sample1B
method = SRS
seed = 12345678
samprate = 0.30;
title;
RUN;
PROC PRINT data = sample1B NOOBS;
title1 'Sample1B: Approximate-Sized Simple Random Sample';
title2 'without Replacement (using PROC SURVEYSELECT)';
RUN;
```

Launch and run * * the SAS program. Then, review the resulting output to convince yourself that the code did indeed select a sample from the *mailing* data set. As you can see, the SURVEYSELECT procedure produces one page of output that is merely informational, reiterating much of the information that we supplied to SAS in our SURVEYSELECT code:

- The DATA= option tells SAS the name of the input data set (
*stat482.mailing*) from which observations should be selected. - The OUT= option tells SAS the name of the output data set (
*sample1B*) in which the selected observations should be stored. - The METHOD= option tells SAS the sampling method that should be used. Here, SRS tells SAS to use the simple random sampling method to select observations, that is, with equal probability and without replacement.
- The SEED= option tells SAS the initial seed (12345678) for generating the random number. In general, the value of the SEED= option must be an integer, and if you do not specify the SEED= option, or if the SEED= value is negative or zero, the computers clock is used to obtain the initial seed.
- The SAMPRATE= option tells SAS what proportion (0.30) of the input data set should be sampled.

Oh, and the empty title statement that appears in the code is there merely to minimize any confusion its absence may cause you. If it, or another TITLE statement, is not present, the first (informational) page of the SURVEYSELECT output will contain the most recent title, which in this case would concern Sample1A from the previous example. Now that would be confusing!

## Exact-Sized Samples

Thus far, we've produced only approximate-sized random samples without replacement. Now, we'll turn our attention to three examples that illustrate how to produce exact-sized random samples without replacement. We'll start (naturally?!) with the most complicated procedure first (using a DATA step) and end up with the most straightforward procedure last (using the SURVEYSELECT procedure).

## Example 34.4

The following program illustrates how to use a SAS data step to obtain an **exact-sized random sample without replacement**. Specifically, the program uses the **ranuni** function in a DATA step to tell SAS to randomly sample exactly 15 of the 50 observations from the permanent SAS data set *mailing*:

```
DATA sample2;
set stat482.mailing nobs=total;
if _N_ = 1 then n=total;
retain k 15 n;
random = ranuni(860244);
propn = k/n;
if random le propn then
do;
output;
k=k-1;
end;
n=n-1;
if k=0 then stop;
RUN;
PROC PRINT data=sample2 NOOBS;
title1 'Sample2: Exact-Sized Simple Random Sample';
title2 'without Replacement';
var num name n k random propn;
RUN;
```

Launch and run * * the SAS program. Then, review the resulting output to convince yourself that the code did indeed select a sample of 15 observations from the *mailing* data set.

In summary, here's the approach used to select the sample:

- For each observation in the data set, generate a uniform random number.
- Select the first observation in the original data set for inclusion in the sample if its random number is less than or equal to the proportion of records needed (15 of 50, or 0.30).
- Modify the proportion still needed in the sample. Here, it is 14/49 if the first observation was selected for the sample; and it is 15/49 if it was not. If the random number generated for the second observation is less than or equal to this proportion, include it in the sample.
- Continue this process until you have selected exactly 15 observations.

Now, how to accomplish this approach using the SAS DATA step? Here's how we did it step-by-step:

*k* = the number of observations needed to complete the sample.

*n* = the number of observations left to read from the original data set.

- Define two variables
*k*and*n*, where: - Using the NOBS= option of the SET statement, determine the number of observations in the
*stat482.mailing*data set and assign the value to a variable called*total*. In general, the NOBS= option creates and names a temporary variable whose value is the total number of observations in the data set specified in the SET statement. - For the first observation, that is, when the automatic variable _N_ equa1s 1, set the variable
*n*to the value of the variable*total*(here, 50). (Recall that automatic variables are created automatically by the DATA step, are added to the program data vector, but are not output to the data set being created. The values of automatic variables are retained from one iteration of the DATA step to the next, rather than set to missing. The automatic variable _N_ is initially set to 1. Each time the DATA step loops past the DATA statement, the variable _N_ increments by 1. The value of _N_ represents the number of times the DATA step has iterated, and often equals the number of observations in the output data set.) - Using the RETAIN statement, initialize
*k*to 15, the number of observations desired in the final sample. - Use the
**ranuni**function (starting with a seed of 860244) to generate a uniform random number between 0 and 1. Use*k*and*n*to determine the proportion of observations that still needs to be selected from the mailing data set. - If the random number generated is less than the proportion of observations still needed, then OUTPUT the observation to the output data set. When an observation is selected, reduce the number of observations still needed in the sample by 1 (that is,
*k*=*k*-1). - At the end of each iteration of the DATA step:
- reduce the number of observations left in the
*mailing*data set by 1 (*n*=*n*- 1) - determine if the sample is complete (is
*k*= 0?). If yes, tell SAS to STOP. In general, the STOP statement tells SAS to stop processing the current DATA step immediately and resume processing statements after the end of the current DATA step.

- reduce the number of observations left in the

Note that the *random *= **ranuni**( ) and *propn = k/n* assignments are made here only so their values can be printed. In another situation, these values would be incorporated directly in the IF statement: if **ranuni**( ) le *k/n* then do; Additionally, *k* and *n* could be dropped from the output data set, but are kept here, so their values can be printed for educational purposes.

## Example 34.5

The following code illustrates an alternative way of using a DATA step to randomly select an **exact-sized random sample without replacement**. The code, while less efficient — because it requires that the data set be processed twice and sorted once — may feel more natural and intuitive to you:

```
DATA sample3A;
set stat482.mailing;
random=ranuni(860244);
RUN;
PROC SORT data=sample3A;
by random;
RUN;
DATA sample3A;
set sample3A;
if _N_ le 15;
RUN;
PROC PRINT data=sample3A;
title1 'Sample3A: Exact-Sized Simple Random Sample';
title2 'without Replacement';
RUN;
```

Launch and run * * the SAS program. Then, review the resulting output to convince yourself that the code did indeed select a sample of 15 observations from the *mailing* data set.

The approach used is very similar to the approach used previously for selecting an approximate-sized sample without replacement. That is:

- For each observation in the data set, use the
**ranuni**function to generate a uniform random number and store it in the variable called*random*. - Sort the data set by the random number
*random*. - Select the first 15 observations from the sorted data set using the automatic variable _N_ (if _N_ le 15).

By so doing, every observation in the *mailing* data set has an equal likelihood of being one of the first 15 observations, and therefore an equal likelihood of being selected into the sample.

## Example 34.6

The following code illustrates yet another alternative way of randomly selecting an **exact-sized random sample without replacement**. Specifically, the program uses the SURVEYSELECT procedure to tell SAS to randomly sample exactly 15 of the 50 observations from the permanent SAS data set *mailing*:

```
PROC SURVEYSELECT data = stat482.mailing
out = sample3B
method = SRS
seed = 12345678
sampsize = 15;
title;
RUN;
PROC PRINT data = sample3B;
title1 'Sample3B: Exact-Sized Simple Random Sample';
title2 'without Replacement (using PROC SURVEYSELECT)';
RUN;
```

Launch and run * * the SAS program. Then, review the resulting output to convince yourself that the code did indeed select a sample of 15 observations from the *mailing* data set. Note that the only difference between this code and the previous SURVEYSELECT code is the *sampsize* = 15 statement here replaces the *samprate* = 0.30 statement there. You might want to change the seed (*seed*) and sample size (*sampsize*) a few times to see how it affects the sample.