<?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: PROC SQL to load column from multiple tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598913#M172797</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Of course macros are great, &lt;STRONG&gt;I try to use is as much as I can.&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No!&amp;nbsp; I disagree.&amp;nbsp; I would change this to "I try to use only when required".&amp;nbsp; So, what is the definition of "required"?&amp;nbsp; Obviously you'll get different answers, my quick one off the top of my head is 1) when you want to parameterize a block of &lt;U&gt;&lt;STRONG&gt;working&lt;/STRONG&gt;&lt;/U&gt; code (if you find yourself cut-and-pasting a block of code with the same edits, that's a good candidate), or 2) you want to encapsulate a block of code that you will call often (this could also be an external %include block of code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;However, do you have any tips/hints of when macros should NOT be used?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro is analogous to the pre-processor in C.&amp;nbsp; It does a lot more, but let's run with this analogy.&amp;nbsp; All it does is control the code that gets sent to the compiler (for now I'm ignoring data step interfaces like symget and call symputx).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I like to think of macro as "If I could type this code at 10,000 words a second, and never make a mistake, then I wouldn't need macro".&amp;nbsp; Silly perhaps, but &lt;U&gt;macro is just controlling the code that gets sent to the compiler&lt;/U&gt; (or a proc which isn't compiled, but I digress).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, macro is MUCH slower than if you could do it in the data step or a proc.&amp;nbsp; Another example is if you're using macro to process in groups, when that processing would be supported via BY group processing in SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you find you're writing really klunky, hard to follow macro code for your problem, step back and see if you really need macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A quick example and I've got to run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Say you have 1000 external files you need to concatenate into a single dataset.&amp;nbsp; They are all in one directory and have the same format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could get the list of files, then loop over that list in a macro, generating 1000 data steps, appending each input to a target table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or, you could use an aggregated filename, plus the filename and/or filevar options to the infile statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd say the key to knowing when not to use macro is simply increasing your overall knowledge of alternative, better approaches via SAS, esp. the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can usually tell a "newbie" (I'm not saying you're one, by the way) when they think macro is the panacea for everything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Oct 2019 04:04:28 GMT</pubDate>
    <dc:creator>ScottBass</dc:creator>
    <dc:date>2019-10-24T04:04:28Z</dc:date>
    <item>
      <title>PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598226#M172502</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have more than 50 tables with identical structure (they all have the same number of columns and same datatype). I would like to do the following code which returns 1 line of data, but for all my 50+ tables. Then, I would also like to regroup all those lines into one dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE DASHBOARD2 AS
(	SELECT AVG(VALUE) AS MOYENNE, MAX(VALUE) AS MAXIMUM, MIN(VALUE) AS MINIMUM, SUM(VALUE) AS SOMME, COUNT(*) AS NBOBS
	FROM TEST01
);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to do so for all the 50+ tables and to get one dataset with 50+ lines with thoses informations (avg, max, min, sum, nbos)?&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 19:26:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598226#M172502</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-21T19:26:20Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598228#M172504</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp; &amp;nbsp;I think&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Append 50 tables to 1&lt;/P&gt;
&lt;P&gt;2. Data step/proc summary (By groups)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;should be a better approach than proc sql(which warrants 48 union all, or a macro loop one by one)&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 19:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598228#M172504</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-21T19:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598229#M172505</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp; &amp;nbsp;I think&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Append 50 tables to 1&lt;/P&gt;
&lt;P&gt;2. Data step/proc summary (By groups)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;should be a better approach than proc sql(which warrants 48 union all, or a macro loop one by one)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I did the following macro :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dash;
%do i = 1 %to 3;
	PROC SQL;
	CREATE TABLE DASHBOARDk&amp;amp;i AS
	(	SELECT AVG(VALUE) AS MOYENNE, MAX(VALUE) AS MAXIMUM, MIN(VALUE) AS MINIMUM, SUM(VALUE) AS SOMME, COUNT(*) AS NBOBS
		FROM TEST0&amp;amp;i
	);
	QUIT;
