<?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: Generate multiple tables in a loop in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337467#M22430</link>
    <description>&lt;P&gt;&lt;FONT face="courier new,courier" size="6"&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Tabulations and output NAMED output datasets for every combination of car make and drivetrain

HAVE
===

Up to 40 obs WORK.CARS total obs=428

Obs    TYPE      ORIGIN    MAKE     DRIVETRAIN

  1    SUV       Asia      Acura      All
  2    Sedan     Asia      Acura      Front
  3    Sedan     Asia      Acura      Front
  4    Sedan     Asia      Acura      Front
  5    Sedan     Asia      Acura      Front
  6    Sedan     Asia      Acura      Front
  7    Sports    Asia      Acura      Rear
  8    Sedan     Europe    Audi       Front
  9    Sedan     Europe    Audi       Front
 10    Sedan     Europe    Audi       Front
 11    Sedan     Europe    Audi       All


WANT Tabulations and datasets
=============================

Tabulations for all

make and drivetrain combinations for Hybrid and Asia

--------------------------------------------
|                |       DRIVETRAIN        |
|                |-------------------------|
|                |          Front          |
|                |-------------------------|
|                |     N      |    PctN    |
|----------------+------------+------------|
|MAKE            |            |            |
|----------------|            |            |
|Honda           |        2.00|      100.00|
|----------------+------------+------------|
|Toyota          |        1.00|      100.00|
--------------------------------------------

....

make and drivetrain combinations for Truck and Asia

----------------------------------------------------------------------
|                |                    DRIVETRAIN                     |
|                |---------------------------------------------------|
|                |           All           |          Rear           |
|                |-------------------------+-------------------------|
|                |     N      |    PctN    |     N      |    PctN    |
|----------------+------------+------------+------------+------------|
|MAKE            |            |            |            |            |
|----------------|            |            |            |            |
|Mazda           |        1.00|      100.00|        1.00|      100.00|
|----------------+------------+------------+------------+------------|
|Nissan          |        2.00|      100.00|           .|           .|
|----------------+------------+------------+------------+------------|
|Subaru          |        1.00|      100.00|           .|           .|
|----------------+------------+------------+------------+------------|
|Toyota          |        1.00|      100.00|        2.00|      100.00|
----------------------------------------------------------------------

 Also want these datasets

                   Member
 #  Name           Type

 1  HYBRID_ASIA    DATA
 2  HYBRID_EUROPE  DATA
 3  HYBRID_USA     DATA
 6  SEDAN_ASIA     DATA
 7  SEDAN_EUROPE   DATA
 8  SEDAN_USA      DATA
 9  SPORTS_ASIA    DATA
 0  SPORTS_EUROPE  DATA
 1  SPORTS_USA     DATA
 2  SUV_ASIA       DATA
 3  SUV_EUROPE     DATA
 4  SUV_USA        DATA
 5  TRUCK_ASIA     DATA
 6  TRUCK_EUROPE   DATA
 7  TRUCK_USA      DATA
 8  WAGON_ASIA     DATA
 9  WAGON_EUROPE   DATA
 0  WAGON_USA      DATA


Sample datsets TRUCK_ASIA

 Up to 40 obs from TRUCK_ASIA total obs=6

 Obs     MAKE     DRIVETRAIN    _TYPE_    _PAGE_    _TABLE_    N    PCTN_11

  1     Mazda        All          11         1         1       1      100
  2     Mazda        Rear         11         1         1       1      100
  3     Nissan       All          11         1         1       2      100
  4     Subaru       All          11         1         1       1      100
  5     Toyota       All          11         1         1       1      100
  6     Toyota       Rear         11         1         1       2      100


WORKING CODE
===========

    DOSUBL
       select distinct (quote(type))   into :types separated   by "," from sashelp.cars;
       select distinct (quote(origin)) into :origins separated by "," from sashelp.cars
    DOSUBL
       proc tabulate data=sashelp.cars out=&amp;amp;typ._&amp;amp;org;
        title "make and drivetrain combinations for &amp;amp;typ and &amp;amp;org";
        where type="&amp;amp;typ" and origin="&amp;amp;org";


