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
| Simple Random Sampling Frequency | |
|---|---|
| Number of Obs | Number of Samples |
| 5960 | 596 |
Output 20.3.2 shows the sample data, which are stored in the mycas.out data table.
Output 20.3.2: Sample Data
| Obs | JOB | REASON | LOAN | VALUE | DELINQ | DEROG |
|---|---|---|---|---|---|---|
| 1 | Sales | DebtCon | 4300 | 78698 | 0 | 0 |
| 2 | ProfExe | HomeImp | 4500 | 146870 | 0 | 2 |
| 3 | Self | HomeImp | 5000 | 53448 | 0 | 0 |
| 4 | Other | DebtCon | 5100 | 97064 | 0 | 0 |
| 5 | Office | HomeImp | 5500 | 65054 | 0 | 0 |
| 6 | Other | HomeImp | 5700 | 85753 | 0 | 0 |
| 7 | Other | HomeImp | 5800 | 58191 | 0 | 0 |
| 8 | Mgr | DebtCon | 5900 | . | 6 | 0 |
| 9 | Mgr | HomeImp | 6000 | 95500 | 2 | 1 |
| 10 | Other | HomeImp | 6100 | 35250 | . | . |
| 11 | ProfExe | DebtCon | 6300 | 69000 | 0 | 0 |
| 12 | ProfExe | HomeImp | 6800 | 160063 | 0 | 0 |
| 13 | Other | HomeImp | 7000 | 99149 | 1 | 0 |
| 14 | Other | DebtCon | 7100 | 180104 | 0 | 0 |
| 15 | Other | DebtCon | 7500 | 50125 | 0 | 0 |
| 16 | Mgr | HomeImp | 7500 | 71799 | 3 | 0 |
| 17 | Office | DebtCon | 7600 | 62357 | 1 | 0 |
| 18 | Other | HomeImp | 7900 | 75189 | 0 | 0 |
| 19 | Office | DebtCon | 8000 | 86900 | 1 | 0 |
| 20 | Other | HomeImp | 8200 | 50061 | 0 | 0 |
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))