With apologies to this candy advertisement from the 1980s:
"Hey, you got your Lua in my SAS program."
"You got your SAS code in my Lua program!"
Announcer: "PROC LUA: Two great programming languages that program great together!"
What is Lua? It's an embeddable scripting language that is often used as a way to add user extensions to robust software applications. Lua has been embedded into SAS for some time already, as it's the basis for new ODS destinations like EXCEL and POWERPOINT. But SAS users haven't had a way to access it.
With SAS 9.4 Maintenance 3 (released July 2015), you can now run Lua code in the new LUA procedure. And from within that Lua code, you can exchange data with SAS and call SAS functions and submit SAS statements. (Running SAS within Lua within SAS -- it's just like Inception.)
Paul Tomas, the developer for PROC LUA, presented a demo of the feature and its usefulness in a recent SAS Tech Talk:
Paul also wrote a paper for SAS Global Forum 2015: Driving SAS with Lua.
Like many innovations that find their way into customer-facing features, this new item was added to help SAS R&D complete work for a SAS product (specifically, the new version of SAS Forecast Server). But the general technique was so useful that we decided to add it into Base SAS as a way for you to integrate Lua logic.
PROC LUA can be an alternative to the SAS macro language for injecting logical control into your SAS programs. For example, here's a sample program that generates a SAS data set only if the data set doesn't already exist.
proc lua ; submit; -- example of logic control within LUA if not sas.exists("work.sample") then print "Creating new WORK.SAMPLE" sas.submit [[ data work.sample; set sashelp.class; run; ]] else print "WORK.SAMPLE already exists" end endsubmit; run;
NOTE: Lua initialized. Creating new WORK.SAMPLE data work.sample; set sashelp.class; run;
And subsequent runs:
NOTE: Resuming Lua state from previous PROC LUA invocation. WORK.SAMPLE already exists NOTE: PROCEDURE LUA used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Unlike other embedded languages (like PROC GROOVY), Lua runs in the same process as SAS -- and not in a separate virtual machine process like a Java VM. This makes it easy to exchange information such as data and macro variables between your SAS and Lua programming structures.
If you have SAS 9.4M3 and have time to play with PROC LUA, let us know what interesting applications you come up with!
Thanks for sharing Chris. Exciting times for SAS programmers!
Was intrigued by the candy ad relevance. Oh my, what a retro and well chosen ad to show what SAS programmers have ahead.
LUA Luaa, oh no
Sayin' we gotta go, yeah yeah, yeah yeah yeah
Said LUA Luaa, oh baby
Said we gotta go
A fine little output, it waits for me
Catch a codestream across the sea
Sail that code about, all alone
Never know how I did it before
Now if we could just get PROC WOOLYBULLY implemented, we'll have added some serious rocking code.
Very interesting, thanks Chris.
I checked my 9.4 M2 installation (64 bit Linux) and PROC Lua is already there, although it says, cautiously:
NOTE: PROC LUA is an experimental procedure.
Dave, yes - it was smuggled into M2 as well and marked Experimental. But it is much improved (read: working) in M3, officially.
Does this mean that Lualatex can be called like that too?
I don't think so. It looks like LuaTex includes some additional executables plus a collection of Lua files. You can use PROC LUA to run Lua programs that work in Lua 5.2 as long as they don't have additional dependencies on other applications. PROC LUA also does not support Lua statements that access OS commands.
As an alternative, SAS provides a package called StatRep, which uses SAS and the LaTeX typesetting system to create documents with reproducible results.
Does it work in SAS under z/OS?
I haven't had a chance to test, but according to the internal documentation I've seen: Yes, it should work on z/OS.
Pingback: What were your #FirstSevenLanguages? - The SAS Dummy
I absolutley love Proc Lua!
But when checking the package.loaded (or _G) I find functions the crash my SAS session? Like io.read for instance. Is it not supported?
And why is there a loadfile and a load but no loadstring?
Can we look forward to more functionality in comming maintenance releases? :)
I was at the Global Forum this year but I could not find anything about Proc Lua?
Maybe you could have Paul Tomas as a guest on your blog? :)
Super blog btw, Chris!
Peter, thanks for the comments. I think that some of the Lua packages, such as access to the OS, are disabled in PROC Lua. I think the developers didn't want Lua to be an end-run around limitations that a SAS admin can impose to restrict access to the operating system shell (usually in enterprise SAS installations). You would need to use standard SAS language functions for that stuff.
Thank you for the response!
But the OS-package is not loaded into SAS, IO is. Still it does not seem to work? I was hoping this was because these are functions that WILL be supported in later releases?
SAS seems to have an eye for Lua, do you think more functionality will be supported along the line?
Is this a full version of Lua? What I mean by that is can any Lua work within SAS? Or is it a gimped (sandboxed) version of Lua? For example, if I write a Lua extension in C or C++, will I be able to call that Lua script from within SAS? If not, why not? That would seriously limit the functionality and appeal of this feature if that is true.
This embedded version of Lua does have some limitations, including the ability to call out to the OS shell. From Paul's paper:
LUA is great! When can we expect SAS to support LUA 5.3? https://www.lua.org/versions.html
Hi Allan, I'll check around and see if I can learn the plan.
Hi Allan, Just learned that this is *not* planned in the near term. But keep those cards and letters coming.
Pingback: How to code in Python with SAS 9.4 - SAS Users
Pingback: Using the Lua programming language within Base SAS® - SAS Users