FULL SOLUTION
=============

* create some data;
data cars;
  retain  type origin make drivetrain;
  set sashelp.cars(keep=make drivetrain type origin);
run;quit;

%symdel types origins; * just in case they exist;
proc datasets lib=work kill;
run;quit;

data _null_;

   * get the metat data;
   if _n_ = 0 then do;
      %let rc=%sysfunc(dosubl('
         proc sql;
            select distinct (quote(type))   into :types separated   by "," from sashelp.cars;
            select distinct (quote(origin)) into :origins separated by "," from sashelp.cars
         ;quit;
      '));
   end;

   set sashelp.cars(obs=1);

     do typ=&amp;amp;types;
       do org=&amp;amp;origins;
         call symputx('typ',typ);
         call symputx('org',org);
         rc=dosubl('
            proc tabulate data=sashelp.cars out=&amp;amp;typ._&amp;amp;org;
              title "make and drivetrain combinations for &amp;amp;typ and &amp;amp;org";
              where type="&amp;amp;typ" and origin="&amp;amp;org";
              class make drivetrain;
              table make,drivetrain*(n pctn&amp;lt;drivetrain&amp;gt;)/rts=18;
            run;quit;
         ');
       end;
     end;
   stop;
run;quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 02 Mar 2017 17:31:49 GMT</pubDate>
    <dc:creator>rogerjdeangelis</dc:creator>
    <dc:date>2017-03-02T17:31:49Z</dc:date>
    <item>
      <title>Generate multiple tables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337195#M22410</link>
      <description>&lt;P&gt;Currently using the following codes to generate one set of tables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Num_category = 3;

%macro table;
%do _j=1 %to &amp;amp;Num_category;

proc tabulate data=input.data out=output.sum_classA_typeA_&amp;amp;_j;   /* use the same input dataset */
      class v1;
      var Period_1 - Period_23;
      where class = 'classA' and type = 'typeA';  /* specify class and type here */
      table (v1 =''), (Period_1 - Period_23) * (sum='No.') *f=comma9.2 / box ='name' printmiss;
quit;

data output.sum_classA_typeA_&amp;amp;j;
set output.sum_classA_typeA_&amp;amp;j;
...             
run;

proc sort data=output.sum_classA_typeA_&amp;amp;j;
by ID;
run;
%end;
%mend table;
%table;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now the problem is, there are more classes and types than just 'classA' and 'typeA'.&lt;/P&gt;&lt;P&gt;Is there a way to create new loops to run the macro above?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One possible approach might be creating two arrays:&lt;/P&gt;&lt;P&gt;1. &amp;nbsp;classA &amp;nbsp;classB &amp;nbsp;classC;&lt;/P&gt;&lt;P&gt;2. &amp;nbsp;typeA &amp;nbsp;typeB &amp;nbsp;typeC;&lt;/P&gt;&lt;P&gt;Each time will take one value from array1 and one value from array2, and generate a new set of tables: e.g.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;output.sum_classA_typeB_&amp;amp;j;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;and then keep going:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;output.sum_classA_typeC_&amp;amp;j;&lt;BR /&gt;output.sum_classB_typeA_&amp;amp;j;&lt;BR /&gt;...&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's very easy to achieve this task in other languages (e.g. R, python), not sure how to do it in SAS enterprise guide?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 00:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337195#M22410</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-02T00:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Generate multiple tables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337199#M22412</link>
      <description>&lt;P&gt;Have you looked into expanding your BY group processing?&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120587"&gt;@ayin&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's very easy to achieve this task in other languages (e.g. R, python), not sure how to do it in SAS enterprise guide?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Find the best way to do something in SAS. R/Python function as matrix type languages while SAS processed data row by row. EG tends to be more SQL but if you're a programmer I don't recommend the GUI tools. This means that (most of) my SAS code that processes 100 rows will work for 1 million or 100 million as long as I can wait a bit, without further changes to account for data size. It's not as important these days since RAM has become so cheap but was very important once and in many places where getting upgrades is difficult it's still important.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 00:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337199#M22412</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-02T00:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Generate multiple tables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337203#M22413</link>
      <description>Hey Reeza, thanks for your reply.&lt;BR /&gt;The objectivity here is to create multiple sets of tables so they can be used in the future.&lt;BR /&gt;&lt;BR /&gt;Also the users need to pre-determine what classes/types they will be using. (The input dataset has more than 10 classes and more than 10 types, but we only want to generate tables for 3 classes and 3 types. i.e. 9 sets of tables instead of 100 sets).</description>
      <pubDate>Thu, 02 Mar 2017 01:09:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337203#M22413</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-02T01:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: Generate multiple tables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337205#M22414</link>
      <description>&lt;P&gt;Given your response I strongly suggest you revisit BY GROUP processing. The number of tables created doesn't matter, simply apply the criteria as a filter.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously this can be done with loops, it's just not efficient and cumbersome to manage that code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 01:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337205#M22414</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-02T01:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: Generate multiple tables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337207#M22415</link>
      <description>&lt;P&gt;If you decide to continue on a macro route:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Here's a link to the SAS macro examples available in the documentation&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. You'll likely find CALL EXECUTE helpful to call the macro, see the documentation examples for how to do this from parameters in a dataset.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 01:28:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337207#M22415</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-02T01:28:38Z</dc:date>
    </item>
    <item>
      <title>Re: Generate multiple tables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337243#M22416</link>
      <description>&lt;P&gt;Thanks Reeza for your suggestion.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regarding the question, I have found one solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Class = classA classB classC;                /* determine the classes */
%let Type = typeA typeB typeC;                   /* determine the types   */
%let Num_class = %sysfunc(countw(&amp;amp;Class));
%let Num_type = %sysfunc(countw(&amp;amp;Type));

%global Class_Name;
%global Type_Name;

%macro test;
%do _k=1 %to &amp;amp;Num_class;
%let Class_Name = %scan(&amp;amp;Class,&amp;amp;_k);      /* retrieve the value */

%do _l=1 %to &amp;amp;Num_type;
%let Type_Name = %scan(&amp;amp;Type,&amp;amp;_l);

proc tabulate data=input.data out=output.sum_&amp;amp;Class_Name._&amp;amp;Type_Name._&amp;amp;_j;
...
          where class = symget('Class_Name') and type = symget('Type_Name');
...
quit;

%end;
%end;
%mend test;
%test;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 05:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337243#M22416</guid>
      <dc:creator>ayin</dc:creator>
      <dc:date>2017-03-02T05:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: Generate multiple tables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337249#M22417</link>
      <description>&lt;P&gt;If you post the full proc tabulate code I can show you how to do this without a macro.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 Mar 2017 06:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337249#M22417</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-02T06:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Generate multiple tables in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337467#M22430</link>
      <description>&lt;P&gt;&lt;FONT face="courier new,courier" size="6"&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Tabulations and output NAMED output datasets for every combination of car make and drivetrain

HAVE
===

Up to 40 obs WORK.CARS total obs=428

Obs    TYPE      ORIGIN    MAKE     DRIVETRAIN

  1    SUV       Asia      Acura      All
  2    Sedan     Asia      Acura      Front
  3    Sedan     Asia      Acura      Front
  4    Sedan     Asia      Acura      Front
  5    Sedan     Asia      Acura      Front
  6    Sedan     Asia      Acura      Front
  7    Sports    Asia      Acura      Rear
  8    Sedan     Europe    Audi       Front
  9    Sedan     Europe    Audi       Front
 10    Sedan     Europe    Audi       Front
 11    Sedan     Europe    Audi       All


WANT Tabulations and datasets
=============================

Tabulations for all

make and drivetrain combinations for Hybrid and Asia

--------------------------------------------
|                |       DRIVETRAIN        |
|                |-------------------------|
|                |          Front          |
|                |-------------------------|
|                |     N      |    PctN    |
|----------------+------------+------------|
|MAKE            |            |            |
|----------------|            |            |
|Honda           |        2.00|      100.00|
|----------------+------------+------------|
|Toyota          |        1.00|      100.00|
--------------------------------------------

....

make and drivetrain combinations for Truck and Asia

----------------------------------------------------------------------
|                |                    DRIVETRAIN                     |
|                |---------------------------------------------------|
|                |           All           |          Rear           |
|                |-------------------------+-------------------------|
|                |     N      |    PctN    |     N      |    PctN    |
|----------------+------------+------------+------------+------------|
|MAKE            |            |            |            |            |
|----------------|            |            |            |            |
|Mazda           |        1.00|      100.00|        1.00|      100.00|
|----------------+------------+------------+------------+------------|
|Nissan          |        2.00|      100.00|           .|           .|
|----------------+------------+------------+------------+------------|
|Subaru          |        1.00|      100.00|           .|           .|
|----------------+------------+------------+------------+------------|
|Toyota          |        1.00|      100.00|        2.00|      100.00|
----------------------------------------------------------------------

 Also want these datasets

                   Member
 #  Name           Type

 1  HYBRID_ASIA    DATA
 2  HYBRID_EUROPE  DATA
 3  HYBRID_USA     DATA
 6  SEDAN_ASIA     DATA
 7  SEDAN_EUROPE   DATA
 8  SEDAN_USA      DATA
 9  SPORTS_ASIA    DATA
 0  SPORTS_EUROPE  DATA
 1  SPORTS_USA     DATA
 2  SUV_ASIA       DATA
 3  SUV_EUROPE     DATA
 4  SUV_USA        DATA
 5  TRUCK_ASIA     DATA
 6  TRUCK_EUROPE   DATA
 7  TRUCK_USA      DATA
 8  WAGON_ASIA     DATA
 9  WAGON_EUROPE   DATA
 0  WAGON_USA      DATA


Sample datsets TRUCK_ASIA

 Up to 40 obs from TRUCK_ASIA total obs=6

 Obs     MAKE     DRIVETRAIN    _TYPE_    _PAGE_    _TABLE_    N    PCTN_11

  1     Mazda        All          11         1         1       1      100
  2     Mazda        Rear         11         1         1       1      100
  3     Nissan       All          11         1         1       2      100
  4     Subaru       All          11         1         1       1      100
  5     Toyota       All          11         1         1       1      100
  6     Toyota       Rear         11         1         1       2      100


WORKING CODE
===========

    DOSUBL
       select distinct (quote(type))   into :types separated   by "," from sashelp.cars;
       select distinct (quote(origin)) into :origins separated by "," from sashelp.cars
    DOSUBL
       proc tabulate data=sashelp.cars out=&amp;amp;typ._&amp;amp;org;
        title "make and drivetrain combinations for &amp;amp;typ and &amp;amp;org";
        where type="&amp;amp;typ" and origin="&amp;amp;org";


FULL SOLUTION
=============

* create some data;
data cars;
  retain  type origin make drivetrain;
  set sashelp.cars(keep=make drivetrain type origin);
run;quit;

%symdel types origins; * just in case they exist;
proc datasets lib=work kill;
run;quit;

data _null_;

   * get the metat data;
   if _n_ = 0 then do;
      %let rc=%sysfunc(dosubl('
         proc sql;
            select distinct (quote(type))   into :types separated   by "," from sashelp.cars;
            select distinct (quote(origin)) into :origins separated by "," from sashelp.cars
         ;quit;
      '));
   end;

   set sashelp.cars(obs=1);

     do typ=&amp;amp;types;
       do org=&amp;amp;origins;
         call symputx('typ',typ);
         call symputx('org',org);
         rc=dosubl('
            proc tabulate data=sashelp.cars out=&amp;amp;typ._&amp;amp;org;
              title "make and drivetrain combinations for &amp;amp;typ and &amp;amp;org";
              where type="&amp;amp;typ" and origin="&amp;amp;org";
              class make drivetrain;
              table make,drivetrain*(n pctn&amp;lt;drivetrain&amp;gt;)/rts=18;
            run;quit;
         ');
       end;
     end;
   stop;
run;quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 02 Mar 2017 17:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/Generate-multiple-tables-in-a-loop/m-p/337467#M22430</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-02T17:31:49Z</dc:date>
    </item>
  </channel>
</rss>