run ;

%end;
%mend dash;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It extracts the wanted informations into 50+ different tables. Now, I struggle to get them all together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I use your proposed solution? I'm new with SAS and I'm trying to "Google" as many things as I can.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 19:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598229#M172505</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-21T19:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598232#M172507</link>
      <description>&lt;P&gt;I assume that each table have 1 or more observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you ask for &lt;STRONG&gt;max(value)&lt;/STRONG&gt;&amp;nbsp;- or any other statistics - do you mean&amp;nbsp;per table or per all tables as a unit ?&lt;/P&gt;
&lt;P&gt;Do you want the statistics of one specific variable or on all variables ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems to me that PROC MEANS will be more efficient than PROC SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 19:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598232#M172507</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-10-21T19:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598233#M172508</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data append;
set test1-test50 indsname=n;/*if it is in continuous sequence suffix*/
nameof_dsn=n;
run;


proc summary data=append nway noprint;
class nameof_dsn;
var value;
output out=want mean=moyenne max=maximum; /*and so forth, i am lazy to write the rest*/
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited: to include&amp;nbsp;&lt;CODE class=" language-sas"&gt;nameof_dsn as Class variable&lt;/CODE&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 19:42:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598233#M172508</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-21T19:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598239#M172512</link>
      <description>In fact, my 50+ tables represents some the 50+ last months. Of course, I have more observations each month the file is generated. What I want is to define limits from the past 50+ months usings statistics of those months to verify that my new data (next months) are within those limits.&lt;BR /&gt;&lt;BR /&gt;So the answer to your question is the a specific statistic on one variable (value).&lt;BR /&gt;&lt;BR /&gt;If it's not clear, do not hesitate.</description>
      <pubDate>Mon, 21 Oct 2019 19:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598239#M172512</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-21T19:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598276#M172530</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In fact, my 50+ tables represents some the 50+ last months. Of course, I have more observations each month the file is generated. What I want is to define limits from the past 50+ months usings statistics of those months to verify that my new data (next months) are within those limits.&lt;BR /&gt;&lt;BR /&gt;So the answer to your question is the a specific statistic on one variable (value).&lt;BR /&gt;&lt;BR /&gt;If it's not clear, do not hesitate.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In such case the code should look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets lib=work nolist; delete final; quit; 

%macro stat;
    %do i=1 %to 50;
     proc means data=test&amp;amp;i noprint nway;
           class &amp;lt;month&amp;gt;;
           var value;
           output out=temp mean=moyenne max=maximum 
                          min=minimum sum=somme n=nobs;
     run;
     proc append base=final data=temp; run;
     proc datasets lib=work nolist; delete temp; quit; 
%mend;
%stat;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Oct 2019 22:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598276#M172530</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-10-21T22:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598392#M172561</link>
      <description>&lt;P&gt;I think this can be done quite simply by using an INSERT statement in SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dash;
	PROC SQL;
	CREATE TABLE DASHBOARD AS
		SELECT AVG(VALUE) AS MOYENNE, MAX(VALUE) AS MAXIMUM, MIN(VALUE) AS MINIMUM, SUM(VALUE) AS SOMME, COUNT(*) AS NBOBS
		FROM TEST01;
	;
	QUIT;

%do i = 2 %to 3;
	PROC SQL;
	Insert into dashboard
		SELECT AVG(VALUE) AS MOYENNE, MAX(VALUE) AS MAXIMUM, MIN(VALUE) AS MINIMUM, SUM(VALUE) AS SOMME, COUNT(*) AS NBOBS
		FROM TEST0&amp;amp;i
	);
	QUIT;
run ;

%end;
%mend dash;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Oct 2019 13:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598392#M172561</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-10-22T13:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598403#M172565</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;In fact, my 50+ tables represents some the 50+ last months. Of course, I have more observations each month the file is generated. What I want is to define limits from the past 50+ months usings statistics of those months to verify that my new data (next months) are within those limits.&lt;BR /&gt;&lt;BR /&gt;So the answer to your question is the a specific statistic on one variable (value).&lt;BR /&gt;&lt;BR /&gt;If it's not clear, do not hesitate.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In such case the code should look like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets lib=work nolist; delete final; quit; 

