<?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: PROC SQL - restric sum function to non-missing values in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290973#M59826</link>
    <description>&lt;P&gt;A bit tricky,&amp;nbsp;especially if you want to solve this within a single query.&lt;/P&gt;
&lt;P&gt;But if you don't want to sum&amp;nbsp;certain groups, shall they still be part of the&amp;nbsp;output (as missing)?&lt;/P&gt;
&lt;P&gt;If so, you could try to use the nmiss() aggregate function in&amp;nbsp;combination with the sum().&lt;/P&gt;</description>
    <pubDate>Thu, 11 Aug 2016 13:48:36 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2016-08-11T13:48:36Z</dc:date>
    <item>
      <title>PROC SQL - restric sum function to non-missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290955#M59822</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the pace and level of detail of the help I received thus far on this forum is really astonishing.&lt;/P&gt;&lt;P&gt;I am ashamed to come up with a next, my now fourth, issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This time, I want to restric the "sum" statement in a "PROC SQL" step to return values only, if all variables are available.&lt;/P&gt;&lt;P&gt;(would be great if I could choose from which amount of missing values on the sum-function shall return a missing value, but this only as a side note),&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The overall-goal is to merge return-data of companies to a data set containing fundamental data of said companies.&lt;/P&gt;&lt;P&gt;I order to achieve this, I would like to sum up, in monthly intervals, continuous returns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;       PROC SQL;
          CREATE TABLE data.expansion
          AS SELECT a.*,
	/* creation of monthly increments */
	INTCK('month',datevar,c.date)   AS   dt,
	log(1+c.discrete_return)                 AS   log_return
          FROM       fundamental_data		      AS   a
          LEFT JOIN  data.return_data             AS  c
          ON        a.firmid = c.firmid 
            AND     a.datevar  &amp;lt; c.date &amp;lt;= intnx('month',a.datevar,12,'E')
            AND     not(missing(a.firmid))
            AND     not(missing(a.datevar))
            AND     not(missing(c.date))
          ORDER BY a.firmid_1, a.datevar, dt;
       QUIT;
       PROC SORT data=data.expansion NODUPKEYS; 
          BY firmid_1 datevar dt;
       RUN;

    
       PROC SQL;
           CREATE TABLE temporaer
           AS SELECT a.*,
                     sum(c.log_return)        AS logret_k12
           FROM      fundamental_data         AS a
           LEFT JOIN data.expansion           AS c
             ON      a.datevar  = c.datevar
             AND     a.firmid     = c.firmid
             AND     1 &amp;lt;= c.dt &amp;lt;= 12
           GROUP BY a.datevar, a.firmid
           HAVING c.dt = max(c.dt);
       QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"Sum" would not calculate "1.st log_return + 2.nd log_return... + last log_return" and thus return a missing value, if one log_return were missing.&lt;/P&gt;&lt;P&gt;I have never "worked" with "PROC SQL" thus far.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be glad, if someone could give me a hint.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2016 12:41:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290955#M59822</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2016-08-11T12:41:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - restric sum function to non-missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290973#M59826</link>
      <description>&lt;P&gt;A bit tricky,&amp;nbsp;especially if you want to solve this within a single query.&lt;/P&gt;
&lt;P&gt;But if you don't want to sum&amp;nbsp;certain groups, shall they still be part of the&amp;nbsp;output (as missing)?&lt;/P&gt;
&lt;P&gt;If so, you could try to use the nmiss() aggregate function in&amp;nbsp;combination with the sum().&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2016 13:48:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290973#M59826</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-08-11T13:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - restric sum function to non-missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290976#M59827</link>
      <description>&lt;P&gt;Its not really clear to me, test data in the form of a datastep and required output generally helps. &amp;nbsp;Conditionals in sql are done by using case statements:&lt;/P&gt;
&lt;P&gt;case when xyz=1 then 1 else 99 end as VAR&lt;/P&gt;
&lt;P&gt;I.e. if xyz-1 then var=1 else var=99. &amp;nbsp;Sum functions however are aggregates, they work over a number of rows in the data defined by the group. &amp;nbsp;You can however restrict what goes into the group by sub-querying the from statement so:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  select   SEX,
           sum(AGE) as AGE_SUM
  from     SASHELP.CLASS
  group by SEX;
quit;&lt;/PRE&gt;
&lt;P&gt;If from that I wanted to drop any records with missings:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  select   SEX,
           sum(AGE) as AGE_SUM
  from     (select * from SASHELP.CLASS where AGE ne .)
  group by SEX;
quit;&lt;/PRE&gt;
&lt;P&gt;So I filter the data in the from clause before the groups and aggregates are created. &amp;nbsp;This is a useful technique to learn. &amp;nbsp;You could also try:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  select   SEX,
           sum(case when AGE=. then 0 else AGE end) as AGE_SUM
  from     SASHELP.CLASS
  group by SEX;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2016 13:54:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290976#M59827</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-11T13:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - restric sum function to non-missing values</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290983#M59829</link>
      <description>&lt;HR /&gt;&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Its not really clear to me, test data in the form of a datastep and required output generally helps.&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am sorry for this obvious dereliction - I should have prepared better, before positing.&lt;BR /&gt;Next time, I should not let panic seize hold of me, if something does not work properly or I cannot solve directly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Conditionals in sql are done by using case statements [...]&lt;/P&gt;&lt;BR /&gt;&lt;P&gt;So I filter the data in the from clause before the groups and aggregates are created. &amp;nbsp;This is a useful technique to learn. [...]&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nonetheless, that was exactly the kind of support I asked for.&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Aug 2016 14:12:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-restric-sum-function-to-non-missing-values/m-p/290983#M59829</guid>
      <dc:creator>Sinistrum</dc:creator>
      <dc:date>2016-08-11T14:12:52Z</dc:date>
    </item>
  </channel>
</rss>

