<?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: Do loop in PROC SQL in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop-in-PROC-SQL/m-p/230472#M5654</link>
    <description>&lt;P&gt;Thank you so much. It works like charm.&lt;/P&gt;</description>
    <pubDate>Sun, 18 Oct 2015 10:45:12 GMT</pubDate>
    <dc:creator>Ujjawal</dc:creator>
    <dc:date>2015-10-18T10:45:12Z</dc:date>
    <item>
      <title>Do loop in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop-in-PROC-SQL/m-p/230378#M5651</link>
      <description>&lt;P&gt;I want the following PROC SQL code to be run for multiple variables. Till now, it is designed for a single variable i.e. Balance. I want this to be run for multiple variables in a loop. The code calculates ratio and total sum of balance for a time period.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Data Sets used in the code -&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example1;&lt;BR /&gt;input ID Months Revenue Balance;&lt;BR /&gt;cards;&lt;BR /&gt;101 1 3 90&lt;BR /&gt;101 2 33 68&lt;BR /&gt;101 3 22 51&lt;BR /&gt;101 4 3 90&lt;BR /&gt;101 5 33 65&lt;BR /&gt;101 6 22 54&lt;BR /&gt;101 7 3 92&lt;BR /&gt;101 8 33 65&lt;BR /&gt;101 9 22 55&lt;BR /&gt;101 10 3 96&lt;BR /&gt;101 11 33 65&lt;BR /&gt;101 12 22 54&lt;BR /&gt;102 1 100 18&lt;BR /&gt;102 2 58 62&lt;BR /&gt;102 3 95 97&lt;BR /&gt;102 4 100 18&lt;BR /&gt;102 5 58 65&lt;BR /&gt;102 6 95 92&lt;BR /&gt;102 7 100 11&lt;BR /&gt;102 8 58 62&lt;BR /&gt;102 9 95 92&lt;BR /&gt;102 10 100 15&lt;BR /&gt;102 11 58 60&lt;BR /&gt;102 12 95 91&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data trend;&lt;BR /&gt;input x y;&lt;BR /&gt;cards;&lt;BR /&gt;1 3&lt;BR /&gt;4 6&lt;BR /&gt;1 6&lt;BR /&gt;7 9&lt;BR /&gt;1 9&lt;BR /&gt;10 12&lt;BR /&gt;1 12&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;BR /&gt;select cat("sum(case when Months =",y," then Balance else . end)", "/ sum(case when Months =",x," then Balance else . end)&lt;BR /&gt;as Balance_",y,"_",x,"_ratio")&lt;BR /&gt;into :ratio separated by ","&lt;BR /&gt;from trend;&lt;BR /&gt;select cat("sum(case when ", x,"&amp;lt;= Months &amp;lt;=",y, " then Balance else . end) as Balance_",x,"_",y,"_Sum")&lt;BR /&gt;into :loop separated by ","&lt;BR /&gt;from trend;&lt;BR /&gt;create table Output_trend as&lt;BR /&gt;select ID, &amp;amp;ratio., &amp;amp;loop.,&amp;nbsp;&lt;BR /&gt;from example1&lt;BR /&gt;group by ID;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;For example :&lt;/STRONG&gt; Variables are specified&amp;nbsp;in the following "vars" macro paramer and then run the above PROC SQL code for the variables mentioned in the macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let vars = Balance Revenue;&lt;BR /&gt;%let n=%sysfunc(countw(&amp;amp;vars,%str( )));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Oct 2015 21:12:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop-in-PROC-SQL/m-p/230378#M5651</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2015-10-16T21:12:56Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop-in-PROC-SQL/m-p/230393#M5653</link>
      <description>&lt;P&gt;You can do this without macro programming. Transpose your dataset to a long table format and summarize your data in a single query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example1;
input ID Month Revenue Balance;
cards;
101 1 3 90
101 2 33 68
101 3 22 51
101 4 3 90
101 5 33 65
101 6 22 54
101 7 3 92
101 8 33 65
101 9 22 55
101 10 3 96
101 11 33 65
101 12 22 54
102 1 100 18
102 2 58 62
102 3 95 97
102 4 100 18
102 5 58 65
102 6 95 92
102 7 100 11
102 8 58 62
102 9 95 92
102 10 100 15
102 11 58 60
102 12 95 91
;

data trends;
input m1 m2;
cards;
1 3
4 6
1 6
7 9
1 9
10 12
1 12
;

proc transpose data=example1 out=ex1 name=variable prefix=x;
by id month;
run;

proc sql;
create table trendList as
select variable, id, m1, m2
from trends cross join (select unique id, variable from ex1);
create table exTrends as
select catx("_", t.variable, t.m2, t.m1, "Ratio") as label length=32,
       id, variable, m2, m1,
       (select sum(x1) from ex1 
        where t.m1=month and t.id=id and t.variable=variable) /
       (select sum(x1) from ex1 
        where t.m2=month and t.id=id and t.variable=variable) as  value
from trendList as t
union all
select catx("_", t.variable, t.m2, t.m1, "Sum") as label length=32,
       id, variable, m2, m1,
       (select sum(x1) from ex1 
        where month between t.m1 and t.m2 and t.id=id and t.variable=variable) as  value
from trendList as t
order by id, variable, m2, m1, label;
quit;

proc transpose data=exTrends out=exTable(drop=_name_);
by id;
id label;
var value;
run;

proc print data=exTable noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Oct 2015 03:28:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop-in-PROC-SQL/m-p/230393#M5653</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-10-17T03:28:30Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop in PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Do-loop-in-PROC-SQL/m-p/230472#M5654</link>
      <description>&lt;P&gt;Thank you so much. It works like charm.&lt;/P&gt;</description>
      <pubDate>Sun, 18 Oct 2015 10:45:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Do-loop-in-PROC-SQL/m-p/230472#M5654</guid>
      <dc:creator>Ujjawal</dc:creator>
      <dc:date>2015-10-18T10:45:12Z</dc:date>
    </item>
  </channel>
</rss>

