PARTITION Procedure
Example 24.6 Segment-Stratified Sampling for Each Target Using the FREQ Statement
(View the complete code for this example.)
This example demonstrates how to use PROC PARTITION to perform segment-stratified partitioning for each target with the FREQ statement. It uses the same data table as in Example 24.1.
You can load the sampsio.hmeq data set into your CAS session by naming your CAS engine libref in the first statement of the following DATA step. This DATA step assumes that your CAS engine libref is named mylib, but you can substitute any appropriately defined CAS engine libref.
data mylib.hmeq;
set sampsio.hmeq;
id = _N_;
freqVar = 10;
run;
Note that the frequency variable freqVar is set to 10 for all the observations in the hmeq data set in the CAS engine libref mylib.
The following statements perform the partitioning:
proc partition data=mylib.hmeq samppct=10 samppct2=20 seed=10;
by bad;
target job reason;
freq freqVar;
output out=mylib.out6 copyvars=(job reason loan value delinq derog);
run;
proc print data=mylib.out6(obs=20);
run;
The SAMPPCT=10 option requests that 10% of the input data be included in the training partition, and the SAMPPCT2=20 option requests that 20% of the input data be included in the testing partition. The SEED= option specifies 10 as the random seed to be used in the partitioning process. The BY statement requests that the BY variable bad be used as the segmentation variable. The TARGET statement requests that stratified sampling be performed for each target variable job and reason in each segment level. The FREQ statement requests that the variable freqVar be used as the frequency variable. In this example, the procedure treats each observation as if it appears 10 times. The OUTPUT statement requests that the sampled data be stored in a table named mylib.out6, and the COPYVARS= option lists the variables to be copied from mylib.hmeq to mylib.out6. The output data table, mylib.out6, also includes one partition indicator variable that shows whether each observation is selected for a partition (1 for training, 2 for testing, or 0 for not selected), as well as two sample frequency variables (one for each target variable in each segment level in the BY variable bad).
Output 24.6.1 shows the frequency information for each level combination of the BY variable bad and each target variable in the mylib.out6 data table.
Output 24.6.1: Frequency Information Table
| Stratified Sampling Frequency | |||||
|---|---|---|---|---|---|
| Target Name | Target Level | BAD | Number of Obs | Sample Size 1 | Sample Size 2 |
| JOB | 0 | 2560 | 256 | 512 | |
| JOB | Mgr | 0 | 5880 | 588 | 1176 |
| JOB | Office | 0 | 8230 | 823 | 1646 |
| JOB | Other | 0 | 18340 | 1834 | 3668 |
| JOB | ProfExe | 0 | 10640 | 1064 | 2128 |
| JOB | Sales | 0 | 710 | 71 | 142 |
| JOB | Self | 0 | 1350 | 135 | 270 |
| JOB | 1 | 230 | 23 | 46 | |
| JOB | Mgr | 1 | 1790 | 179 | 358 |
| JOB | Office | 1 | 1250 | 125 | 250 |
| JOB | Other | 1 | 5540 | 554 | 1108 |
| JOB | ProfExe | 1 | 2120 | 212 | 424 |
| JOB | Sales | 1 | 380 | 38 | 76 |
| JOB | Self | 1 | 580 | 58 | 116 |
| REASON | 0 | 2040 | 204 | 408 | |
| REASON | DebtCon | 0 | 31830 | 3183 | 6366 |
| REASON | HomeImp | 0 | 13840 | 1384 | 2768 |
| REASON | 1 | 480 | 48 | 96 | |
| REASON | DebtCon | 1 | 7450 | 745 | 1490 |
| REASON | HomeImp | 1 | 3960 | 396 | 792 |
Output 24.6.2 shows the map of each sample frequency to each target variable.
Output 24.6.2: Sample Frequency Map
| Sample Frequency Map | |
|---|---|
| Sample Frequency Name | Target Name |
| _SampleFreq1_ | JOB |
| _SampleFreq2_ | REASON |
Output 24.6.3 shows the first 20 output sample observations in mylib.out6. Each observation is duplicated by three rows in the output, with the _PartInd_ column showing which partition each observation is selected for (1 for training, 2 for testing, or 0 for not selected). The _SampleFreq1_ column shows the sample frequency for each partition for the target variable job. The _SampleFreq2_ column shows the sample frequency for each partition for the target variable reason. Note that the rows for which all the _SampleFreq_ columns have a value of 0 are not included in the output.
Output 24.6.3: Sample Output with Partition Indicator
| Obs | JOB | REASON | LOAN | VALUE | DELINQ | DEROG | _PartInd_ | _SampleFreq1_ | _SampleFreq2_ |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Other | HomeImp | 1100 | 39025 | 0 | 0 | 0 | 8 | 7 |
| 2 | Other | HomeImp | 1100 | 39025 | 0 | 0 | 1 | 1 | 2 |
| 3 | Other | HomeImp | 1100 | 39025 | 0 | 0 | 2 | 1 | 1 |
| 4 | Other | HomeImp | 1300 | 68400 | 2 | 0 | 0 | 8 | 8 |
| 5 | Other | HomeImp | 1300 | 68400 | 2 | 0 | 1 | 1 | 1 |
| 6 | Other | HomeImp | 1300 | 68400 | 2 | 0 | 2 | 1 | 1 |
| 7 | Other | HomeImp | 1500 | 16700 | 0 | 0 | 0 | 9 | 6 |
| 8 | Other | HomeImp | 1500 | 16700 | 0 | 0 | 1 | 0 | 1 |
| 9 | Other | HomeImp | 1500 | 16700 | 0 | 0 | 2 | 1 | 3 |
| 10 | 1500 | . | . | . | 0 | 9 | 6 | ||
| 11 | 1500 | . | . | . | 1 | 1 | 1 | ||
| 12 | 1500 | . | . | . | 2 | 0 | 3 | ||
| 13 | Office | HomeImp | 1700 | 112000 | 0 | 0 | 0 | 8 | 8 |
| 14 | Office | HomeImp | 1700 | 112000 | 0 | 0 | 1 | 0 | 1 |
| 15 | Office | HomeImp | 1700 | 112000 | 0 | 0 | 2 | 2 | 1 |
| 16 | Other | HomeImp | 1700 | 40320 | 0 | 0 | 0 | 9 | 7 |
| 17 | Other | HomeImp | 1700 | 40320 | 0 | 0 | 1 | 0 | 1 |
| 18 | Other | HomeImp | 1700 | 40320 | 0 | 0 | 2 | 1 | 2 |
| 19 | Other | HomeImp | 1800 | 57037 | 2 | 3 | 0 | 8 | 8 |
| 20 | Other | HomeImp | 1800 | 57037 | 2 | 3 | 1 | 2 | 1 |