Lua logo

Integrate SAS with Lua

Getting started with SAS Viya for Lua

The SAS Scripting Wrapper for Analytics Transfer (SWAT) package for Lua is a Lua interface to SAS Cloud Analytic Services (CAS) which is the centerpiece of the SAS Viya framework. This package allows you to:

  • retain the ease-of-use of Lua on the client side to further post process CAS result tables
  • load data into memory and apply CAS actions
  • transform, summarize, model and score the data

Sample code

The example below shows how to use the SUMMARY ACTION to get various descriptive statistics (minimum and maximum values, mean, standardDeviation, etc.) about a data table using SAS Cloud Analytics Services. This example requires that you have in-memory data to analyze.

Get Descriptive Statistics

Run the Summary Action
Save the sample program below in a file SummaryExample1.lua and run the program with the lua command from a console:


lua SummaryExample.lua

swat = require 'swat'
swat.setOption('display.width', 120)
s = swat.CAS("cloud.example.com", 5570)

                                                                --1
result = s:upload{"/path/to/iris.csv", casout={name="iris", replace=true}}

irisTbl = {}                                                    --2
irisTbl.name = result.tableName
irisTbl.groupBy = {"species"}

stats = {"min", "max", "n", "nmiss", "mean", "std", "stderr"}

results, info = s:simple_summary{table=irisTbl, subset=stats}   --3

bgi = results.ByGroupInfo                                       --4

print(bgi)
print()

-- so we don't print this again
results.ByGroupInfo = nil

for k,v in pairs(results) do
  print(k, v)
  print()
end
  1. Perform a client-side load of the Iris.csv file from a path that Lua can access. The upload method transfers the CSV file from Lua to the server, and then the server loads the data into memory.
  2. A variable with the name IrisTbl is created to represent the in-memory table. The groupBy value, Species, is set as a key in the variable.
  3. The results from the simple_summary action include the descriptive statistics that are listed in the Stats variable.
  4. When you perform BY-group processing, the results include one table that is named ByGroupInfo and a table for each BY-group. The ByGroupInfo table is printed first and then a loop is used to print the remaining tables.

The sample program prints the summary statisctics to standard output:


           ByGroupInfo
Species     Species_f   _key_
Setosa      Setosa      Setosa
Versicolor  Versicolor  Versicolor
Virginica   Virginica   Virginica

ByGroup1.Summary                                       Descriptive Statistics for IRIS
Species  Analysis Variable      Min      Max   N  Number Missing     Mean  Std Dev.  Std Error
Setosa   SepalLength        43.0000  58.0000  50               0  50.0600    3.5249     0.4985
Setosa   SepalWidth         23.0000  44.0000  50               0  34.2800    3.7906     0.5361
Setosa   PetalLength        10.0000  19.0000  50               0  14.6200    1.7366     0.2456
Setosa   PetalWidth          1.0000   6.0000  50               0   2.4600    1.0539     0.1490

ByGroup2.Summary                                         Descriptive Statistics for IRIS
Species     Analysis Variable      Min      Max   N  Number Missing     Mean  Std Dev.  Std Error
Versicolor  SepalLength        49.0000  70.0000  50               0  59.3600    5.1617     0.7300
Versicolor  SepalWidth         20.0000  34.0000  50               0  27.7000    3.1380     0.4438
Versicolor  PetalLength        30.0000  51.0000  50               0  42.6000    4.6991     0.6646
Versicolor  PetalWidth         10.0000  18.0000  50               0  13.2600    1.9775     0.2797

ByGroup3.Summary                                        Descriptive Statistics for IRIS
Species    Analysis Variable      Min      Max   N  Number Missing     Mean  Std Dev.  Std Error
Virginica  SepalLength        49.0000  79.0000  50               0  65.8800    6.3588     0.8993
Virginica  SepalWidth         22.0000  38.0000  50               0  29.7400    3.2250     0.4561
Virginica  PetalLength        45.0000  69.0000  50               0  55.5200    5.5189     0.7805
Virginica  PetalWidth         14.0000  25.0000  50               0  20.2600    2.7465     0.3884

Back to Top