<?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 SQL Loop in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23123#M5085</link>
    <description>Hi,&lt;BR /&gt;
I'm trying to optimize a SQL loop like this&lt;BR /&gt;
&lt;BR /&gt;
%do i=1 %to 100 %by 2;&lt;BR /&gt;
&lt;BR /&gt;
	proc sql;&lt;BR /&gt;
	create table table&amp;amp;i as&lt;BR /&gt;
	select x , idx, z, sum(y) as tot&amp;amp;i from database	&lt;BR /&gt;
                where idx between &amp;amp;i and %eval(&amp;amp;i+1)&lt;BR /&gt;
	group by x;&lt;BR /&gt;
	quit;&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
This loop creates 50 little tables referring to the variable in the where clause. After that I merge all the tables. The time process is very long, probably ralated to the huge database (3.000.000 records, 10GB). How can i avoid the 50 tables?&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance&lt;BR /&gt;
&lt;BR /&gt;
Simone</description>
    <pubDate>Mon, 07 Mar 2011 09:54:45 GMT</pubDate>
    <dc:creator>HDSimo</dc:creator>
    <dc:date>2011-03-07T09:54:45Z</dc:date>
    <item>
      <title>SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23123#M5085</link>
      <description>Hi,&lt;BR /&gt;
I'm trying to optimize a SQL loop like this&lt;BR /&gt;
&lt;BR /&gt;
%do i=1 %to 100 %by 2;&lt;BR /&gt;
&lt;BR /&gt;
	proc sql;&lt;BR /&gt;
	create table table&amp;amp;i as&lt;BR /&gt;
	select x , idx, z, sum(y) as tot&amp;amp;i from database	&lt;BR /&gt;
                where idx between &amp;amp;i and %eval(&amp;amp;i+1)&lt;BR /&gt;
	group by x;&lt;BR /&gt;
	quit;&lt;BR /&gt;
%end;&lt;BR /&gt;
&lt;BR /&gt;
This loop creates 50 little tables referring to the variable in the where clause. After that I merge all the tables. The time process is very long, probably ralated to the huge database (3.000.000 records, 10GB). How can i avoid the 50 tables?&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance&lt;BR /&gt;
&lt;BR /&gt;
Simone</description>
      <pubDate>Mon, 07 Mar 2011 09:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23123#M5085</guid>
      <dc:creator>HDSimo</dc:creator>
      <dc:date>2011-03-07T09:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23124#M5086</link>
      <description>How is the table DATABASE sorted?</description>
      <pubDate>Mon, 07 Mar 2011 13:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23124#M5086</guid>
      <dc:creator>DBailey</dc:creator>
      <dc:date>2011-03-07T13:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23125#M5087</link>
      <description>How do you merge the tables at the end?&lt;BR /&gt;
Pls describe the "business problem". Knowing more about what you are trying to accomplish will help finding alternatives.&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Mon, 07 Mar 2011 13:35:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23125#M5087</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2011-03-07T13:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23126#M5088</link>
      <description>Hi,&lt;BR /&gt;
database is not sorted. The code that i have posted is a simpler version of the original (it contains subquery). I'm trying to calculate different "sums" (idx could be a time period).&lt;BR /&gt;
&lt;BR /&gt;
Thanks</description>
      <pubDate>Mon, 07 Mar 2011 13:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23126#M5088</guid>
      <dc:creator>HDSimo</dc:creator>
      <dc:date>2011-03-07T13:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23127#M5089</link>
      <description>This still does not explain why you need to split and merge the data.&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Mon, 07 Mar 2011 13:53:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23127#M5089</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2011-03-07T13:53:28Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23128#M5090</link>
      <description>Hi Linus,&lt;BR /&gt;
i split and then merge the data because I don't know if with the proc SQL i can calculate n sum with different where clause.</description>
      <pubDate>Mon, 07 Mar 2011 13:59:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23128#M5090</guid>
      <dc:creator>HDSimo</dc:creator>
      <dc:date>2011-03-07T13:59:57Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23129#M5091</link>
      <description>Hmmm...&lt;BR /&gt;
Your idx column aren't used as group by column nor as a input to a statistical function. This means that your data aren't summarized at all...?&lt;BR /&gt;
Can't still see why you are using this logic, why you can't use an ordinary sum()/group by? What is your final delivery?&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Mon, 07 Mar 2011 14:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23129#M5091</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2011-03-07T14:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23130#M5092</link>
      <description>First of all, sorry but i have 2 problem: SAS and english language.&lt;BR /&gt;
I don't use idx as group because:&lt;BR /&gt;
idx 1 =period 1&lt;BR /&gt;
idx 2 =period 2&lt;BR /&gt;
Data are summarized selecting period 1 and 2, period 3 and 4.....&lt;BR /&gt;
With 100 idx values i should have 50 different sums...</description>
      <pubDate>Mon, 07 Mar 2011 14:18:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23130#M5092</guid>
      <dc:creator>HDSimo</dc:creator>
      <dc:date>2011-03-07T14:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23131#M5093</link>
      <description>If you run your program, you should probably see something like this in the log:&lt;BR /&gt;
