<?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: Looping macro variables to create table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295855#M61874</link>
    <description>&lt;P&gt;Take a look at the macro examples, one should help you get started.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right now it's not clear what you're trying to do...the table vendortree will be replaces/overwritten in each iteration.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Sep 2016 14:33:41 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-09-01T14:33:41Z</dc:date>
    <item>
      <title>Looping macro variables to create table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295854#M61873</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop;
%do i=1 %to 15
%let var&amp;amp;i= 'parent_level&amp;amp;i_vender_cde';
proc sql;
create table vendortree as
select 
&amp;amp;var&amp;amp;i
FROM vendorlist;
QUit; 
%end;
%mend loop;
%loop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Fairly new to this macro and loop business and I'm looking for a probably very simple and easy solution which I have not been able to identify.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically, my end result is to create a table where the variable included includes all&amp;nbsp;variables from:&lt;/P&gt;&lt;P&gt;parent_level1_vendor_cde&amp;nbsp;to parent_level15_vendor_cde.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas??&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2016 14:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295854#M61873</guid>
      <dc:creator>camfarrell25</dc:creator>
      <dc:date>2016-09-01T14:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: Looping macro variables to create table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295855#M61874</link>
      <description>&lt;P&gt;Take a look at the macro examples, one should help you get started.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Right now it's not clear what you're trying to do...the table vendortree will be replaces/overwritten in each iteration.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2016 14:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295855#M61874</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-09-01T14:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: Looping macro variables to create table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295865#M61878</link>
      <description>&lt;P&gt;There's much to learn, and you did not pick the simplest example to start.&amp;nbsp; You will need macro language to loop through, each time generating a single field name within the SELECT statement, and possibly adding a comma.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%macro loop;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%local i;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;proc sql;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;create table vendortree as&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;select&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%do i=1 %to 15;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; parent_level&amp;amp;i._vender_cde&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp; %if &amp;amp;i &amp;lt; 15 %then ,;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%end;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;from vendorlist;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;quit;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%mend loop;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;%loop&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;For now, note that you are permitted to delimit the name of a macro variable with a dot so that SAS knows how many characters are actually part of the macro variable name:&amp;nbsp; &amp;amp;i. vs. &amp;amp;i both refer to the same macro variable.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2016 14:52:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295865#M61878</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-01T14:52:56Z</dc:date>
    </item>
    <item>
      <title>Re: Looping macro variables to create table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295871#M61881</link>
      <description>&lt;P&gt;First always start with code that does what you want without any macro values at all. Suppose to select 2 variables you have this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table vendortree as
   select 
     parent_level1_vender_cde,
     parent_level2_vender_cde
   FROM vendorlist;
QUit; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then if you have a Macro variable such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0080" face="SAS Monospace" size="2"&gt;%let&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; varlist = parent_level1_vender_cde, parent_level2_vender_cde;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;that resolves to just the variables (and note the presence of the comma as required for SQL syntax)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;then the above sql call could be:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="SAS Monospace" size="2"&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table vendortree as
   select 
      &amp;amp;varlist
   FROM vendorlist;
QUit;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;We are not using any quotes because we want the Name of the variable not the value.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;So bit you need is how to build that list AND account for the comma&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;The code below uses the loop information separately to make the variable list and then use it in the SQL code. I have added a parameter to allow you to specify the number of variables of the name to select. The %sysfunc is a macro function that allows you to call datastep functions. The CATX function will place a string indicated at the start of the function call between resolved strings removing blanks at beginning and end. The %quote is to get the quoted value of the comma as the macro language is sometimes picky about quoted strings with ','.&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="SAS Monospace" size="2"&gt;&lt;CODE class=" language-sas"&gt;%macro loop(MaxN=);
%do i = 1 %to &amp;amp;Maxn;
   %if &amp;amp;i=1 %then %let varlist= parent_level&amp;amp;i._vender_cde;
   %else %let varlist= %sysfunc(catx(%quote(,), &amp;amp;varlist , parent_level&amp;amp;i._vender_cde));
%end;

proc sql;
   create table vendortree as
   select 
   &amp;amp;varlist
   FROM vendorlist;
QUit; 
%end;
%mend loop;
&lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;Call using&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;%loop(MaxN=15); but you could use integers from 1 to the maximum number of those variables you want.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2016 15:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295871#M61881</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-09-01T15:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Looping macro variables to create table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295935#M61909</link>
      <description>&lt;P&gt;In general you use macro logic to generate code. &amp;nbsp;So you need to place your %DO loop such that it will generate the variable names as part of a single SELECT statement.&lt;/P&gt;
&lt;P&gt;Note that you do not place quotes around macro variable values, unless you want the quotes to be part of the value.&lt;/P&gt;
&lt;P&gt;Also note that you need to tell SAS where the macro variable reference ends and constant text begins by adding a period after the macro variable name when building the name of your variable that you want SQL to select from the value of your macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro loop;
%local i sep ;

proc sql;
  create table vendortree as
    select 
%do i=1 %to 15;
      &amp;amp;sep parent_level&amp;amp;i._vender_cde
 %let sep=,;
%end;
    from vendorlist
  ;
quit; 
%end;
%mend loop;
%loop;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2016 16:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-macro-variables-to-create-table/m-p/295935#M61909</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-09-01T16:50:50Z</dc:date>
    </item>
  </channel>
</rss>

