BookmarkSubscribeRSS Feed
DLangley
Fluorite | Level 6

Hello.

 

I am trying to find out how to use the include statement to run Lua code that then terminates at the end to free up all of the memory.

This is the sort of thing that can be done via the call to proc lua as in this example.

 

proc lua terminate;
submit;

print("Hello World")

endsubmit;
run;

But can this be done through the include statement as below?

%include "&filePath.\helloWorld.lua";
8 REPLIES 8
ChrisNZ
Tourmaline | Level 20

Seems to work.

data _null_;
  file "&wdir\helloWorld.lua"; 
  put 'submit;';
  put 'print("Hello World")';
  put 'endsubmit;';
run;

proc lua terminate;
%include "&wdir\helloWorld.lua";
run;

46 proc lua terminate;
47 %include "&wdir\helloWorld.lua";
NOTE: %INCLUDE (level 1) file K:\SASWORK\_TD19356_NZ8037SPSAS2003_\Prc2\helloWorld.lua is file
K:\SASWORK\_TD19356_NZ8037SPSAS2003_\Prc2\helloWorld.lua.
48 +submit;
49 +print("Hello World")
50 +endsubmit;
NOTE: %INCLUDE (level 1) ending.
51 run;

NOTE: PROC LUA is an experimental procedure.
NOTE: Lua initialized.
Hello World

NOTE: Lua terminated.
NOTE: PROCEDURE LUA used (Total process time):
real time 0.02 seconds
user cpu time 0.00 seconds
system cpu time 0.01 seconds
memory 711.00k
OS Memory 28516.00k
Timestamp 06/05/2021 10:21:08 PM
Step Count 1063 Switch Count 50

 

DLangley
Fluorite | Level 6

Thank you Chris.

 

Whilst that does work as part of an include statement it isn't quite the solution I was hoping for. I'll add more context in the hopes of getting there and thank you very much for your time already.

 

Later versions of SAS (9.4M+??) allow you to call pure lua code with just an include statement and without the proc lua wrapper. I think this is a wonderful innovation.

 

/* generate a lua file containing only lua code */
data _null_;
  file "&wdir\helloWorld.lua"; 
  put 'print("Hello World")';
run;

/* this runs the lua code by creating a lua instance */
%include "&wdir\helloWorld.lua";
/* this then resumes the instance */
%include "&wdir\helloWorld.lua";
/* this code would terminate the instance */
proc lua terminate;
run;
/* this code starts a new instance */
%include "&wdir\helloWorld.lua";

My hope is that there is a system option or something that will enable the lua code called by the include statement to terminate. I tried seeing if there was something that could go in the include statement such as

 

%include "&wdir\helloWorld.lua" terminate;

But that does not work.

Tom
Super User Tom
Super User

I don't think your example code will work at all.

You have essentially asked SAS to run this code:

data _null_;
  file "&wdir\helloWorld.lua"; 
  put 'print("Hello World")';
run;
print("Hello World")
print("Hello World")
proc lua terminate;
run;
print("Hello World")

That does not look like valid SAS code to me.

Sounds like you want to run:

proc lua;
submit;
print("Hello World")
print("Hello World")
endsubmit;
submit;
print("Hello World")
endsubmit;
run;

Does that work? 

If so then replace the LUA print() statements with the macro %INCLUDE statements.

DLangley
Fluorite | Level 6

Hi Tom.

 

That was my thought at first as well but according to https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p07wjwkog4ia54n1b7586zlzmxwe.htm#n0zo27q...

 

You can also run external Lua scripts (*.lua or *.luc files) by using the %INCLUDE statement in a SAS session. For example, to run the Lua script abc.luc, enter the following line in your SAS program:

 

%include "./tmp/abc.luc";

 

 

Which I tested and does work in SAS Studio at least.

 

83   /* this runs the lua code by creating a lua instance */
84   %include "helloWorld.lua";
NOTE: Lua initialized.
Hello World
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
      
93    /* this then resumes the instance */
94    %include "helloWorld.lua";
NOTE: Resuming Lua state from previous PROC LUA invocation.
Hello World
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
103   /* this code would terminate the instance */
104   proc lua terminate;
105   run;
NOTE: Resuming Lua state from previous PROC LUA invocation.
NOTE: TKLua terminated.
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
106   /* this code starts a new instance */
107   %include "helloWorld.lua";
NOTE: Lua initialized.
Hello World
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
116   
117   
118 

(I modified the include statement to remove the path but other than that this is a copy of the log)

Tom
Super User Tom
Super User

So the question is about this special behavior that %INCLUDE is doing when the name of the text you are including happens to end with '.lua'?

 

What are the SAS log messages when you run the 

proc lua terminate; run;

in a SAS session where you have never tried to run any LUA code?

 

When you have two %INCLUDEs of lua code next to each other is there some memory of the state of the LUA process between them?  For example can you reference values defined in the first include in the code of the second include?

 

Is there some LUA command that means get a clean slate that you could run?

DLangley
Fluorite | Level 6
82   proc lua terminate;
83   run;
NOTE: Lua initialized.
NOTE: TKLua terminated.
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.15 seconds
      cpu time            0.00 seconds
      
84   proc lua terminate;
85   run;
NOTE: Lua initialized.
NOTE: TKLua terminated.
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
86   proc lua terminate;
87   run;
NOTE: Lua initialized.
NOTE: TKLua terminated.
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds
      
88   proc lua terminate;
89   run;
NOTE: Lua initialized.
NOTE: TKLua terminated.
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
90   
91   
82   proc lua;
83   submit;
84   
85   local x = "Hello Local"
86   y = "Hello Global"
87   
88   endsubmit;
89   run;
NOTE: Resuming Lua state from previous PROC LUA invocation.
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      
90   
91   
92   proc lua;
93   submit;
94   
95   print(x)
96   print(y)
97   
98   endsubmit;
99   run;
NOTE: Resuming Lua state from previous PROC LUA invocation.
nil
Hello Global
NOTE: PROCEDURE LUA used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
      

I think we mark everything as local anyway so it might be completely fine. I had never actually tested that so thank you.

Tom
Super User Tom
Super User

I know next to nothing about LUA, but a quick scan of manual shows it does have a local construct.  So perhaps the solution is to have your %INCLUDE lua programs do everything in a local that disappears when they finish?

 

Something like:

do
  print("Hello World")
end
DLangley
Fluorite | Level 6

Hi Tom.

 

If you run and terminate by itself it does not cause any problems.

Yes Lua code can be passed between instances if they are not terminated but that memory is held the entire time which is not ideal.

 

I suppose having everything as fully local might be a solution but that would require a lot of rewriting for us so we won't be going ahead with that.

 

Thank you though for this.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 644 views
  • 3 likes
  • 3 in conversation