← Booker Gong
Saspark: a SAS to Python/Scala Transpiler
Created a trans-compiler from SAS Data-steps transformation to Spark Dataframe API, generating well-organized Python or Scala code over Spark API with Scala & parser combinator, presented at Spark Summit East
Saspark is a Trans-Compiler from SAS Dataset Transformation Language to correpsonding structures in Spark Dataframes with Python, Scala. It has three main components:
- Compiler: wriiten in Scala with parser combinator fastparse, grammar analyzer, a shared abstract syntax tree, a structure visualizer and two generators to target language syntaxes
- Backend: written in Scala with Akka Http Server and a few REST APIs
- Frontend: Developed with React Ecosytem with bootstrap, marked, highlight.js, served by simple Node/Python server

Example
Variable
- Table Reference vs Variable
- In SAS data step, all variables are inside SAS dataset, thus new variable and table column are not distinguished
- In Spark transformation, however, we need to interpret if a variable is from source table before runtime.
- In order to distinguish these, we keep track of all new varaible created, and regard all the rest referenece as table columns
- Table
- In Python, table columns is called via
row.xxx - In Scala, all column type need to be declared explicitly, like
row.getAs[String]("xxx")
- In Python, table columns is called via
- Variable Scopes
- In Python and SAS, variable declaration is not required, and global scope is always available
- In Scala, situation become more complicated as declaration & type inference comes in
- Top level varaibles can be directly created via
var - Variables created in the inner scopes, corresponding to SAS's IF/DO block & Python's Indentation, are also required in outer scopes.
Thus we have to create them and declare their types explicitly in Scala as well, like
var someVar : Int
- Top level varaibles can be directly created via
IF column=1 THEN b=1;
ELSE b=2;
y=0;
IF column=1 THEN DO; y="ONE"; END;
ELSE y="NOT ONE";
//[Any] must be replaced with actual type for scala to compile correctly
val column = row.getAs[Any]("column")
var b : Any
if (column == 1) b = 1
else b = 2
/*Local variables are promoted to global level in Scala transformation*/
var y = 0
if (column == 1) y = "ONE"
else y = "NOT ONE"
IF Statement
- If block is regularly used in most column mapping logic
IF expression THEN statement; <ELSE statement;>- In assignment logic based on a single variable, optimizations that generates a python dictionary & scala case will be applied
IF a=7 THEN r = "seven";
ELSE IF a=8 THEN r = "eight";
ELSE r = "other";
r = {
7: "seven",
8: "eight",
}.get(a) or "other"
r = a match {
case 7 => "seven"
case 8 => "eight"
case _ => "other"
}
DO Statement
- Do Statement create a do group between
DOandEND - It allows multiple statement execute together
- It is usually used within IF-THEN-ELSE block
- In Scala, do statement creates a closure (curly braces block)
- In Python, indentation level already act as a closure, so no additional syntax needed
IF isNumber THEN DO; x=0; x=x+1; END;
ELSE x="number";
if row.isNumber:
x = 0
x = x + 1
else:
x = "number"
if (isNumber) {
x = 0
x = x + 1
}
else x = "number"
Comparison Expression
- In SAS and Python, continous comparison operator is handled separated
- However in static typed language like Scala,
0<x<1will fail, as0<xyields a boolean, which is not comparable with an Integer type - In Scala, a chain of comparisons connected with
&&will generated instead