<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using SAS macrovars in LUA in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938412#M83597</link>
    <description>&lt;P&gt;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&amp;nbsp; macro variable in the same scope?&lt;/P&gt;</description>
    <pubDate>Tue, 06 Aug 2024 19:40:02 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-08-06T19:40:02Z</dc:date>
    <item>
      <title>Using SAS macrovars in LUA</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938338#M83594</link>
      <description>&lt;P&gt;I know You can use sas.symget to get a macrovar from SAS in Your LUA code.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%let myVar = TEST;&lt;/P&gt;
&lt;P&gt;proc lua;&lt;/P&gt;
&lt;P&gt;submit;&lt;/P&gt;
&lt;P&gt;function mv2lua()&lt;BR /&gt;&amp;nbsp; &amp;nbsp;print("Creating LUA variables for the following SAS macro variables...")&lt;BR /&gt;&amp;nbsp; &amp;nbsp;ds = sas.open("sashelp.vmacro","I")&lt;BR /&gt;&amp;nbsp; &amp;nbsp;while (sas.next(ds) ~= nil) do&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; name = sas.get_value(ds, "name")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; value = sas.get_value(ds, "value")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; print(name .. " - " .. value .. " -&amp;gt; mv_" .. name)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; s = "mv_"..name.."=value" -- code for assigning value to new LUA variable&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; load(s)() -- execute the code thereby creating the variable&lt;BR /&gt;&amp;nbsp; &amp;nbsp;end&lt;BR /&gt;&amp;nbsp; &amp;nbsp;rc = sas.close(ds)&lt;BR /&gt;end&lt;BR /&gt;&lt;BR /&gt;mv2lua()&lt;/P&gt;
&lt;P&gt;print(mv_SYSUSERID)&lt;/P&gt;
&lt;P&gt;print(mv_myVar)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;endsubmit;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2024 10:43:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938338#M83594</guid>
      <dc:creator>bl_jyskebank_dk</dc:creator>
      <dc:date>2024-08-06T10:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS macrovars in LUA</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938352#M83595</link>
      <description>&lt;P&gt;Note that you logic will only work for macro variables with values that are less than 200 characters long.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2024 13:12:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938352#M83595</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-06T13:12:12Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS macrovars in LUA</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938404#M83596</link>
      <description>&lt;P&gt;But it's easy to fix:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options ls=max;

%let myVar = TEST+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++test;


%put &amp;amp;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 .. " -&amp;gt; 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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Aug 2024 19:11:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938404#M83596</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-08-06T19:11:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS macrovars in LUA</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938412#M83597</link>
      <description>&lt;P&gt;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&amp;nbsp; macro variable in the same scope?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2024 19:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938412#M83597</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-06T19:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using SAS macrovars in LUA</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938445#M83598</link>
      <description>&lt;P&gt;just for fun;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you can find which value is at which nesting level, even for recursive case&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;resetline;

/* "Recursive loop" */

%macro R2(s,e,b);
%if %sysevalf(&amp;amp;s. &amp;lt;= &amp;amp;e.) %then 
%do;
  %local _SYSMEXECDEPTH;
  %let _SYSMEXECDEPTH=%sysfunc(abs(%SYSMEXECDEPTH),z10.) ;;
  %put &amp;amp;_SYSMEXECDEPTH. &amp;amp;=s. &amp;amp;=e. &amp;amp;=b.;
  data _null_&amp;amp;_SYSMEXECDEPTH.;
    SET sashelp.vmacro (where=(scope NE 'AUTOMATIC'));
    depth = &amp;amp;_SYSMEXECDEPTH. ;
    ord+1;
  run;
  data _null_;
  run;
  %R2(%sysevalf(&amp;amp;s.+&amp;amp;b.)   /* change in s */
     ,%sysevalf(&amp;amp;e.*1.001) /* change in e */
     ,&amp;amp;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2024 23:07:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Using-SAS-macrovars-in-LUA/m-p/938445#M83598</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2024-08-06T23:07:02Z</dc:date>
    </item>
  </channel>
</rss>

