Sampling and Partitioning Action Set

Oversampling

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 demonstrates how to use PROC CAS to perform oversampling on the mycas.hmeq data table. 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 oversample action to perform oversampling on the mycas.hmeq data table:

proc cas;
   loadactionset "sampling";
   action oversample result=r/table={name="hmeq", groupby={"bad"}}
      event="1" samppctevt=90 eventprop=0.5 partind="true" seed=10
      output={casout={name="out3",replace="TRUE"},
              copyvars={"job","loan","value","delinq","derog"},
       partindname='MyPartInd' freqname='MyFreq'};
   run;
   print r.OVERFreq; run;
run;

proc print data=mycas.out3(obs=20);
run;

The table parameter names the input data table to be analyzed. The groupby subparameter in the table parameter names the variables that are used for oversampling. The eventprop parameter requests that 50% of the sample be rare events. The samppctevt parameter requests that 90% of the rare events be sampled. The event parameter specifies that level 1 of the variable bad corresponds to a rare event. The output parameter requests that the sampled data be stored in a table named mycas.out, and the copyvars subparameter in the output parameter lists the variables to be copied from mycas.hmeq to mycas.out. The partindname subparameter in the output parameter requests that the action-generated column _PartInd_ be renamed MyPartInd, and the freqname subparameter requests that the action-generated column _Freq_ be renamed MyFreq.

Output 24.2.1 shows the number of observations in the sample and in each level of the BY variable bad in the mycas.hmeq data table.

Output 24.2.1: Frequency Information Table

OVERFreq: Results from sampling.oversample

Oversampling Frequency
IndexBADNumber
of Obs
Number
of Samples
0047711070
1111891070


Output 24.2.2 shows the first 20 output sample observations in the mycas.out data table; the MyFreq column shows the ratio of the target level’s proportion in the population to its proportion in the sample.

Output 24.2.2: Output Data Table

ObsJOBLOANVALUEDELINQDEROGMyPartIndMyFreq
1Other1100390250010.39899
2 1500...10.39899
3Other1800570372310.39899
4Sales2000622500010.39899
5Other2000550000010.39899
6Other2200346871010.39899
7Other2300401500010.39899
8ProfExe2400733950110.39899
9Other2400171800010.39899
10 2500202000010.39899
11ProfExe2500786000001.60101
12ProfExe29001130000110.39899
13Other2900679960310.39899
14Other3000203000010.39899
15Other30001935000010.39899
16Other3000141000010.39899
17Mgr3000715002.11.60101
18 310070400..01.60101
19Other3200408340010.39899
20Mgr3200.2.10.39899


Oversampling

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 oversampling on the hmeq data table:

s:loadactionset{actionset="sampling"}
s:oversample{table={name='hmeq',groupby={'bad'}},
          event="1", samppctevt=90, eventprop=0.5, partind="true", seed=10,
          outputTables={names={OVERFreq='overf'}},
          output={casout={ name="out3", replace="TRUE"},
                  copyvars={"job","reason","loan","value","delinq","derog"},
                  partindname='MyPartInd',
                     freqname='MyFreq'
                     }
             }
s:fetch{table={name="out"},to=20}

Oversampling

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 oversampling on the hmeq data table:

s.loadactionset(actionset="sampling")
s.oversample(display={"names":"OVERFreq"},
             output={"casOut":{"name":"out", "replace":True}, "copyVars":"ALL"},
             partind=True, seed=10, eventprop=0.5, event="1", samppctevt=90,
             table={"name":"hmeq", "groupBy":{"bad"}},
             outputTables={"names":{"OVERFreq"},"replace":True})

over_out=s.CASTable('out')
print(over_out.fetch(to=20))

over_out2=s.CASTable('OVERFreq')
print(over_out2.fetch())

Oversampling

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 oversampling on the hmeq data table:

          "hmeq.csv",
          header = TRUE,
          casOut = list(name = "hmeq", replace = TRUE))

          table        = list(name = "hmeq", groupby = "bad"),
          event        = "1",
          samppctevt   = 90,
          eventprop    = 0.5,
          partind      = "true",
          seed         = 10,
          output       = list(casOut      = list(name = "out3", replace = TRUE),
                              copyVars    = list("job","loan","value","delinq",
                                                 "derog"),
                              partindname = "MyPartInd",
                              freqname    = "MyFreq"),
          outputTables = list(names = "OVERFreq", replace = TRUE))

Last updated: March 27, 2025