<?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: Time weighted average for non-Zero data point in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Time-weighted-average-for-non-Zero-data-point/m-p/803874#M316552</link>
    <description>&lt;P&gt;What you describe is the weighted sum of the observations for which v^=0, assuming that the Date variable is positive. If you don't have to use the DATA step, I would write this as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=Have mean; 
   where v ^= 0;
   class id;
   var v;
   weight Date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do have to use the DATA step, then this is an ideal situation to &lt;A href="https://blogs.sas.com/content/iml/2018/02/26/how-to-use-first-variable-and-last-variable-in-a-by-group-analysis-in-sas.html" target="_self"&gt;use a BY-group analysis and the FIRST.ID and LAST.ID variables&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Want;
set Have(where=(v^=0));
by id;
if first.id then do;
   sum = 0;  sumw = 0;
end;
sum + date*v;
sumw + date;
if last.id then do;
   time_weighted_average = sum / sumw;
   output;
end;
keep time_weighted_average;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 24 Mar 2022 19:12:25 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2022-03-24T19:12:25Z</dc:date>
    <item>
      <title>Time weighted average for non-Zero data point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-weighted-average-for-non-Zero-data-point/m-p/803839#M316538</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;I want to calculate a time-weighted average for non-zero series and the procedure is as below:&lt;/P&gt;
&lt;P&gt;For the data below,&lt;/P&gt;
&lt;P&gt;For day 4, ID=1: I have 3 non-zero value: &lt;BR /&gt;1 1 6&lt;BR /&gt;2 1 4&lt;BR /&gt;&lt;STRIKE&gt;&lt;EM&gt;3 1 0&lt;/EM&gt;&lt;/STRIKE&gt;&lt;BR /&gt;4 1 -7&lt;/P&gt;
&lt;P&gt;the average will be: (sumproduct of value and day)/(sum of day) = (1*6 + 2*4 + 4*-7)/(1+2+4)&lt;/P&gt;
&lt;P&gt;My code below works fine but I know you always have a faster code and I would like to learn about your method.&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input date id v ;
datalines;
1 1 6
2 1 4
3 1 0
4 1 -7
5 1 5
1 2 1
2 2 0
3 2 -2
4 2 2
5 2 5
6 2 1
;run;

proc sort data=have; by id descending date;run;

data want;
set have;
drop d1 id1 v1;
sum_value=0;
sum_date=0;

do n=_N_ to _N_+3;
	set have (rename = (date=d1 id=id1 v=v1)) point = n;
			if id=id1 and v1^=0 then do;
				sum_value=sum_value+v1*d1;
				sum_date=sum_date+d1;
			end;
end;
time_weighted_average = sum_value/sum_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2022 16:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-weighted-average-for-non-Zero-data-point/m-p/803839#M316538</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-03-24T16:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Time weighted average for non-Zero data point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-weighted-average-for-non-Zero-data-point/m-p/803874#M316552</link>
      <description>&lt;P&gt;What you describe is the weighted sum of the observations for which v^=0, assuming that the Date variable is positive. If you don't have to use the DATA step, I would write this as&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=Have mean; 
   where v ^= 0;
   class id;
   var v;
   weight Date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you do have to use the DATA step, then this is an ideal situation to &lt;A href="https://blogs.sas.com/content/iml/2018/02/26/how-to-use-first-variable-and-last-variable-in-a-by-group-analysis-in-sas.html" target="_self"&gt;use a BY-group analysis and the FIRST.ID and LAST.ID variables&lt;/A&gt;.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Want;
set Have(where=(v^=0));
by id;
if first.id then do;
   sum = 0;  sumw = 0;
end;
sum + date*v;
sumw + date;
if last.id then do;
   time_weighted_average = sum / sumw;
   output;
end;
keep time_weighted_average;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Mar 2022 19:12:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-weighted-average-for-non-Zero-data-point/m-p/803874#M316552</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-03-24T19:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Time weighted average for non-Zero data point</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Time-weighted-average-for-non-Zero-data-point/m-p/803875#M316553</link>
      <description>&lt;P&gt;Why do you only want to read 4 of the 5 observations for ID = 1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check out the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p0ctnx21fdgs7qn1jgolu1ihl7kf.htm" target="_self"&gt;Weight Statement of the Proc Summary&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2022 19:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Time-weighted-average-for-non-Zero-data-point/m-p/803875#M316553</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-03-24T19:14:06Z</dc:date>
    </item>
  </channel>
</rss>

