Sampling and Partitioning Action Set
Segment-Stratified Sampling for Each Target
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts. A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.
This example performs segment-stratified partitioning of 5,960 fictitious mortgages for each target variable, with the groupby parameter bad used as the segment variable. This example essentially performs two stratified partitions simultaneously for every stratum that is defined by the level combinations of each target with the variable that is specified in the groupby parameter. The input data table mycas.hmeq includes information about fictitious mortgages. Each observation represents an applicant for a home equity loan, and all applicants have an existing mortgage.
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:
data mycas.hmeq;
set sampsio.hmeq;
run;
This DATA step assumes that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
The following statements load the sampling action set and then use the stratified action to partition the mycas.hmeq data table for each stratum:
proc cas;
loadactionset "sampling";
action stratified result=r/table={name="hmeq",groupby={"bad"}}
samppct=10, samppct2=20, seed=10, target={"job","reason"},
outputTables={names={STRAFreq="straf",PartIndMap="pimap"}},
output={casout={name="out",replace="TRUE"},
copyvars={"job","reason","loan","value","delinq","derog"}};
run;
print r.STRAFreq;
print r.PartIndMap;
run;
quit;
proc print data=mycas.out(obs=20);
run;
The table parameter names the input data table to be analyzed. The groupby subparameter in the table parameter names the variables to use for segmentation. The samppct parameter requests that 10% of the input data be included in the training partition, and the samppct2 parameter option requests that 20% of the input data be included in the testing partition. The seed parameter specifies 10 as the random seed to use in the partitioning process. The by parameter requests that the variable bad be used as the segment variable. The target parameter requests that stratified sampling be performed for each target variable job and reason in each segment level. The outputTables parameter outputs the frequency table to the mycas.straf data table and outputs the partition indicator map table to the mycas.pimap data table. The output parameter requests that the sampled data be stored in a table named mycas.out, and the copyvars parameter lists the variables to be copied from mycas.hmeq to mycas.out. The output data table, mycas.out, also includes two partition indicators (each one for each target), which show whether each observation is selected for a partition (1 for training, 2 for testing, or 0 for not selected).
Output 24.5.1 shows the frequency information for each level of stratification that is defined by the level combination of the groupby variable bad and each target variable in the mycas.hmeq data table.
Output 24.5.1: Frequency Information Table
| Stratified Sampling Frequency | |||||
|---|---|---|---|---|---|
| Target Name | Target Level | BAD | Number of Obs | Sample Size 1 | Sample Size 2 |
| JOB | 0 | 256 | 26 | 51 | |
| JOB | Mgr | 0 | 588 | 59 | 117 |
| JOB | Office | 0 | 823 | 82 | 165 |
| JOB | Other | 0 | 1834 | 183 | 367 |
| JOB | ProfExe | 0 | 1064 | 106 | 213 |
| JOB | Sales | 0 | 71 | 7 | 14 |
| JOB | Self | 0 | 135 | 14 | 27 |
| JOB | 1 | 23 | 2 | 5 | |
| JOB | Mgr | 1 | 179 | 18 | 36 |
| JOB | Office | 1 | 125 | 13 | 25 |
| JOB | Other | 1 | 554 | 55 | 111 |
| JOB | ProfExe | 1 | 212 | 21 | 43 |
| JOB | Sales | 1 | 38 | 4 | 7 |
| JOB | Self | 1 | 58 | 6 | 11 |
| REASON | 0 | 204 | 20 | 41 | |
| REASON | DebtCon | 0 | 3183 | 318 | 637 |
| REASON | HomeImp | 0 | 1384 | 138 | 277 |
| REASON | 1 | 48 | 5 | 9 | |
| REASON | DebtCon | 1 | 745 | 75 | 149 |
| REASON | HomeImp | 1 | 396 | 40 | 79 |
Output 24.5.2 shows the map of each partition indicator to each target.
Output 24.5.2: Partition Indicator Map
| Partition Indicator Map | |
|---|---|
| Partition Indicator Name | Target Name |
| _PartInd1_ | JOB |
| _PartInd2_ | REASON |
Output 24.5.3 shows the first 20 output sample observations in mycas.out. The _PartInd1_ column shows which partition each observation is selected for (1 for training, 2 for testing, or 0 for not selected) for the target variable job. The _PartInd2_ column shows which partition each observation is selected for (1 for training, 2 for testing, or 0 for not selected) for the target variable reason.
Output 24.5.3: Sample Output with Partition Indicator
| Obs | JOB | REASON | LOAN | VALUE | DELINQ | DEROG | _PartInd1_ | _PartInd2_ |
|---|---|---|---|---|---|---|---|---|
| 1 | Other | HomeImp | 1100 | 39025 | 0 | 0 | 2 | 0 |
| 2 | 1500 | . | . | . | 0 | 0 | ||
| 3 | Other | HomeImp | 1800 | 57037 | 2 | 3 | 0 | 0 |
| 4 | Sales | HomeImp | 2000 | 62250 | 0 | 0 | 0 | 1 |
| 5 | Other | HomeImp | 2000 | 55000 | 0 | 0 | 0 | 0 |
| 6 | Other | HomeImp | 2200 | 34687 | 1 | 0 | 0 | 0 |
| 7 | Other | HomeImp | 2300 | 40150 | 0 | 0 | 0 | 0 |
| 8 | ProfExe | HomeImp | 2400 | 73395 | 0 | 1 | 0 | 0 |
| 9 | Other | HomeImp | 2400 | 17180 | 0 | 0 | 0 | 2 |
| 10 | HomeImp | 2500 | 20200 | 0 | 0 | 0 | 1 | |
| 11 | ProfExe | HomeImp | 2500 | 78600 | 0 | 0 | 2 | 0 |
| 12 | ProfExe | DebtCon | 2900 | 113000 | 0 | 1 | 1 | 0 |
| 13 | Other | HomeImp | 2900 | 67996 | 0 | 3 | 0 | 0 |
| 14 | Other | HomeImp | 3000 | 20300 | 0 | 0 | 1 | 0 |
| 15 | Other | HomeImp | 3000 | 193500 | 0 | 0 | 0 | 0 |
| 16 | Other | HomeImp | 3000 | 14100 | 0 | 0 | 0 | 0 |
| 17 | Mgr | HomeImp | 3000 | 71500 | 2 | . | 1 | 0 |
| 18 | 3100 | 70400 | . | . | 0 | 2 | ||
| 19 | Other | HomeImp | 3200 | 40834 | 0 | 0 | 1 | 0 |
| 20 | Mgr | HomeImp | 3200 | . | 2 | . | 0 | 0 |
Segment-Stratified Sampling for Each Target
This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the hmeq data to the comma-separated-value (CSV) file hmeq.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="hmeq.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
The following code loads the sampling action set and then performs segment-stratified sampling for each target on the hmeq data table:
s:loadactionset{actionset="sampling"}
s:stratified{table={name='hmeq',groupby={'bad'}},
target={'job','reason'},
samppct=10, samppct2=20, partind="TRUE", seed=10,
outputTables={names={STRAFreq='straf',PartIndMap='pimap'}},
output={casout={name="out", replace="TRUE"},
copyvars={"job","reason","loan","value","delinq","derog"}}}
s:fetch{table={name="out"},to=20}
Segment-Stratified Sampling for Each Target
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the hmeq data to the comma-separated-value (CSV) file hmeq.csv and then use the following code to load the CSV file into CAS:
s.upload_file('hmeq.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
The following code loads the sampling action set and then performs segment-stratified sampling for each target on the hmeq data table:
s.loadactionset(actionset="sampling")
s.stratified(
output={"casOut":{"name":"out", "replace":True}, "copyVars":"ALL"},
samppct=10, samppct2=20, partind=True, seed=10,
target={'job','reason'},
table={"name":"hmeq", "groupBy":{"bad"}},
outputTables={"names":{"STRAFreq","PartIndMap"}, "replace":True} )
stra_out=s.CASTable('out')
print(stra_out.fetch(to=20))
stra_out2=s.CASTable('STRAFreq')
print(stra_out2.fetch())
stra_out3=s.CASTable('PartIndMap')
print(stra_out3.fetch())
Segment Stratified Sampling for Each Target
This section contains R code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the hmeq data to the comma-separated-value (CSV) file hmeq.csv and then use the following code to load the CSV file into CAS:
m <- cas.read.csv(s, "hmeq.csv", casOut=list(name="hmeq"))
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
The following code loads the sampling action set and then performs segment-stratified sampling on the hmeq data table:
"hmeq.csv",
header = TRUE,
casOut = list(name = "hmeq", replace = TRUE))
table = list(name = "hmeq", groupby = "bad"),
samppct = 10,
samppct2 = 20,
partind = TRUE,
seed = 10,
targe = list("job","reason"),
output = list(casout = list(name = "out", replace = "TRUE"),
copyvars = list("job", "reason", "loan", "value",
"delinq", "derog")),
outputTables = list(names ={ "STRAFreq", "PartIndMap" }, replace = TRUE)