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

STRAFreq: Results from sampling.stratified

Stratified Sampling Frequency
Target NameTarget LevelBADNumber
of Obs
Sample
Size 1
Sample
Size 2
JOB 02562651
JOBMgr058859117
JOBOffice082382165
JOBOther01834183367
JOBProfExe01064106213
JOBSales071714
JOBSelf01351427
JOB 12325
JOBMgr11791836
JOBOffice11251325
JOBOther155455111
JOBProfExe12122143
JOBSales13847
JOBSelf158611
REASON 02042041
REASONDebtCon03183318637
REASONHomeImp01384138277
REASON 14859
REASONDebtCon174575149
REASONHomeImp13964079


Output 24.5.2 shows the map of each partition indicator to each target.

Output 24.5.2: Partition Indicator Map

PartIndMap: Results from sampling.stratified

Partition Indicator Map
Partition Indicator NameTarget 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

ObsJOBREASONLOANVALUEDELINQDEROG_PartInd1__PartInd2_
1OtherHomeImp1100390250020
2  1500...00
3OtherHomeImp1800570372300
4SalesHomeImp2000622500001
5OtherHomeImp2000550000000
6OtherHomeImp2200346871000
7OtherHomeImp2300401500000
8ProfExeHomeImp2400733950100
9OtherHomeImp2400171800002
10 HomeImp2500202000001
11ProfExeHomeImp2500786000020
12ProfExeDebtCon29001130000110
13OtherHomeImp2900679960300
14OtherHomeImp3000203000010
15OtherHomeImp30001935000000
16OtherHomeImp3000141000000
17MgrHomeImp3000715002.10
18  310070400..02
19OtherHomeImp3200408340010
20MgrHomeImp3200.2.00


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)
Last updated: March 27, 2025