<?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 create multiple tables by variable values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600157#M173401</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;It is for reporting purpose. I have a large table, for simplicity just think as it has two vars: names; var1. The variable names has more values: aaa, bbb, ccc, .... My final output is to break down the large table into multiple tables by the values of "names". So output tables are: aaa_table; bbb_table; ccc_table...... each is a subset of the large table broken down by the values of variable "names".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i am not good at explaining thing. hope this can make it clear.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why do you need separate tables to make reports?&amp;nbsp; Why not just make the reports from the original table and filter by the value of the variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=BIG;
  where names='aaa';
  tables var1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Oct 2019 19:41:23 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-10-29T19:41:23Z</dc:date>
    <item>
      <title>proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600137#M173390</link>
      <description>&lt;P&gt;I want to create multiple tables from one data set using proc sql. First thought is using loop with macro variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The large data set likes (call it large_table):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;names&amp;nbsp; var1&lt;/P&gt;&lt;P&gt;aaa&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 23&lt;/P&gt;&lt;P&gt;aaa&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 33&lt;/P&gt;&lt;P&gt;bbb&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 44&lt;/P&gt;&lt;P&gt;bbb&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 44&lt;/P&gt;&lt;P&gt;bbb&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 45&lt;/P&gt;&lt;P&gt;ccc&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 55&lt;/P&gt;&lt;P&gt;.....&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;want to create tables with all the names, e.g.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table aaa_table as
select *
from large_table
where names = 'aaa'
;
quit;

proc sql;
create table bbb_table as
select *
from large_table
where names = 'bbb'
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;want to use loop, but cannot find out a better way to loop through the values. so far, tried to add index for names, then try to loop using macro variables to create tables. something like below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* get index, may not be necessary if there is other ways to do this */
proc sql;
select names, monotonic() as index
from (select distinct names from large_table)
;
quit;

/* then somehow to use the index as looping start and end */

/* e.g.

%macro names (...)

then add one proc sql to create tables inside the loop

*/

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;sorry for the long story, any help would be great!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 18:38:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600137#M173390</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2019-10-29T18:38:01Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600138#M173391</link>
      <description>&lt;P&gt;It would be really helpful if you told us (or better yet, showed us examples of) the output you want to achieve. It would also be very helpful if you explained what you are doing, and why you are doing it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Breaking up data sets like this is almost never a good idea, and there are almost always better ways to get to your final destination. Assuming I know what you are trying to do, and I'm not sure I actually do know what you want to do. But after you break up this data, what next? What is the next step of the program? Analysis? Plotting or reporting? Something else?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 18:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600138#M173391</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-29T18:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600147#M173397</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input names $  var1;
cards;
aaa          23
aaa          33
bbb          44
bbb          44
bbb          45
ccc          55
;

data _null_;
 set have end=z;
 by names;
 if _n_=1 then call execute('proc sql;');
 if first.names then do;
 call execute(catx(' ','create table ', names ,' as '));
 call execute(catt(' select * from  ','  have  ','  where  ',' names=','"',names,'"',';'));
 end;
 if z;
 call execute(' quit; ');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
514  data _null_;
515   set have end=z;
516   by names;
517   if _n_=1 then call execute('proc sql;');
518   if first.names then do;
519   call execute(catx(' ','create table ', names ,' as '));
520   call execute(catt(' select * from  ','  have  ','  where  ',' names=','"',names,'"',';'));
521   end;
522   if z;
523   call execute(' quit; ');
524  run;

NOTE: There were 6 observations read from the data set WORK.HAVE.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


NOTE: CALL EXECUTE generated line.
1   + proc sql;
2   + create table aaa as
3   +  select * from  have  where names="aaa";
NOTE: Table WORK.AAA created, with 2 rows and 2 columns.

4   + create table bbb as
5   +  select * from  have  where names="bbb";
NOTE: Table WORK.BBB created, with 3 rows and 2 columns.

6   + create table ccc as
7   +  select * from  have  where names="ccc";
NOTE: Table WORK.CCC created, with 1 rows and 2 columns.

