<?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: I would like to do the loop in proc sql function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436165#M108464</link>
    <description>&lt;P&gt;In the original SQL code, N is a count of number of observations.&amp;nbsp; It's value does not change if one of the incoming variables has a missing value on some observations.&amp;nbsp; Therefore, that count is already part of the output data set from PROC MEANS, contained in the variable _FREQ_.&amp;nbsp; There is no need to calculate the N statistic ... and in fact, it can give a different answer depending on the number of missing values.&amp;nbsp; Here's a variation on the program that applies this idea:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc summary data=have nway;&lt;/P&gt;
&lt;P&gt;class year;&lt;/P&gt;
&lt;P&gt;var t1-t100;&lt;/P&gt;
&lt;P&gt;output out=want (drop=_type_ rename=(_freq_=n t1-t100=sum_t1-sum_t100)) sum=;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Feb 2018 14:11:13 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-02-12T14:11:13Z</dc:date>
    <item>
      <title>I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436143#M108450</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to do the loop the proc sql; function.&lt;/P&gt;&lt;P&gt;Currently, I have a code look like&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table temp1 as select count(*) as N,&lt;/P&gt;&lt;P&gt;sum(T1) as sum_T1,&lt;/P&gt;&lt;P&gt;sum(T2) as sum_T2,&lt;/P&gt;&lt;P&gt;sum(T3) as sum_T3,&lt;/P&gt;&lt;P&gt;.,&lt;/P&gt;&lt;P&gt;.,&lt;/P&gt;&lt;P&gt;.,&lt;/P&gt;&lt;P&gt;sum(T100) as sum_T100&lt;/P&gt;&lt;P&gt;from temp&lt;/P&gt;&lt;P&gt;group by year;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are there any shorter way to code like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 04:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436143#M108450</guid>
      <dc:creator>LengYi</dc:creator>
      <dc:date>2018-02-12T04:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436144#M108451</link>
      <description>&lt;P&gt;Not using SQL, because it doesn't support variable&amp;nbsp;lists.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can however use PROC MEANS to do this easily.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=temp nway stackods;
by year;
var T1-T100;
output out=want sum= /autoname;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151001"&gt;@LengYi&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to do the loop the proc sql; function.&lt;/P&gt;
&lt;P&gt;Currently, I have a code look like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table temp1 as select count(*) as N,&lt;/P&gt;
&lt;P&gt;sum(T1) as sum_T1,&lt;/P&gt;
&lt;P&gt;sum(T2) as sum_T2,&lt;/P&gt;
&lt;P&gt;sum(T3) as sum_T3,&lt;/P&gt;
&lt;P&gt;.,&lt;/P&gt;
&lt;P&gt;.,&lt;/P&gt;
&lt;P&gt;.,&lt;/P&gt;
&lt;P&gt;sum(T100) as sum_T100&lt;/P&gt;
&lt;P&gt;from temp&lt;/P&gt;
&lt;P&gt;group by year;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are there any shorter way to code like this.&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 04:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436144#M108451</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-12T04:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436146#M108453</link>
      <description>&lt;P&gt;Thanks for a quick reply,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What if I would like to have 'N' in the table how can I deliver the N (Total number of datalines)&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 04:06:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436146#M108453</guid>
      <dc:creator>LengYi</dc:creator>
      <dc:date>2018-02-12T04:06:25Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436150#M108456</link>
      <description>&lt;P&gt;Just add n= to the output statement. i.e.:&lt;/P&gt;
&lt;PRE&gt;proc means data=temp nway stackods;
  by year;
  var T1-T100;
  output out=want sum= n= /autoname;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 04:53:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436150#M108456</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-02-12T04:53:36Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436151#M108457</link>
      <description>&lt;P&gt;I think OP wants overall count per year, but this code will count for all T1-T100, which will be basically same but will repeated 100 times like T1_N - T100_N, generally all he wants one column of count per year&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 05:01:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436151#M108457</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-02-12T05:01:05Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436152#M108458</link>
      <description>&lt;P&gt;Yes, this is what I want&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 05:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436152#M108458</guid>
      <dc:creator>LengYi</dc:creator>
      <dc:date>2018-02-12T05:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436154#M108459</link>
      <description>&lt;P&gt;One way I think of is;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the proc means code and then do a&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create temp table as&amp;nbsp;&lt;/P&gt;
&lt;P&gt;select year, count(*) as tot&lt;/P&gt;
&lt;P&gt;from demo&lt;/P&gt;
&lt;P&gt;group by year;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data final;&lt;/P&gt;
&lt;P&gt;merge&amp;nbsp;meanstable temp ;&lt;/P&gt;
&lt;P&gt;by year;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or drop columns in the earlier code&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 05:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436154#M108459</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-02-12T05:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436155#M108460</link>
      <description>Thanks I will try to use the code.</description>
      <pubDate>Mon, 12 Feb 2018 05:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436155#M108460</guid>
      <dc:creator>LengYi</dc:creator>
      <dc:date>2018-02-12T05:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436156#M108461</link>
      <description>&lt;P&gt;or just this to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc means data=temp nway stackods;&lt;BR /&gt; by year;&lt;BR /&gt; var T1-T100;&lt;BR /&gt; output out=want(drop = T2_N--T100_N rename=T1_N = Count) sum= n= /autoname;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 05:15:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436156#M108461</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-02-12T05:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to do the loop in proc sql function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436165#M108464</link>
      <description>&lt;P&gt;In the original SQL code, N is a count of number of observations.&amp;nbsp; It's value does not change if one of the incoming variables has a missing value on some observations.&amp;nbsp; Therefore, that count is already part of the output data set from PROC MEANS, contained in the variable _FREQ_.&amp;nbsp; There is no need to calculate the N statistic ... and in fact, it can give a different answer depending on the number of missing values.&amp;nbsp; Here's a variation on the program that applies this idea:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc summary data=have nway;&lt;/P&gt;
&lt;P&gt;class year;&lt;/P&gt;
&lt;P&gt;var t1-t100;&lt;/P&gt;
&lt;P&gt;output out=want (drop=_type_ rename=(_freq_=n t1-t100=sum_t1-sum_t100)) sum=;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Feb 2018 14:11:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-do-the-loop-in-proc-sql-function/m-p/436165#M108464</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-02-12T14:11:13Z</dc:date>
    </item>
  </channel>
</rss>

