<?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 Juletip #9 – Dancing round the tree (looping) with Lua instead of Macro in SAS Community Nordic</title>
    <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-9-Dancing-round-the-tree-looping-with-Lua-instead-of/m-p/785090#M365</link>
    <description>&lt;P&gt;Many SAS users at some point need to run almost the same block of SAS code repeatedly, with only small changes between each run. Enter the &lt;EM&gt;SAS Macro language&lt;/EM&gt; and the&amp;nbsp;&lt;EM&gt;%DO Loop&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;EM&gt;loopreport&lt;/EM&gt; Macro example below reads a "control table" sashelp.nvst1, and for each row performs a PROC MEANS on the sashelp.orsales table - using the &lt;EM&gt;Date&lt;/EM&gt; and &lt;EM&gt;Amount&lt;/EM&gt; columns from each row of the control table as filters "inside" each PROC MEANS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loopreport;
    data _null_;
        set sashelp.nvst1 end=last;
        call symputx('date'||left(_n_), Date, 'L');
        call symputx('amount'||left(_n_), Amount, 'L');
        if last = 1 then call symputx('endloop', _n_, 'L');
    run;
    %do i=1 %to &amp;amp;endloop;
        proc means data=sashelp.orsales sum mean maxdec=1;
            var Profit;
            where Year = year(&amp;amp;&amp;amp;date&amp;amp;i) and Total_Retail_Price &amp;gt; &amp;amp;&amp;amp;amount&amp;amp;i;
            title "Year: %sysfunc(year(&amp;amp;&amp;amp;date&amp;amp;i))";
        run;
    %end;
%mend loopreport;
%loopreport;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result is a series of PROC MEANS outputs, one for each year, with each PROC MEANS filtered by the appropriate &lt;EM&gt;Date&lt;/EM&gt; and &lt;EM&gt;Amount&lt;/EM&gt; from the control table.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Output of loopreport macro - PROC MEANS output for each year in the control table" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66547iF0E648A24AD306DE/image-size/large?v=v2&amp;amp;px=999" role="button" title="macro loopreport output.PNG" alt="Output of loopreport macro - PROC MEANS output for each year in the control table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Output of loopreport macro - PROC MEANS output for each year in the control table&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Now to the core topic of this post. Since a few years SAS also provides a few other ways to do similar things – now with the programming languages &lt;EM&gt;Lua&lt;/EM&gt; and &lt;EM&gt;Python&lt;/EM&gt;. PROC LUA and PROC PYTHON allows us to call Lua and Python from SAS code and to call SAS from Lua and Python code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is an example of PROC LUA doing the exact same thing as the macro above.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc lua;
submit;
    local dates_and_amounts = sas.read_ds("sashelp.nvst1")
    for i, row in ipairs(dates_and_amounts) do
        local date = row.date
        local amount = row.amount;
        sas.submit[[
            proc means data=sashelp.orsales sum mean maxdec=1;
                var Profit;
                where Year = year(@date@) and Total_Retail_Price &amp;gt; @amount@;
                title "Year: %sysfunc(year(@date@))";
            run;
        ]]
    end