8   +  quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.02 seconds
      cpu time            0.03 seconds
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Oct 2019 19:00:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600147#M173397</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-29T19:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600148#M173398</link>
      <description>&lt;P&gt;It is for reporting purpose. I have a large table, for simplicity just think as it has two vars: names; var1. The variable names has more values: aaa, bbb, ccc, .... My final output is to break down the large table into multiple tables by the values of "names". So output tables are: aaa_table; bbb_table; ccc_table...... each is a subset of the large table broken down by the values of variable "names".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i am not good at explaining thing. hope this can make it clear.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 18:53:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600148#M173398</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2019-10-29T18:53:59Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600152#M173399</link>
      <description>&lt;P&gt;Thanks for your help. Well, this code is out of my knowledge. It does work on my data though. I need to learn more about this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One thing noticed that it takes longer time to run and generate a message says "this takes longer time, use lots of resources ...." I tested that it did use more time than if i manually create each table using single proc sql for each table. I am using EG 5.1, not sure if it is related or not.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 19:20:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600152#M173399</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2019-10-29T19:20:44Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600154#M173400</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;&amp;nbsp; &amp;nbsp;Maybe you are after this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input names $  var1;
cards;
aaa          23
aaa          33
bbb          44
bbb          44
bbb          45
ccc          55
;
dm log 'clear';
proc sql ;
select distinct names,count(distinct names) into :names1-,:c
from have;
quit;

%macro t;
proc sql;
%do i =1 %to &amp;amp;c;
 create table &amp;amp;&amp;amp;names&amp;amp;i as
 select *
 from have 
 where names="&amp;amp;&amp;amp;names&amp;amp;i";
%end;
quit;
%mend t;

%t&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Oct 2019 19:31:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600154#M173400</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-10-29T19:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600157#M173401</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;It is for reporting purpose. I have a large table, for simplicity just think as it has two vars: names; var1. The variable names has more values: aaa, bbb, ccc, .... My final output is to break down the large table into multiple tables by the values of "names". So output tables are: aaa_table; bbb_table; ccc_table...... each is a subset of the large table broken down by the values of variable "names".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i am not good at explaining thing. hope this can make it clear.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why do you need separate tables to make reports?&amp;nbsp; Why not just make the reports from the original table and filter by the value of the variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=BIG;
  where names='aaa';
  tables var1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Oct 2019 19:41:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600157#M173401</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-29T19:41:23Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600193#M173416</link>
      <description>&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Make your life easy. Do not split up the data set.&lt;/STRONG&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use one large data set, and do the reporting in wahtever reporting procedure works for you (PROC PRINT, PROC TABULATE, PROC REPORT) using a BY statement to get different reports for different names. Or if you need different reports for different names, then do what &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; said.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Oct 2019 22:07:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600193#M173416</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-29T22:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600297#M173470</link>
      <description>&lt;P&gt;It is a good idea to use SQL to create the names, but actually a lot easier if you let SQL create all the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select distinct catx(' ','create table',cats(names,'_table'),'as select * from large_table where names =',
       quote(names)) into :sqlcode separated by ';' from large_table;
;
&amp;amp;sqlcode;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 Oct 2019 09:11:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600297#M173470</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-10-30T09:11:40Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600301#M173474</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/84484"&gt;@sasecn&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe you need to take a step back and look at your problem with a bit more distance.&lt;/P&gt;
&lt;P&gt;Many SAS Procs allow for By Group processing and you can create a table per by group. Many Procs also offer a CLASS statement and you can get statistics/reports per distinct value of a class variable.&lt;/P&gt;
&lt;P&gt;So... What do you have? And what do you need? If you can provide simple sample data (via a working SAS data step creating such data) and then show us how the report needs to look like then I'm sure someone can come up with a solution which requires less advanced coding and is easier to understand for you.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 09:20:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600301#M173474</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-30T09:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600423#M173555</link>
      <description>&lt;P&gt;Yes, that is what&amp;nbsp; I was looking for. Thanks for the help!&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 15:22:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600423#M173555</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2019-10-30T15:22:43Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql to create multiple tables by variable values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600424#M173556</link>
      <description>&lt;P&gt;Great, thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2019 15:24:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-to-create-multiple-tables-by-variable-values/m-p/600424#M173556</guid>
      <dc:creator>sasecn</dc:creator>
      <dc:date>2019-10-30T15:24:17Z</dc:date>
    </item>
  </channel>
</rss>

