Using Lua within your SAS programs


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 ;
-- 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;
   else print "WORK.SAMPLE already exists"

First run:

NOTE: Lua initialized.
Creating new WORK.SAMPLE
    data work.sample;
      set sashelp.class;

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!

Tags Lua SAS 9.4

About Author

Chris Hemedinger

Director, SAS User Engagement

+Chris Hemedinger is the Director of SAS User Engagement, which includes our SAS Communities and SAS User Groups. Since 1993, Chris has worked for SAS as an author, a software developer, an R&D manager and a consultant. Inexplicably, Chris is still coasting on the limited fame he earned as an author of SAS For Dummies


  1. 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.

  2. 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


    • Chris Hemedinger
      Chris Hemedinger on

      Now if we could just get PROC WOOLYBULLY implemented, we'll have added some serious rocking code.

  3. 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.

    • Chris Hemedinger
      Chris Hemedinger on

      Dave, yes - it was smuggled into M2 as well and marked Experimental. But it is much improved (read: working) in M3, officially.

    • Chris Hemedinger
      Chris Hemedinger on

      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.

    • Chris Hemedinger
      Chris Hemedinger on

      I haven't had a chance to test, but according to the internal documentation I've seen: Yes, it should work on z/OS.

  4. Pingback: What were your #FirstSevenLanguages? - The SAS Dummy

  5. Peter Johansson on

    I absolutley love Proc Lua!
    But when checking the package.loaded (or _G) I find functions the crash my SAS session? Like 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!

    • Chris Hemedinger
      Chris Hemedinger on

      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.

      • Peter Johansson on

        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?

  6. 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.

    • Chris Hemedinger
      Chris Hemedinger on


      This embedded version of Lua does have some limitations, including the ability to call out to the OS shell. From Paul's paper:

      Currently, PROC LUA does not support all the modules that Lua provides. For example, PROC LUA does not currently
      support the os module, which offers direct support to the operating system. Lua’s language features for multithreading
      have also been disabled.
      The Lua community has created a large set of Lua modules that are available online. However, you might find that
      some third-party Lua modules do not work in PROC LUA. In particular, if they were written in C or involve custom-built
      C libraries, PROC LUA cannot run them. Lua code that is written for a Lua version other than 5.2 might need some
      modifications to work in PROC LUA.

  7. Pingback: How to code in Python with SAS 9.4 - SAS Users

  8. Pingback: Using the Lua programming language within Base SAS® - SAS Users

Back to Top