endsubmit;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Submitting this PROC LUA statement produces the exact same output as the &lt;EM&gt;loopreport&lt;/EM&gt; macro.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;(Note that the SAS syntax color highlighting get confused when there is non-SAS code in the editor. This is much better in the new built-in Python editor in SAS Studio on SAS Viya (which also automatically wraps your Python code in a PROC PYTHON call). This is covered in more detail in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13009"&gt;@JeppeDeigaard&lt;/a&gt;'s&amp;nbsp;&lt;A title="Juletip #7" href="https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-7-quot-As-many-code-languages-you-know-as-many-times-you/m-p/784489#M362" target="_blank" rel="noopener"&gt;Juletip #7&lt;/A&gt; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35019"&gt;@CecilyHoffritz&lt;/a&gt;'s &lt;A title="Easy collaboration is key when building data pipelines!" href="https://www.linkedin.com/pulse/easy-collaboration-key-when-building-data-pipelines-cecily-hoffritz/" target="_self"&gt;LinkedIn piece&lt;/A&gt;.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I won't go into a discussion about which variant is "better". My intention here is only to show there are options and it's really not that difficult to switch between. Loop with Macro, Lua or Python… your choice!&lt;/P&gt;</description>
    <pubDate>Thu, 09 Dec 2021 08:49:33 GMT</pubDate>
    <dc:creator>ErikStrömgren</dc:creator>
    <dc:date>2021-12-09T08:49:33Z</dc:date>
    <item>
      <title>Juletip #9 – Dancing round the tree (looping) with Lua instead of Macro</title>
      <link>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-9-Dancing-round-the-tree-looping-with-Lua-instead-of/m-p/785090#M365</link>
      <description>&lt;P&gt;Many SAS users at some point need to run almost the same block of SAS code repeatedly, with only small changes between each run. Enter the &lt;EM&gt;SAS Macro language&lt;/EM&gt; and the&amp;nbsp;&lt;EM&gt;%DO Loop&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;EM&gt;loopreport&lt;/EM&gt; Macro example below reads a "control table" sashelp.nvst1, and for each row performs a PROC MEANS on the sashelp.orsales table - using the &lt;EM&gt;Date&lt;/EM&gt; and &lt;EM&gt;Amount&lt;/EM&gt; columns from each row of the control table as filters "inside" each PROC MEANS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loopreport;
    data _null_;
        set sashelp.nvst1 end=last;
        call symputx('date'||left(_n_), Date, 'L');
        call symputx('amount'||left(_n_), Amount, 'L');
        if last = 1 then call symputx('endloop', _n_, 'L');
    run;
    %do i=1 %to &amp;amp;endloop;
        proc means data=sashelp.orsales sum mean maxdec=1;
            var Profit;
            where Year = year(&amp;amp;&amp;amp;date&amp;amp;i) and Total_Retail_Price &amp;gt; &amp;amp;&amp;amp;amount&amp;amp;i;
            title "Year: %sysfunc(year(&amp;amp;&amp;amp;date&amp;amp;i))";
        run;
    %end;
%mend loopreport;
%loopreport;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result is a series of PROC MEANS outputs, one for each year, with each PROC MEANS filtered by the appropriate &lt;EM&gt;Date&lt;/EM&gt; and &lt;EM&gt;Amount&lt;/EM&gt; from the control table.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Output of loopreport macro - PROC MEANS output for each year in the control table" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66547iF0E648A24AD306DE/image-size/large?v=v2&amp;amp;px=999" role="button" title="macro loopreport output.PNG" alt="Output of loopreport macro - PROC MEANS output for each year in the control table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Output of loopreport macro - PROC MEANS output for each year in the control table&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Now to the core topic of this post. Since a few years SAS also provides a few other ways to do similar things – now with the programming languages &lt;EM&gt;Lua&lt;/EM&gt; and &lt;EM&gt;Python&lt;/EM&gt;. PROC LUA and PROC PYTHON allows us to call Lua and Python from SAS code and to call SAS from Lua and Python code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is an example of PROC LUA doing the exact same thing as the macro above.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc lua;
submit;
    local dates_and_amounts = sas.read_ds("sashelp.nvst1")
    for i, row in ipairs(dates_and_amounts) do
        local date = row.date
        local amount = row.amount;
        sas.submit[[
            proc means data=sashelp.orsales sum mean maxdec=1;
                var Profit;
                where Year = year(@date@) and Total_Retail_Price &amp;gt; @amount@;
                title "Year: %sysfunc(year(@date@))";
            run;
        ]]
    end
endsubmit;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Submitting this PROC LUA statement produces the exact same output as the &lt;EM&gt;loopreport&lt;/EM&gt; macro.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;(Note that the SAS syntax color highlighting get confused when there is non-SAS code in the editor. This is much better in the new built-in Python editor in SAS Studio on SAS Viya (which also automatically wraps your Python code in a PROC PYTHON call). This is covered in more detail in&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13009"&gt;@JeppeDeigaard&lt;/a&gt;'s&amp;nbsp;&lt;A title="Juletip #7" href="https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-7-quot-As-many-code-languages-you-know-as-many-times-you/m-p/784489#M362" target="_blank" rel="noopener"&gt;Juletip #7&lt;/A&gt; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35019"&gt;@CecilyHoffritz&lt;/a&gt;'s &lt;A title="Easy collaboration is key when building data pipelines!" href="https://www.linkedin.com/pulse/easy-collaboration-key-when-building-data-pipelines-cecily-hoffritz/" target="_self"&gt;LinkedIn piece&lt;/A&gt;.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I won't go into a discussion about which variant is "better". My intention here is only to show there are options and it's really not that difficult to switch between. Loop with Macro, Lua or Python… your choice!&lt;/P&gt;</description>
      <pubDate>Thu, 09 Dec 2021 08:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Community-Nordic/Juletip-9-Dancing-round-the-tree-looping-with-Lua-instead-of/m-p/785090#M365</guid>
      <dc:creator>ErikStrömgren</dc:creator>
      <dc:date>2021-12-09T08:49:33Z</dc:date>
    </item>
  </channel>
</rss>

