I know You can use sas.symget to get a macrovar from SAS in Your LUA code.
But I find et easier to just write LUA var.names when programming rather than using sas.symget in my LUA code so I wrote this litte LUA function that gets all SAS macrovars and transfers them to ordinary LUA vars so that I can use them in my code.
%let myVar = TEST;
proc lua;
submit;
function mv2lua()
print("Creating LUA variables for the following SAS macro variables...")
ds = sas.open("sashelp.vmacro","I")
while (sas.next(ds) ~= nil) do
name = sas.get_value(ds, "name")
value = sas.get_value(ds, "value")
print(name .. " - " .. value .. " -> mv_" .. name)
s = "mv_"..name.."=value" -- code for assigning value to new LUA variable
load(s)() -- execute the code thereby creating the variable
end
rc = sas.close(ds)
end
mv2lua()
print(mv_SYSUSERID)
print(mv_myVar)
endsubmit;
run;
But it's easy to fix:
options ls=max;
%let myVar = TEST+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++test;
%put &myVar.;
data test;
set sashelp.vmacro;
where scope = "GLOBAL";
run;
proc lua;
submit;
function mv2lua()
print("Creating LUA variables for the following SAS macro variables...")
ds = sas.open("sashelp.vmacro","I")
while (sas.next(ds) ~= nil) do
name = sas.get_value(ds, "name")
value = sas.get_value(ds, "value")
offset = sas.get_value(ds, "offset")
print(name .. " - " .. value .. " -> mv_" .. name)
if offset==0 then s = "mv_"..name.."=value" -- code for assigning value to new LUA variable
else s = "mv_"..name.."=mv_"..name.." .. value"
end
-- print("###" .. s)
load(s)() -- execute the code thereby creating the variable
end
rc = sas.close(ds)
end
mv2lua()
print(mv_SYSUSERID)
print(mv_MYVAR)
endsubmit;
run;
Note that you logic will only work for macro variables with values that are less than 200 characters long.
But it's easy to fix:
options ls=max;
%let myVar = TEST+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++test;
%put &myVar.;
data test;
set sashelp.vmacro;
where scope = "GLOBAL";
run;
proc lua;
submit;
function mv2lua()
print("Creating LUA variables for the following SAS macro variables...")
ds = sas.open("sashelp.vmacro","I")
while (sas.next(ds) ~= nil) do
name = sas.get_value(ds, "name")
value = sas.get_value(ds, "value")
offset = sas.get_value(ds, "offset")
print(name .. " - " .. value .. " -> mv_" .. name)
if offset==0 then s = "mv_"..name.."=value" -- code for assigning value to new LUA variable
else s = "mv_"..name.."=mv_"..name.." .. value"
end
-- print("###" .. s)
load(s)() -- execute the code thereby creating the variable
end
rc = sas.close(ds)
end
mv2lua()
print(mv_SYSUSERID)
print(mv_MYVAR)
endsubmit;
run;
And the next wrinkle is what if you run it inside a macro call so that there are different SCOPEs present in the VMACRO dataset? What if you run in inside a recursive macro so there are multiple copies of the same macro variable in the same scope?
just for fun;
you can find which value is at which nesting level, even for recursive case
resetline;
/* "Recursive loop" */
%macro R2(s,e,b);
%if %sysevalf(&s. <= &e.) %then
%do;
%local _SYSMEXECDEPTH;
%let _SYSMEXECDEPTH=%sysfunc(abs(%SYSMEXECDEPTH),z10.) ;;
%put &_SYSMEXECDEPTH. &=s. &=e. &=b.;
data _null_&_SYSMEXECDEPTH.;
SET sashelp.vmacro (where=(scope NE 'AUTOMATIC'));
depth = &_SYSMEXECDEPTH. ;
ord+1;
run;
data _null_;
run;
%R2(%sysevalf(&s.+&b.) /* change in s */
,%sysevalf(&e.*1.001) /* change in e */
,&b.) /* keep b unchanged */
%end;
%mend R2;
options mprint;
%put %R2(0,2,0.25);
data all/view=all;
set _NULL_:;
run;
proc sql;
create table variablesScope as
select min(ord) as ord
,scope
,name
,offset
,value
,x.m-count(distinct depth) as depth
from all,(select max(depth)+1 as m from all) as x
group by 2,3,4,5
order by 1,2,3,4,5
;
quit;
:
Bart
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.