Sampling and Partitioning Action Set

Simple Random Sampling

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 simple random sampling 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 srs action to perform simple random sampling on the mycas.hmeq data table:

proc cas;
   loadactionset "sampling";
   action srs result=r/table={name="hmeq"}
      samppct=10 seed=10
      output={casout={name="out",replace="TRUE"},
              copyvars={"job","reason","loan","value","delinq","derog"}};
   run;
   print r.SRSFreq; run;
quit;

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

The table parameter names the input data table to be analyzed. The samppct parameter requests that 10% of the input data be sampled. The seed parameter specifies 10 as the random seed to be used in the sampling process. The output parameter requests that the sampled data be stored in a table named mycas.out, and the copyvars parameter in the output parameter lists the variables to be copied from mycas.hmeq to mycas.out.

Output 20.3.1 shows the number of observations in the mycas.hmeq data table and the number of samples.

Output 20.3.1: Frequency Information Table

SRSFreq: Results from sampling.srs

Simple Random
Sampling Frequency
Number
of Obs
Number
of Samples
5960596


Output 20.3.2 shows the sample data, which are stored in the mycas.out data table.

Output 20.3.2: Sample Data

ObsJOBREASONLOANVALUEDELINQDEROG
1SalesDebtCon43007869800
2ProfExeHomeImp450014687002
3SelfHomeImp50005344800
4OtherDebtCon51009706400
5OfficeHomeImp55006505400
6OtherHomeImp57008575300
7OtherHomeImp58005819100
8MgrDebtCon5900.60
9MgrHomeImp60009550021
10OtherHomeImp610035250..
11ProfExeDebtCon63006900000
12ProfExeHomeImp680016006300
13OtherHomeImp70009914910
14OtherDebtCon710018010400
15OtherDebtCon75005012500
16MgrHomeImp75007179930
17OfficeDebtCon76006235710
18OtherHomeImp79007518900
19OfficeDebtCon80008690010
20OtherHomeImp82005006100


Simple Random Sampling

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 simple random sampling on the hmeq data table.

s:loadactionset{actionset="sampling"}
s:srs{table='hmeq',
      samppct=10, seed=10,
      outputTables={names={SRSFreq='srsf'}},
      output={casout={name="out", replace="TRUE"},
              copyvars={"job","reason","loan","value","delinq","derog"}}}
s:fetch{table={name="out"},to=20}

The table parameter names the input data table to be analyzed. The samppct parameter requests that 10% of the input data be sampled. The seed parameter specifies 10 as the random seed to be used in the sampling process. The outputTables parameter outputs the frequency table to the srsf data table. The output parameter requests that the sampled data be stored in a table named out, and the copyvars parameter in the output parameter lists the variables to be copied from hmeq to out.

Simple Random Sampling

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.

s.loadactionset(actionset="sampling")
s.srs(display={"names":"SRSFreq"},
      output={"casOut":{"name":"out", "replace":True},
      "copyVars":{"job","reason","loan","value","delinq","derog"}},
       samppct=10, seed=10,
       table={"name":"hmeq"},
       outputTables={"names":{"SRSFreq"}, "replace":True})

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

srs_out2=s.CASTable('SRSFreq')
print(srs_out2.fetch())

Simple Random Sampling

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 simple random sampling on the hmeq data table:

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


          table        = list(name = "hmeq"),
          samppct      = 10,
          seed         = 10,
          output       = list(casOut   = list(name = "out", replace = TRUE),
                              copyVars = list("job", "reason", "loan", "value",
                                              "delinq", "derog")),
          outputTables = list(names = "SRSFreq", replace = TRUE))

Last updated: September 09, 2021