SAS is not “A” programming language. Instead, it’s a collection of multiple programming languages, which includes Data Step, DS2, Macro, SQL, IML, Groovy and such. The beauty is that such programming techniques can be interacted by each other. Thus, in recent years we have Proc Lua joins SAS programming family; it offers a totally different programming style and flavor like a data structure, for loop, external packages support and other modern scripting language features.
SAS introduced Lua to its Base module since version 9.4 which will extend SAS programming functionality. Lua which is pronounced as ‘lu-aa’ is scripting language like Python. It is very popular in gaming industry and recent years it has gained popularity in machine learning world which is used in library Torch 7. Lua uses simple syntax, runs fast computations, and automatically manages memory allocation.
Thus, Lua programming language is an embeddable scripting language that runs on any platform that has a standard C compiler. This includes all versions of UNIX, Windows, and mobile operating systems, including Android, iOS, and others. The LUA procedure enables you to run statements from the Lua programming language within SAS code. One can submit Lua statements from an external Lua script, or enter Lua statements directly in SAS code.
PROC LUA enables you to perform these tasks:
- run Lua code within a SAS session
- call most SAS functions within Lua statements
- call PROC FCMP functions within Lua statements
- submit SAS code from Lua
- call CAS actions
In this paper, several examples will be presented to represent how Proc Lua can add value to SAS programmer’s toolbox. I suppose taking even a little bite of Proc Lua can make one more versatile SAS programmer
1. Hey !! Do You Know Proc LUA?
ConSPIC 2018
1
By :– Dr.Sangram K. Parbhane
2. The opinions in this presentation are those of the presenter and
may not necessarily reflect the views of Quartesian, ConSPIC.
ConSPIC 2018
Disclaimer
2
5. Introduction to Proc Lua
Understanding Basics of Lua Programming
How Proc Lua differs over SAS Macros
Limitations and Final Thoughts
Conclusion
ConSPIC 2018
Contents
5
6. What is Lua ?
It's an embeddable scripting language that is often used as a way to add
user extensions to robust software applications.
Created by Roberto Lerusalimschy in Brazil
ConSPIC 2018
Pronounced LOO-ah means "Moon" in Portuguese
The Lua 5.2 run-time environment is embedded within SAS
Popular choice in game development
Open-source software
6
6
7. Why LUA ?..
The need for an alternate to the SAS Macro Language was first felt
by various solutions group within SAS.
It was therefore felt that there was a strong need for a modern
scripting language in addition to the SAS platform. Since the
purpose of Lua is to script C-based software and because SAS is
written in C, Lua was a great fit for SAS.
Lua provides for Eclipse plug-ins and provides more detailed
debugging information.
7ConSPIC 2018
8. How to run Lua In SAS ?...
PROC LUA runs the Lua virtual machine inside the SAS process to offer seamless
integration of Lua with SAS.
Can execute Lua code by embedding it within a SUBMIT/ENDSUBMIT block in the
PROC LUA invocation
Code Output
The preceding program, prints the 'Hello from Lua!' message to the SAS log. PROC
LUA routes output from Lua’s print function to the SAS log
8ConSPIC 2018
10. Lua Basics
In Lua, you usually declare a variable by using a local statement.
local pi = 3.14 -- Declare a numeric variable
local guest = "Veronica“ -- Declare a string variable
Using the local keyword is recommended because omitting it results in the
variable being declared to be global in scope.
The preceding program, prints the 'Hello from Lua!' message to the SAS log.
PROC LUA routes output from Lua’s print function to the SAS log
10ConSPIC 2018
11. Reading & Writing SAS datasets
To read values from a SAS data set, you use the sas.open function,
which returns a handle that provides functions to read
observations, retrieve values of data set variables, and release the
handle.
If you forget to call sas.close()
after using the data set, you
will see a warning like the
following printed to SAS log
when the data set handle goes
out of scope:
11ConSPIC 2018
12. Defining new SAS dataset from scratch in Lua
The sas.new_table function enables you to define a new (empty)
data set. You provide the name of the new data set, and you
provide a Lua table that is set up as an array of Lua tables, each of
which contains the attributes of one data set variable. For example,
the following code creates a new SAS data set called Work.Status
that contains three variables, node, status, and datetime:
.
12ConSPIC 2018
13. Calling SAS functions from Lua
When PROC LUA initializes the Lua state, it creates a special
global Lua table called sas. This table is always available to your
Lua programs.
To Use SAS functions just add prefix sas. to their function names
The MIN function in SAS becomes sas.min in Lua,
The TRIM function in SAS becomes sas.trim in Lua,
Function arguments are declared within the set of parentheses that
precedes the function body, and they are positional only.
13ConSPIC 2018
14. Calling FCMP Functions
We can also call functions that are created by the FCMP procedure. To use FCMP
functions from Lua, simply define them as you normally would by using PROC
FCMP, and then set the CMPLIB= system option.
14ConSPIC 2018
15. Difference between SAS Macros & LUA
USE OF SEMICOLON - The Lua syntax does not mandate that
every valid statement end with a semicolon.
In contrast, the SAS Macro Language follows the SAS
statement syntax in the sense that every valid statement must
end with a semicolon.
A long-time Lua user might be in the habit of forgetting to do
so, which might lead to additional efforts in debugging later.
15ConSPIC 2018
16. Error Reporting
Lua syntax errors are reported with the line number where error has encountered
Errors that are reported by the SAS macro processor
So when debug your programs for large programs that involves thousands of lines of
code..The result of macro expansion is a set of SAS statements that might not
correspond, line-by-line, to the statements in your original source file. Thus, when errors
that are reported in the SAS log refer to line numbers, those line numbers are often not
useful to you. On the other hand, Lua reports back line numbers that you can relate to the
lines in your source file—so you can quickly locate the problem area and address the
issue.
16ConSPIC 2018
17. Syntax Highlighting
Text editors and IDEs such as Eclipse recognize Lua syntax and support syntax error
highlighting.
17ConSPIC 2018
18. Arithmetic
In the SAS macro language, it requires to use the %EVAL macro to indicate where a text
expression should be interpreted numerically.
In Lua numeric data types are supported directly.
To increment a counter variable in the SAS macro language, you need to write the
following:
In SAS Macros
In Proc Lua
18ConSPIC 2018
19. SAS options Management
SAS options are global. It’s designed in this way for good purpose. But
sometimes you just want to evoke a SAS option but are too lazy to close it at
the end. Proc Lua is good to have in this case.
The trick is the options inside the SAS.SUBMIT blocks are pretty local; it will
not be inherited by the outside programs.
19ConSPIC 2018
20. Macro Alternatives (Wrapper programs)
Lets take us the example of wrapper programs :to check if a dataset exists
By Data Step by using SAS external file functions like fexist:
20ConSPIC 2018
21. Macro Alternatives (Wrapper programs)
Same syntax can be written in Proc Lua with more elegant way and aesthetically preferable
and also is easy to debug
21ConSPIC 2018
22. Which External files SAS doesn’t support?
SAS has access to wide range of external files, but lacks support to JSON(used in
internet exchange) or HDF5 file formats but Proc Lua includes packages to read
JSON, HDF5 and other data formats.
22ConSPIC 2018
23. When SAS Macro Language is Better Choice?
Case Insensitivity - Both uppercase and lowercase keywords can be used
alternatingly in Macros while uppercasing will produce an error in Proc Lua.
23ConSPIC 2018
24. When SAS Macro Language is Better Choice? Continued…
Lua is not integrated with the SAS tokenizer. Therefore, Lua code cannot
be used in open code and neither can it be used interchangeably with SAS
code.
24
Integration with SAS tokenizer
Familiarity with SAS Macros – Programmers have become
comfortable coding with the Macro Language but the same cannot
be said for Lua, which is in itself a big drawback.
ConSPIC 2018
25. Limitations of Proc Lua
Doesn’t port any
External libraries
can only import
external libraries
written by pure Lua
Currently only
supports Lua 5.1
Doesn’t
support Lua IO
module &
Multithreading
25ConSPIC 2018
26. Final Thoughts
SAS is not “A” programming language,
It’s a collection of multiple programming languages, which includes
Data Step, DS2, Macro, SQL, IML, Groovy and such.
Proc Lua offers a totally different programming style and flavor
like a data structure, for loop, external packages support
and other modern scripting language features.
I believe taking even a little bite of Lua can make a more versatile SAS
programmer.
26ConSPIC 2018
27. Conclusion
The LUA procedure offers a new approach to writing SAS
code by enabling you to use Lua to orchestrate your PROC and
DATA steps.
This difference is especially noticeable when you work with
larger and more complex programs that can be very difficult
to implement using only the SAS macro language for the control
logic.
The fact that PROC LUA and the SAS macro language can work
together lets you decide how much of one approach versus the
other to adopt.
27ConSPIC 2018