<?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 Macro Loop to Append Columns to a Base Table from a every table in a Library in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-to-Append-Columns-to-a-Base-Table-from-a-every-table/m-p/484930#M287098</link>
    <description>&lt;P&gt;I am trying to create a macro that takes a base table, loops through a library of tables and adds (in this specific case) one field from each&amp;nbsp;append table to the base table (for example, take a list of baseball players and loop through a library of monthly stats, adding one column from each monthly table to the base table).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am running into an issue where the last step (creating the 'Combine' table fails because table c1 is not 'properly sorted.'&amp;nbsp; What confuses me is that the way this is written, it is using the same base table through each pass through the loop, so there should be no&amp;nbsp; changes to sorting, nor missing/additional records in each pass.&amp;nbsp; Any help is appreciated, especially if there is a more efficient way to accomplish this goal.&amp;nbsp; Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

/*Create base data set*/
Data r;
	set r.all;
run;


/*Get list of column names from base data set*/
proc sql noprint;
	select name
	into: base separated by " "
	from dictionary.columns
	where memname = 'r';
quit;

%put &amp;amp;base.;

/*Get a list of all tables in the append library*/
proc contents data =a._all_ noprint out=ds_names(keep=memname); run;

/*Get a distinct list of just the tables you want to include*/
proc sql noprint;
    select distinct(memname)
    into: ds_list separated by " "
    from ds_names
	where substr(memname,1,6)='AT' and 201701 &amp;lt;= input(substr(memname,7,6),best6.) &amp;lt;= 201806;
quit;

%put &amp;amp;ds_list.;

%macro shoop(ds_list);
%do i=1 %to 2;

proc sql;
	create table c&amp;amp;i. as 
	select a.*, b.status as %scan(&amp;amp;ds_list, &amp;amp;i)
	from r as a
	left join a.%scan(&amp;amp;ds_list, &amp;amp;i) as b	on (a.id = b.id)
	order by &amp;amp;base.;
run;

%end;

data combine;
	merge
	%do i=1 %to 2;
	 c&amp;amp;i
	%end;;      
	by &amp;amp;base.;
run;

%mend;

%shoop(&amp;amp;ds_list.);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Aug 2018 22:33:40 GMT</pubDate>
    <dc:creator>P5C768</dc:creator>
    <dc:date>2018-08-07T22:33:40Z</dc:date>
    <item>
      <title>Macro Loop to Append Columns to a Base Table from a every table in a Library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-to-Append-Columns-to-a-Base-Table-from-a-every-table/m-p/484930#M287098</link>
      <description>&lt;P&gt;I am trying to create a macro that takes a base table, loops through a library of tables and adds (in this specific case) one field from each&amp;nbsp;append table to the base table (for example, take a list of baseball players and loop through a library of monthly stats, adding one column from each monthly table to the base table).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am running into an issue where the last step (creating the 'Combine' table fails because table c1 is not 'properly sorted.'&amp;nbsp; What confuses me is that the way this is written, it is using the same base table through each pass through the loop, so there should be no&amp;nbsp; changes to sorting, nor missing/additional records in each pass.&amp;nbsp; Any help is appreciated, especially if there is a more efficient way to accomplish this goal.&amp;nbsp; Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

/*Create base data set*/
Data r;
	set r.all;
run;


/*Get list of column names from base data set*/
proc sql noprint;
	select name
	into: base separated by " "
	from dictionary.columns
	where memname = 'r';
quit;

%put &amp;amp;base.;

/*Get a list of all tables in the append library*/
proc contents data =a._all_ noprint out=ds_names(keep=memname); run;

/*Get a distinct list of just the tables you want to include*/
proc sql noprint;
    select distinct(memname)
    into: ds_list separated by " "
    from ds_names
	where substr(memname,1,6)='AT' and 201701 &amp;lt;= input(substr(memname,7,6),best6.) &amp;lt;= 201806;
quit;

%put &amp;amp;ds_list.;

%macro shoop(ds_list);
%do i=1 %to 2;

proc sql;
	create table c&amp;amp;i. as 
	select a.*, b.status as %scan(&amp;amp;ds_list, &amp;amp;i)
	from r as a
	left join a.%scan(&amp;amp;ds_list, &amp;amp;i) as b	on (a.id = b.id)
	order by &amp;amp;base.;
run;

%end;

data combine;
	merge
	%do i=1 %to 2;
	 c&amp;amp;i
	%end;;      
	by &amp;amp;base.;
run;

%mend;

%shoop(&amp;amp;ds_list.);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Aug 2018 22:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-to-Append-Columns-to-a-Base-Table-from-a-every-table/m-p/484930#M287098</guid>
      <dc:creator>P5C768</dc:creator>
      <dc:date>2018-08-07T22:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop to Append Columns to a Base Table from a every table in a Library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-to-Append-Columns-to-a-Base-Table-from-a-every-table/m-p/484941#M287099</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/22545"&gt;@P5C768&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to create a macro that takes a base table, loops through a library of tables and adds (in this specific case) one field from each&amp;nbsp;append table to the base table (for example, take a list of baseball players and loop through a library of monthly stats, adding one column from each monthly table to the base table).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You would be much better off Appending data sets as long as those monthly data sets have a date or variable that indicates which month is involved.&lt;/P&gt;
&lt;P&gt;Then the code for manipulating any of those stats gets much easier. It looks like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set at201701 - at201806;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;would work. With use of the INDSNAME option on the set statement you could even build a date value from the apparent data set names.&lt;/P&gt;
&lt;P&gt;The report procedures such as Proc Tabulate or Report would build output tables using that date value to make a column per month, quarter, year, or even season with a small amount of work.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Aug 2018 23:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-to-Append-Columns-to-a-Base-Table-from-a-every-table/m-p/484941#M287099</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-08-07T23:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro Loop to Append Columns to a Base Table from a every table in a Library</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-to-Append-Columns-to-a-Base-Table-from-a-every-table/m-p/484955#M287100</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;Sir, hats off to your healthy eyes and super fast catching spirit while I am so tired by the evening. I like your neat approach. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 00:16:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Loop-to-Append-Columns-to-a-Base-Table-from-a-every-table/m-p/484955#M287100</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-08T00:16:03Z</dc:date>
    </item>
  </channel>
</rss>