&lt;BR /&gt;
NOTE: The query requires remerging summary statistics back with the original data.&lt;BR /&gt;
&lt;BR /&gt;
Which means that your result will have detail data in your output data.&lt;BR /&gt;
&lt;BR /&gt;
Still knowing exactly what you want, but I assume you want to sum two periods together in each output record.&lt;BR /&gt;
Try to have some mechanism that maps two periods together. Since I don't know what kind of periods we're talking about, I can't give a complete solution. One way if you could create a SAS format that maps periods pairwise to a single value, and apply it in the SELECT clause. Then you could do like this:&lt;BR /&gt;
&lt;BR /&gt;
/* Create format before this step */&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table table&amp;amp;i as&lt;BR /&gt;
select x , put(idx,MyPeriodFmt.) as id, z, sum(y) as tot_y &lt;BR /&gt;
from database&lt;BR /&gt;
group by x, calculated id, z;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
If our periods are dates or months you can probably us some SAS functions to group periods direclty in the SQL.&lt;BR /&gt;
&lt;BR /&gt;
Conclussion, no need for looping and merging data afterwards, whic will probably save a lot of processing time.&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Mon, 07 Mar 2011 15:39:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23131#M5093</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2011-03-07T15:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23132#M5094</link>
      <description>I think the other commenters are correct..but I believe you would experience significant performance gains if you sorted the table correctly or created appropriate indices.</description>
      <pubDate>Mon, 07 Mar 2011 16:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23132#M5094</guid>
      <dc:creator>DBailey</dc:creator>
      <dc:date>2011-03-07T16:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23133#M5095</link>
      <description>&amp;gt; Hi Linus,&lt;BR /&gt;
&amp;gt; i split and then merge the data because I don't know&lt;BR /&gt;
&amp;gt; if with the proc SQL i can calculate n sum with&lt;BR /&gt;
&amp;gt; different where clause.&lt;BR /&gt;
&lt;BR /&gt;
although you cannot apply different where clauses, you can achieve the effect with the CASE-WHEN-ELSE structure&lt;BR /&gt;
This example takes the macro loop inside the select statement to create 30 new columns tot1 to tot30.&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table table_all_30 as&lt;BR /&gt;
select x &lt;BR /&gt;
, idx, z&lt;BR /&gt;
%do i= 1 to 30 ;&lt;BR /&gt;
, case when idx GE &amp;amp;i and idx LE %eval(&amp;amp;i+1) &lt;BR /&gt;
    then sum(y) else 0 end as tot&amp;amp;i &lt;BR /&gt;
%end &lt;BR /&gt;
from database &lt;BR /&gt;
group by x, idx, z &lt;BR /&gt;
;&lt;BR /&gt;
quit;

naming the new columns tot1 to tot30 ( not idx1 to idx30)&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: Peter.C</description>
      <pubDate>Mon, 07 Mar 2011 17:29:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23133#M5095</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-03-07T17:29:04Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23134#M5096</link>
      <description>Hello HDSimo,&lt;BR /&gt;
&lt;BR /&gt;
Try to use views instead on your 50 tables. In this case you create a real table only when you merge all views.&lt;BR /&gt;
&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Mon, 07 Mar 2011 19:12:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23134#M5096</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2011-03-07T19:12:38Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23135#M5097</link>
      <description>Hi.Maybe you write code like this to avoid 50 dataset.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table result as&lt;BR /&gt;
&lt;BR /&gt;
select x , idx, z, sum(y) as tot&amp;amp;i from database&lt;BR /&gt;
             where idx between 1 and 2&lt;BR /&gt;
                group by x;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
  %do i=2 %to 100 %by 2;&lt;BR /&gt;
&lt;BR /&gt;
   union all corresponding&lt;BR /&gt;
&lt;BR /&gt;
   select x , idx, z, sum(y) as tot&amp;amp;i from database&lt;BR /&gt;
             where idx between &amp;amp;i and %eval(&amp;amp;i+1)&lt;BR /&gt;
                group by x;&lt;BR /&gt;
&lt;BR /&gt;
           &lt;BR /&gt;
    %end;&lt;BR /&gt;
&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 08 Mar 2011 02:31:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23135#M5097</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-03-08T02:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: SQL Loop</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23136#M5098</link>
      <description>Thanks!&lt;BR /&gt;
A lot of good advices!&lt;BR /&gt;
&lt;BR /&gt;
Thank you again</description>
      <pubDate>Tue, 08 Mar 2011 08:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-Loop/m-p/23136#M5098</guid>
      <dc:creator>HDSimo</dc:creator>
      <dc:date>2011-03-08T08:01:40Z</dc:date>
    </item>
  </channel>
</rss>

