<?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: How do I loop a SQL Query - Different Variables in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/505005#M72771</link>
    <description>&lt;P&gt;Well, whilst there are loops in SAS, what you are trying to do here is not a loop as such, it is called by group processing, and it is simpler.&amp;nbsp; From what you post I gather that you want to produce an Excel file with one tab for each of the groups from History table where it is in a given list.&amp;nbsp; So the steps are (and I have no data to work off here):&lt;/P&gt;
&lt;PRE&gt;/* Import list */
proc import ... out=list;
run;

/* Filter big data */
proc sql;
  create table want as
  select * 
  from   history
  where account in (select account from list);
quit;

/* Produce report */
ods excel file="....xml" options(sheet_interval="bygroup");
proc report data=want nowd;
  by account;
...
run;
ods excel close;&lt;/PRE&gt;
&lt;P&gt;The by is a key line here, as is the sheet_interval option.&lt;/P&gt;
&lt;P&gt;That should get you started.&lt;/P&gt;</description>
    <pubDate>Wed, 17 Oct 2018 08:47:08 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-10-17T08:47:08Z</dc:date>
    <item>
      <title>How do I loop a SQL Query - Different Variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/504999#M72770</link>
      <description>&lt;P&gt;SAS EG&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi there... I am new to the forum and a first time poster so please go easy !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have basic understanding of loops from my experience with VBA although I need some help with doing the same in SAS..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My simplified code looks something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;****************************************&lt;/P&gt;&lt;P&gt;Proc Import Excel data set;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let Master_Account = "123456789";&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*step 1*/&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;create table HISTORY&amp;nbsp;as&lt;/P&gt;&lt;P&gt;select A from Table&lt;BR /&gt;where ACCOUNT = &amp;amp;Master_Account;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;/*step 2*/&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc export to excel Sheet 1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;****************************************&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The "&amp;amp;Master_Account" exists as there are 4 repetitions of the above code with different select queries.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is a local excel file that contains a list of account numbers that I import and save to 'work' where I then need to loop through each account number and run the above query.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the end there should be excel reports generated based off the inputted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance you for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 08:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/504999#M72770</guid>
      <dc:creator>Adam93</dc:creator>
      <dc:date>2018-10-17T08:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do I loop a SQL Query - Different Variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/505005#M72771</link>
      <description>&lt;P&gt;Well, whilst there are loops in SAS, what you are trying to do here is not a loop as such, it is called by group processing, and it is simpler.&amp;nbsp; From what you post I gather that you want to produce an Excel file with one tab for each of the groups from History table where it is in a given list.&amp;nbsp; So the steps are (and I have no data to work off here):&lt;/P&gt;
&lt;PRE&gt;/* Import list */
proc import ... out=list;
run;

/* Filter big data */
proc sql;
  create table want as
  select * 
  from   history
  where account in (select account from list);
quit;

/* Produce report */
ods excel file="....xml" options(sheet_interval="bygroup");
proc report data=want nowd;
  by account;
...
run;
ods excel close;&lt;/PRE&gt;
&lt;P&gt;The by is a key line here, as is the sheet_interval option.&lt;/P&gt;
&lt;P&gt;That should get you started.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 08:47:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/505005#M72771</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-17T08:47:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I loop a SQL Query - Different Variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/505034#M72772</link>
      <description>&lt;P&gt;Hi RW9, thanks for a speedy reply &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I should have clarified the output&amp;nbsp;goal better...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The final outcome I am trying to reach is to have a separate excel document for each account.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As it stands, my code will run 4 queries and produce an excel spreadsheet that contains 4 worksheets (for each query).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can run this code one account at a time for each account, but hoping to speed up the process for large batches...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My thinking was to loop the accounts from&amp;nbsp;my&amp;nbsp;list into the "&amp;nbsp;&lt;SPAN&gt;%let Master_Account = "123456789";&amp;nbsp;&lt;/SPAN&gt;" section of the code.. this would allow the code to generate a separate spreadsheet for each set of queries.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What are your thoughts?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 11:58:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/505034#M72772</guid>
      <dc:creator>Adam93</dc:creator>
      <dc:date>2018-10-17T11:58:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do I loop a SQL Query - Different Variables</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/505037#M72773</link>
      <description>&lt;P&gt;In which case use call execute:&lt;/P&gt;
&lt;PRE&gt;/* Import list */&lt;BR /&gt;proc import ... out=list;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/* Produce report */&lt;BR /&gt;data _null_;&lt;BR /&gt; set list;&lt;BR /&gt; call execute(cats('ods excel file="',account,'.xml";'));&lt;BR /&gt; call execute(cats('proc report data=want nowd; by account; where account="',account,'"; run;'));&lt;BR /&gt; call execute('ods excel close;');&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;So for each row in list, the ods and proc report gets generated.&amp;nbsp; In the above, the filename is called what is in the row of account from list.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 12:09:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-do-I-loop-a-SQL-Query-Different-Variables/m-p/505037#M72773</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-10-17T12:09:33Z</dc:date>
    </item>
  </channel>
</rss>

