<?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: Simple Time Weighting in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479993#M124003</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID DataDate : $15. Var1;
cards;
1 Apr101980 50
1 Apr111980 55
1 Apr171980 55
1 Apr191980 40
1 Apr241980 30
1 Feb81981 40
1 Feb101981 40
1 Feb181981 40
1 Feb271981 40
2 March181981 30
2 June271981 27
2 Sept81981 20
;
data temp;
set have;
year=substr(DataDate,length(DataDate)-3);
/*in your real, date should be numeric, so you can do year=year(date)*/
run;


data want;
do n=1 by 1 until(last.year);
set temp;
by id year;
end;
do _n_=1 by 1 until (last.year);
set temp;
by id year;
want=Var1*_n_/n;
output;
end;
drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 20 Jul 2018 17:56:22 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-07-20T17:56:22Z</dc:date>
    <item>
      <title>Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479951#M123968</link>
      <description>&lt;P&gt;Curious how best to go about simple time weighting.&amp;nbsp; Essentially, I have quasi-daily data that I need to weight based upon when in the year it occurred relative to all other data points for that year and ID where the first observation (temporally) receives a weight of 1/n and the last observation receives a weight of n/n or 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for example, if a given ID-year has the following data points (and only these data points):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DataDate Var1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apr101980 50&lt;/P&gt;
&lt;P&gt;Apr111980 55&lt;/P&gt;
&lt;P&gt;Apr171980 55&lt;/P&gt;
&lt;P&gt;Apr191980 40&lt;/P&gt;
&lt;P&gt;Apr241980 30&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then the weights need to be as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DataDate Var1 Weight&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Apr101980 50 1/5&lt;/P&gt;
&lt;P&gt;Apr111980 55 2/5&lt;/P&gt;
&lt;P&gt;Apr171980 55 3/5&lt;/P&gt;
&lt;P&gt;Apr191980 40 4/5&lt;/P&gt;
&lt;P&gt;Apr241980 30 5/5&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Such that the weighted sum for the given ID for the year 1980 would be = 50*1/5 + 55*2/5 + 55*3/5 + 40*4/5 + 30*5/5 = 127.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The actual data set is quite large, includes thousands of IDs, 40+ years, and probably 200+ daily observations (not always the same per year or even per ID in the same year).&amp;nbsp; Any help on where to start would be appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479951#M123968</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-07-20T17:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479956#M123973</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input DataDate :$10. Var1;
n+1;/*creating counter, you could also use n=_n_*/
cards;
Apr101980 50
Apr111980 55
Apr171980 55
Apr191980 40
Apr241980 30
;
proc sql;
create table want(drop=n) as
select *,var1*n/count(*) as weight
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479956#M123973</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-20T17:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479959#M123975</link>
      <description>&lt;P&gt;Wow, is it really that simple?&amp;nbsp; Thanks novinosrin.&amp;nbsp; I'll try it out and see if it works for my larger dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:11:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479959#M123975</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-07-20T17:11:53Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479962#M123978</link>
      <description>&lt;P&gt;assign a sequential number by year, then weighted sum, then divide by N&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assumes data is properly sorted and each record has the value for Year (not&amp;nbsp;date as in the data you provided)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
    set have;
    by year;
    if first.year then seq=0;
    seq+1;
run;

proc summary nway data=have2;
    class year;
    var var1/weight=seq;
    output out=have3 sum=sum_var1;
run;

data want;
    set have3;
    final_answer=sum_var1/_freq_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479962#M123978</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-20T17:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479963#M123979</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input DataDate :$10. Var1;
n+1;/*creating counter, you could also use n=_n_*/
cards;
Apr101980 50
Apr111980 55
Apr171980 55
Apr191980 40
Apr241980 30
;
proc sql;
create table want(drop=n) as
select *,var1*n/count(*) as weight
from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This fails if the data set contains multiple years.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:12:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479963#M123979</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-20T17:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479965#M123981</link>
      <description>&lt;P&gt;Please show a sample output of what you want for your input so I could modify.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;has pointed out my mistake .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, please provide me better representative sample with ID var etc&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:17:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479965#M123981</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-20T17:17:33Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479969#M123985</link>
      <description>If I'm understanding the above code correctly, the proc summary provides the weights, whereas the last data function creates an average, correct?</description>
      <pubDate>Fri, 20 Jul 2018 17:19:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479969#M123985</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-07-20T17:19:23Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479979#M123992</link>
      <description>&lt;P&gt;PROC SUMMARY provides a weighted sum&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data step following divided by the N to turn it into an average&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:34:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479979#M123992</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-20T17:34:49Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479982#M123994</link>
      <description>For an extended sample such as the below:&lt;BR /&gt;ID DataDate Var1&lt;BR /&gt; &lt;BR /&gt;1 Apr101980 50&lt;BR /&gt;1 Apr111980 55&lt;BR /&gt;1 Apr171980 55&lt;BR /&gt;1 Apr191980 40&lt;BR /&gt;1 Apr241980 30&lt;BR /&gt;&lt;BR /&gt;1 Feb81981 40&lt;BR /&gt;1 Feb101981 40&lt;BR /&gt;1 Feb181981 40&lt;BR /&gt;1 Feb271981 40&lt;BR /&gt;&lt;BR /&gt;2 March181981 30&lt;BR /&gt;2 June271981 27&lt;BR /&gt;2 Sept81981 20&lt;BR /&gt;&lt;BR /&gt;I would want the following output&lt;BR /&gt;&lt;BR /&gt;ID DataDate Var1 Weight&lt;BR /&gt;&lt;BR /&gt;1 Apr101980 50 1/5&lt;BR /&gt;1 Apr111980 55 2/5&lt;BR /&gt;1 Apr171980 55 3/5&lt;BR /&gt;1 Apr191980 40 4/5&lt;BR /&gt;1 Apr241980 30 5/5&lt;BR /&gt;&lt;BR /&gt;1 Feb81981 40 1/4&lt;BR /&gt;1 Feb101981 40 2/4&lt;BR /&gt;1 Feb181981 40 3/4&lt;BR /&gt;1 Feb271981 40 4/4&lt;BR /&gt;&lt;BR /&gt;2 March181981 30 1/3&lt;BR /&gt;2 June271981 27 2/3&lt;BR /&gt;2 Sept81981 20 3/3&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Of course, I'm not picky on the weight variable.  It could be two variables, one which contains the temporal order and a second which contains the total n.  I can manipulate to get what I want afterward.  Eventually, the weights will be applied to the variable to get a sum for the year.&lt;BR /&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:39:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479982#M123994</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-07-20T17:39:08Z</dc:date>
    </item>
    <item>
      <title>Re: Simple Time Weighting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479993#M124003</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID DataDate : $15. Var1;
cards;
1 Apr101980 50
1 Apr111980 55
1 Apr171980 55
1 Apr191980 40
1 Apr241980 30
1 Feb81981 40
1 Feb101981 40
1 Feb181981 40
1 Feb271981 40
2 March181981 30
2 June271981 27
2 Sept81981 20
;
data temp;
set have;
year=substr(DataDate,length(DataDate)-3);
/*in your real, date should be numeric, so you can do year=year(date)*/
run;


data want;
do n=1 by 1 until(last.year);
set temp;
by id year;
end;
do _n_=1 by 1 until (last.year);
set temp;
by id year;
want=Var1*_n_/n;
output;
end;
drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:56:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Simple-Time-Weighting/m-p/479993#M124003</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-20T17:56:22Z</dc:date>
    </item>
  </channel>
</rss>

