<?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: multiplying observations in a column by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/607959#M176842</link>
    <description>&lt;P&gt;Can you show what code is giving you the output in weeklyret1? That would be helpful to see what's going on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's my code and I get the expected result.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id return relreturn;
datalines;
1 . .
1 -0.055556  0.9444444444
1 -0.009804  0.9901960781
1 -0.009901  0.9900990101
1  0.0
;
run;

proc sql;
create table want as
select id, exp(sum(log(relreturn))) as product format=decimal10.10
from have
group by id
;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;-unison&lt;/P&gt;</description>
    <pubDate>Thu, 28 Nov 2019 05:22:40 GMT</pubDate>
    <dc:creator>unison</dc:creator>
    <dc:date>2019-11-28T05:22:40Z</dc:date>
    <item>
      <title>multiplying observations in a column by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/607928#M176829</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been looking at this post:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/Multiplying-observations-values-in-row-1-by-values-in-row-2-and/td-p/363815" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/Multiplying-observations-values-in-row-1-by-values-in-row-2-and/td-p/363815&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was unable to post a reply so I have created this new question. I want to do something similar to what the above question posted i.e. multiply values of first observation with second observation and so forth by year or date. The solution given in that question does not seem to work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The solution code being:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;create&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;want&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;year, returns, exp(sum(log(returns)))&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;newcol&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;have&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;group&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;year;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I manually calculated for one group in my dataset and saw that the result of the product of the observations is not the same as the result given by the sql code. However when I calculated exp(sum(log(returns))) manually and it matches with the result give by the code. This means that exp(sum(log(returns))) does not calculate the product of the observations in a column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you take a look at the attached screenshot. The dataset in the top/background (weeklyret1) is the result of sql code and the dataset in the front/bottom (weeklyret) is the "have". I have calculated the product of (0.944444444X0.9901960781X0.9900990101X1) for cusip-00002130, fyear-1990, week-33 and the result is 0.92675926037. But the sql code gave the result -0.090601481. But when I calculated exp((log0.944444444)+(log0.9901960781)+(log0.9900990101)+(log1)) then the result matches with that of the sql code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone shed some light in this? And possibly give me a correct way of multiplying observations of a column by group?&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2019 01:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/607928#M176829</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2019-11-28T01:51:27Z</dc:date>
    </item>
    <item>
      <title>Re: multiplying observations in a column by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/607958#M176841</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/300936"&gt;@Adubhai&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I manually calculated for one group in my dataset and saw that the result of the product of the observations is not the same as the result given by the sql code. However when I calculated exp(sum(log(returns))) manually and it matches with the result give by the code. This means that exp(sum(log(returns))) does not calculate the product of the observations in a column.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you take a look at the attached screenshot. The dataset in the top/background (weeklyret1) is the result of sql code and the dataset in the front/bottom (weeklyret) is the "have". I have calculated the product of (0.944444444X0.9901960781X0.9900990101X1) for cusip-00002130, fyear-1990, week-33 and the result is 0.92675926037. But the sql code gave the result -0.090601481. But when I calculated exp((log0.944444444)+(log0.9901960781)+(log0.9900990101)+(log1)) then the result matches with that of the sql code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can someone shed some light in this? And possibly give me a correct way of multiplying observations of a column by group?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not get the result you report for &amp;nbsp;&amp;nbsp;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;0.944444444 * 0.9901960781 * 0.9900990101 * 1).&amp;nbsp; I get&amp;nbsp; 0.9259259254, both via a data step, and via the SQL code:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;If you are not getting the same results, then either the NEWCOL is being modified subsequent to the create table statement in your code, or you have other observations in the calculation.&amp;nbsp; I think it is the latter, because you report using&lt;/SPAN&gt;&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;group by year&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but you should probably be using&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;group by yearwk&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or more likely in your case&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;group by cusip,yearwk&lt;/CODE&gt;&lt;/PRE&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;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: 'HelevticaNeue-light','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;You can check on my supposition by adding a variable (n_returns) to the select clause:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select year, returns, exp(sum(log(returns))) as newcol, n(returns) as  n_returns&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The new variable n_returns will report the number of non-missing returns for each cusip*yearwk group.&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;</description>
      <pubDate>Thu, 28 Nov 2019 05:15:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/607958#M176841</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-11-28T05:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: multiplying observations in a column by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/607959#M176842</link>
      <description>&lt;P&gt;Can you show what code is giving you the output in weeklyret1? That would be helpful to see what's going on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's my code and I get the expected result.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id return relreturn;
datalines;
1 . .
1 -0.055556  0.9444444444
1 -0.009804  0.9901960781
1 -0.009901  0.9900990101
1  0.0
;
run;

proc sql;
create table want as
select id, exp(sum(log(relreturn))) as product format=decimal10.10
from have
group by id
;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;-unison&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2019 05:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/607959#M176842</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2019-11-28T05:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: multiplying observations in a column by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/608627#M177160</link>
      <description>&lt;P&gt;Thanks for the suggestions. Actually I solved the problem another way. I took the arithmetic mean and raised it to the power of the frequency of each group. This does not give the accurate result of the product of observations but it gives very close results and that will work fine with me.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Dec 2019 07:36:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/multiplying-observations-in-a-column-by-group/m-p/608627#M177160</guid>
      <dc:creator>Adubhai</dc:creator>
      <dc:date>2019-12-02T07:36:17Z</dc:date>
    </item>
  </channel>
</rss>