%macro stat;
    %do i=1 %to 50;
     proc means data=test&amp;amp;i noprint nway;
           class &amp;lt;month&amp;gt;;
           var value;
           output out=temp mean=moyenne max=maximum 
                          min=minimum sum=somme n=nobs;
     run;
     proc append base=final data=temp; run;
     proc datasets lib=work nolist; delete temp; quit; 
%mend;
%stat;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;for your help and time.&lt;/P&gt;
&lt;P&gt;Really appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2019 14:30:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598403#M172565</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-22T14:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598631#M172650</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi folks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have more than 50 tables with identical structure (they all have the same number of columns and same datatype). I would like to do the following code which returns 1 line of data, but for all my 50+ tables. Then, I would also like to regroup all those lines into one dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE DASHBOARD2 AS
(	SELECT AVG(VALUE) AS MOYENNE, MAX(VALUE) AS MAXIMUM, MIN(VALUE) AS MINIMUM, SUM(VALUE) AS SOMME, COUNT(*) AS NBOBS
	FROM TEST01
);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to do so for all the 50+ tables and to get one dataset with 50+ lines with thoses informations (avg, max, min, sum, nbos)?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;is correct...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your proc sql is going to return one line, with avg, max, min, sum, and count for all the rows in test01.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Get proc summary to create the same results for your single test01 dataset.&amp;nbsp; Read the doc on proc summary if you need to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now that it's working for one dataset, just append all your datasets into one, using the indsname option to capture the contributing dataset name.&amp;nbsp; You could even use a data step view if your datasets are really large.&amp;nbsp; Read the doc on the indsname option if you need to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, just use the dataset name as a class variable in proc summary.&amp;nbsp; This is analogous to group by in SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you insist on proc sql, then append the datasets as above, and use the dataset name in a group by in proc sql:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test01 (where=(x between 0 and 10))
     test02 (where=(x between 10 and 20))
     test03 (where=(x between 20 and 30))
     ;
   do x=0 to 30;
      output;
   end;
run;

data append / view=append;
   * or test: if you don't have any extra files named test... ;   
   set test01-test03 indsname=n;  
   name=n;
run;

proc sql;
   create table foo as
   select 
      name
      ,avg(x) as avg
      ,min(x) as min
      ,max(x) as max
   from
      append
   group by
      name
   ;
quit;

proc summary data=append nway noprint;
   class name;
   var x;
   output out=foo2 (drop=_type_ _freq_)
      mean=avg 
      min=min
      max=max 
   ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Macros are great - I use them every day.&amp;nbsp; But it's just as important to know when NOT to use a macro.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 07:59:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598631#M172650</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-10-23T07:59:59Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598701#M172684</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285961"&gt;@jpprovost&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi folks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have more than 50 tables with identical structure (they all have the same number of columns and same datatype). I would like to do the following code which returns 1 line of data, but for all my 50+ tables. Then, I would also like to regroup all those lines into one dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE DASHBOARD2 AS
(	SELECT AVG(VALUE) AS MOYENNE, MAX(VALUE) AS MAXIMUM, MIN(VALUE) AS MINIMUM, SUM(VALUE) AS SOMME, COUNT(*) AS NBOBS
	FROM TEST01
);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to do so for all the 50+ tables and to get one dataset with 50+ lines with thoses informations (avg, max, min, sum, nbos)?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;is correct...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your proc sql is going to return one line, with avg, max, min, sum, and count for all the rows in test01.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Get proc summary to create the same results for your single test01 dataset.&amp;nbsp; Read the doc on proc summary if you need to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now that it's working for one dataset, just append all your datasets into one, using the indsname option to capture the contributing dataset name.&amp;nbsp; You could even use a data step view if your datasets are really large.&amp;nbsp; Read the doc on the indsname option if you need to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, just use the dataset name as a class variable in proc summary.&amp;nbsp; This is analogous to group by in SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you insist on proc sql, then append the datasets as above, and use the dataset name in a group by in proc sql:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test01 (where=(x between 0 and 10))
     test02 (where=(x between 10 and 20))
     test03 (where=(x between 20 and 30))
     ;
   do x=0 to 30;
      output;
   end;
