<?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 MACRO inside Proc SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243488#M45275</link>
    <description>&lt;P&gt;Thanks, I only wrote it straight out, hadn't tested it, so those things should come up, but I am always forgetting to set lengths. &amp;nbsp;Call execute is helpful in a lot of instances, I use it a lot, in fact most of my programs are metadata generated.&lt;/P&gt;</description>
    <pubDate>Thu, 14 Jan 2016 15:46:34 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-01-14T15:46:34Z</dc:date>
    <item>
      <title>Using MACRO inside Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243353#M45242</link>
      <description>&lt;P&gt;Hi, I'm trying to define a variable list for a proc sql command rather than type each instance out. For example, here's the code breaking out each variable into its own table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table remi.Emp as&lt;BR /&gt;select subregion_ID, year, emp&lt;BR /&gt;from remi.remisas&lt;BR /&gt;where emp is not missing&lt;BR /&gt;order by subregion_ID, year;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;create table remi.r111 as&lt;BR /&gt;select subregion_ID, year, r111&lt;BR /&gt;from remi.remisas&lt;BR /&gt;where r111 is not missing&lt;BR /&gt;order by subregion_ID, year;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;create table remi.r113 as&lt;BR /&gt;select subregion_ID, year, r113&lt;BR /&gt;from remi.remisas&lt;BR /&gt;where r113 is not missing&lt;BR /&gt;order by subregion_ID, year;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to have something more efficient lwould give me the same output as above, but I keep getting errors on this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%let var_list= emp r111 r113&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table remi."&amp;amp;var_list" as&lt;BR /&gt;select subregion_id, year, "&amp;amp;var_list"&lt;BR /&gt;from remi.remisas&lt;BR /&gt;where "&amp;amp;var_list" is not missing&lt;BR /&gt;order by subregion_id, year;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any help!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2016 22:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243353#M45242</guid>
      <dc:creator>prhamilton</dc:creator>
      <dc:date>2016-01-13T22:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using MACRO inside Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243357#M45246</link>
      <description>&lt;P&gt;Your fairly close, things to check:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Does your macro variable need to be in quotes? If it doesn't in regular code it likely doesn't in macro code&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Write it out for one iteration. In this case you need to loop through your variable list. I'm attaching some sample code that loops through a variable list, hopefully you can expand it to your code.&lt;/P&gt;
&lt;P&gt;3. Test your macro first as raw code, second as with a single macro variable, third with the macro variable list, so looping through the macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*This Loops thoough a set of variables where the variables
are separated by "|". Any other delimiter can be used
and specified in the scan function as well*/
 
%macro loop(varlist);
%let i=1;
%do %while (%scan(&amp;amp;varlist, &amp;amp;i, |) ^=%str());
%let var=%scan(&amp;amp;varlist, &amp;amp;i, |); 
%put &amp;amp;var;

*rest of SAS code goes here;
 
*Increment counter;
%let i=%eval(&amp;amp;i+1);
%end;
%mend;
%let temp=a|b|c|d|e;
%loop(&amp;amp;temp);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jan 2016 23:02:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243357#M45246</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-13T23:02:37Z</dc:date>
    </item>
    <item>
      <title>Re: Using MACRO inside Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243432#M45264</link>
      <description>&lt;P&gt;Simple really, the only thing that changes is the one element, so use a datastep do loop and call execute your code:&lt;/P&gt;
&lt;PRE&gt;data _null_;&lt;BR /&gt;  length i $5;
  do i="EMP","R111","R113";
    call execute('proc sql; 
                    create table REMI.'||strip(i)||' as 
                    select  SUBREGION_ID
                            ,YEAR
                            ,'||strip(i)||'
                    from    REMI.REMISAS
                    where   '||strip(i)||' is not null
                    order by SUBREGION_ID
                             ,YEAR;
                  quit;');
  end;
run;&lt;/PRE&gt;
&lt;P&gt;What this will do is create one proc sql for each of the three loop elements. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2016 15:45:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243432#M45264</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-14T15:45:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using MACRO inside Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243480#M45271</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9﻿&lt;/a&gt;: This is a nice technique to implement the first efficiency tip of the&amp;nbsp;&lt;A href="http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#p06jek7471o2zkn16dwjvon3wf9z.htm" target="_blank"&gt;macro documentation&lt;/A&gt;: "&lt;SPAN&gt;use a macro only when necessary." Thank you very much for bringing this to my attention by similar posts in the recent past. (I had not been aware that CALL EXECUTE has been around since SAS 6.07.)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I suggest two minor corrections:&lt;/SPAN&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;Inserting &lt;FONT face="courier new,courier"&gt;length i $4;&lt;/FONT&gt;&amp;nbsp;at the beginning would avoid the truncation of the second and third value of &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; to "R11".&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;The variable name &lt;FONT face="courier new,courier"&gt;EMP&lt;/FONT&gt; in the SELECT statement should be replaced by &lt;FONT face="courier new,courier"&gt;'||strip(i)||'&lt;/FONT&gt;.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Thu, 14 Jan 2016 15:17:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243480#M45271</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-14T15:17:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using MACRO inside Proc SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243488#M45275</link>
      <description>&lt;P&gt;Thanks, I only wrote it straight out, hadn't tested it, so those things should come up, but I am always forgetting to set lengths. &amp;nbsp;Call execute is helpful in a lot of instances, I use it a lot, in fact most of my programs are metadata generated.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2016 15:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-MACRO-inside-Proc-SQL/m-p/243488#M45275</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-14T15:46:34Z</dc:date>
    </item>
  </channel>
</rss>

