Skip to main content
Version: Next

How to Parse SAS Code

This is the most basic of examples: it shows how to parse some SAS code.

Add Dependencies

repositories {
mavenLocal()
mavenCentral()
flatDir {
dirs("deps")
}
}

dependencies {
implementation(files("deps/sas-parser-with-dependencies-1.6.5-all.jar"))
}

Parse a file

This code shows how to create a SAS parser object and use it parse a file. It expects in input two File(s): a file containing the SAS code and another one the license.

private fun processSASFile(sasFile: File, license: File) {
println("== Processing SAS file " + sasFile.getPath() + " ==\n")
if (!sasFile.exists() && !sasFile.isFile()) {
println("File not found or not a file, skipping")
return
}
// register license
LicenseManager.registerLicense(license)
// create parser
val sasLanguage = SASLanguage()
// set to automatically parse SQL code
sasLanguage.parseNativeSQL = true

val result = sasLanguage.parse(sasFile)
if (!result.issues.isEmpty()) {
println("Issues in file: " + result.issues.size)
for (issue in result.issues) {
println(" - " + issue)
}
}

if (result.root == null) {
println("Compilation unit not built, skipping")
return
}

val sf: SourceFile = result.root!!

if (sf.getStatementsAndDeclarations().isEmpty()) {
println("No statements")
} else {
println(
"Number of statements and declarations: " + sf.getStatementsAndDeclarations().size()
)
}
}

You can use the method we just created very easily.

fun main() {
processSASFile(File("examples/SAS/all-the-code.sas"), File("licenses/strumenta.SAS.license"))
}