BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
bl_jyskebank_dk
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

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;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Note that you logic will only work for macro variables with values that are less than 200 characters long.

yabwon
Onyx | Level 15

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;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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?

yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Call for Content

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!

Submit your idea!

What is Bayesian Analysis?

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.

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
  • 4 replies
  • 607 views
  • 4 likes
  • 3 in conversation