run;

data append / view=append;
   * or test: if you don't have any extra files named test... ;   
   set test01-test03 indsname=n;  
   name=n;
run;

proc sql;
   create table foo as
   select 
      name
      ,avg(x) as avg
      ,min(x) as min
      ,max(x) as max
   from
      append
   group by
      name
   ;
quit;

proc summary data=append nway noprint;
   class name;
   var x;
   output out=foo2 (drop=_type_ _freq_)
      mean=avg 
      min=min
      max=max 
   ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Macros are great - I use them every day.&amp;nbsp; But it's just as important to know when NOT to use a macro.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thank to you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15043"&gt;@ScottBass&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;Of course macros are great, I try to use is as much as I can.&lt;/P&gt;
&lt;P&gt;However, do you have any tips/hints of when macros should NOT be used?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 13:17:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598701#M172684</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-23T13:17:42Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598913#M172797</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Of course macros are great, &lt;STRONG&gt;I try to use is as much as I can.&lt;/STRONG&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No!&amp;nbsp; I disagree.&amp;nbsp; I would change this to "I try to use only when required".&amp;nbsp; So, what is the definition of "required"?&amp;nbsp; Obviously you'll get different answers, my quick one off the top of my head is 1) when you want to parameterize a block of &lt;U&gt;&lt;STRONG&gt;working&lt;/STRONG&gt;&lt;/U&gt; code (if you find yourself cut-and-pasting a block of code with the same edits, that's a good candidate), or 2) you want to encapsulate a block of code that you will call often (this could also be an external %include block of code).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;However, do you have any tips/hints of when macros should NOT be used?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro is analogous to the pre-processor in C.&amp;nbsp; It does a lot more, but let's run with this analogy.&amp;nbsp; All it does is control the code that gets sent to the compiler (for now I'm ignoring data step interfaces like symget and call symputx).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I like to think of macro as "If I could type this code at 10,000 words a second, and never make a mistake, then I wouldn't need macro".&amp;nbsp; Silly perhaps, but &lt;U&gt;macro is just controlling the code that gets sent to the compiler&lt;/U&gt; (or a proc which isn't compiled, but I digress).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, macro is MUCH slower than if you could do it in the data step or a proc.&amp;nbsp; Another example is if you're using macro to process in groups, when that processing would be supported via BY group processing in SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you find you're writing really klunky, hard to follow macro code for your problem, step back and see if you really need macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A quick example and I've got to run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Say you have 1000 external files you need to concatenate into a single dataset.&amp;nbsp; They are all in one directory and have the same format.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could get the list of files, then loop over that list in a macro, generating 1000 data steps, appending each input to a target table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or, you could use an aggregated filename, plus the filename and/or filevar options to the infile statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd say the key to knowing when not to use macro is simply increasing your overall knowledge of alternative, better approaches via SAS, esp. the data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can usually tell a "newbie" (I'm not saying you're one, by the way) when they think macro is the panacea for everything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2019 04:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/598913#M172797</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-10-24T04:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL to load column from multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/599017#M172831</link>
      <description>I define myself as a newbie, don't worry.&lt;BR /&gt;Thanks for all those explanations. They are very useful and I will keep it in mind in the future.&lt;BR /&gt;&lt;BR /&gt;Thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 24 Oct 2019 13:04:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-to-load-column-from-multiple-tables/m-p/599017#M172831</guid>
      <dc:creator>jpprovost</dc:creator>
      <dc:date>2019-10-24T13:04:19Z</dc:date>
    </item>
  </channel>
</rss>

