<?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: Summing the Future for a Given Time Period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44966#M9211</link>
    <description>PROC SQL; will do it fairly reasonably.  However, your question is not well framed as I can interpret it at least two ways that yield very different results.  Perhaps you could show an example with your base data and desired results.&lt;BR /&gt;
&lt;BR /&gt;
Doc Muhlbaier&lt;BR /&gt;
Duke</description>
    <pubDate>Wed, 06 Apr 2011 02:00:43 GMT</pubDate>
    <dc:creator>Doc_Duke</dc:creator>
    <dc:date>2011-04-06T02:00:43Z</dc:date>
    <item>
      <title>Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44965#M9210</link>
      <description>Hello kind person,&lt;BR /&gt;
&lt;BR /&gt;
In a large data set I have observations consisting of variables ID, Date, and X.  I want to form a new variable, Y,  that for each observation, is the sum of X for all observations that occurred in the 120 days following that observation, for that ID.&lt;BR /&gt;
&lt;BR /&gt;
I realize that I can sort by descending date to make this problem a little easier.  Using a specified number of lags is difficult because 120 days will contain different numbers of observations.&lt;BR /&gt;
&lt;BR /&gt;
Any suggestions as to how to create this variable are greatly appreciated.</description>
      <pubDate>Tue, 05 Apr 2011 23:52:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44965#M9210</guid>
      <dc:creator>SColby</dc:creator>
      <dc:date>2011-04-05T23:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44966#M9211</link>
      <description>PROC SQL; will do it fairly reasonably.  However, your question is not well framed as I can interpret it at least two ways that yield very different results.  Perhaps you could show an example with your base data and desired results.&lt;BR /&gt;
&lt;BR /&gt;
Doc Muhlbaier&lt;BR /&gt;
Duke</description>
      <pubDate>Wed, 06 Apr 2011 02:00:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44966#M9211</guid>
      <dc:creator>Doc_Duke</dc:creator>
      <dc:date>2011-04-06T02:00:43Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44967#M9212</link>
      <description>Thank you,&lt;BR /&gt;
&lt;BR /&gt;
Consider the following:&lt;BR /&gt;
&lt;BR /&gt;
ID     Date        X        Desired  Y&lt;BR /&gt;
1         10000      0              1&lt;BR /&gt;
1         10000      1              1&lt;BR /&gt;
1         10010      0              2&lt;BR /&gt;
1         10119      1              2&lt;BR /&gt;
1         10121      1              1&lt;BR /&gt;
1         10125      0              1&lt;BR /&gt;
1         10131      1              0&lt;BR /&gt;
2         90000      0              1&lt;BR /&gt;
2         90001      1              0&lt;BR /&gt;
2         90150      1              1

Message was edited by: SColby</description>
      <pubDate>Wed, 06 Apr 2011 05:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44967#M9212</guid>
      <dc:creator>SColby</dc:creator>
      <dc:date>2011-04-06T05:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44968#M9213</link>
      <description>I am confused &lt;BR /&gt;
[pre]&lt;BR /&gt;
ID Date X Desired Y&lt;BR /&gt;
1 10000 0 1  &amp;lt;---- Is it correct ? in 120 days ,it should include '10119'&lt;BR /&gt;
1 10000 1 1&lt;BR /&gt;
1 10010 0 2&lt;BR /&gt;
1 10119 1 2&lt;BR /&gt;
1 10121 1 1&lt;BR /&gt;
1 10125 0 1&lt;BR /&gt;
1 10131 1 0&lt;BR /&gt;
2 90000 0 1&lt;BR /&gt;
2 90001 1 0&lt;BR /&gt;
2 90150 1 1&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: Ksharp</description>
      <pubDate>Wed, 06 Apr 2011 10:29:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44968#M9213</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-06T10:29:23Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44969#M9214</link>
      <description>Perhaps something like this ?&lt;BR /&gt;
[pre]data sample_input;&lt;BR /&gt;
  input ID Date X;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 10000 0&lt;BR /&gt;
1 10000 1&lt;BR /&gt;
1 10010 0&lt;BR /&gt;
1 10119 1&lt;BR /&gt;
1 10121 1&lt;BR /&gt;
1 10125 0&lt;BR /&gt;
1 10131 1&lt;BR /&gt;
2 90000 0&lt;BR /&gt;
2 90001 1&lt;BR /&gt;
2 90150 1&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
proc sql;&lt;BR /&gt;
  create table sample_output as&lt;BR /&gt;
  select ID&lt;BR /&gt;
       , date&lt;BR /&gt;
       , x&lt;BR /&gt;
       , (select sum(b.x) from sample_input b where a.id=b.id and b.date between a.date and a.date+120) as y&lt;BR /&gt;
    from sample_input a&lt;BR /&gt;
;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 06 Apr 2011 11:57:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44969#M9214</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2011-04-06T11:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44970#M9215</link>
      <description>Yes, that is correct because the next observation with x = 1 occurred on the same date, and so does not count.  The "1" in the Y column comes from the 10119 observation.&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Scott</description>
      <pubDate>Wed, 06 Apr 2011 20:26:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44970#M9215</guid>
      <dc:creator>SColby</dc:creator>
      <dc:date>2011-04-06T20:26:00Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44971#M9216</link>
      <description>Zineg,&lt;BR /&gt;
&lt;BR /&gt;
After being confused about the meaning of "a" and "b" I am able to get your code to work.  Should I think of their use as denoting two separate copies of the data set?&lt;BR /&gt;
&lt;BR /&gt;
I am very happy that this code has worked, and I hate to be greedy, but are there any obvious ways to make this procedure more efficient?  I have to do this to 51 data sets that each contain ~150,000 observations.  It took 32 minutes to do it to one.  (I have already parsed the data set to only those variables I need for this operation.)&lt;BR /&gt;
&lt;BR /&gt;
Thanks again,&lt;BR /&gt;
Scott&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: SColby

Message was edited by: SColby</description>
      <pubDate>Wed, 06 Apr 2011 20:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44971#M9216</guid>
      <dc:creator>SColby</dc:creator>
      <dc:date>2011-04-06T20:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44972#M9217</link>
      <description>OK.&lt;BR /&gt;
But I have another question.&lt;BR /&gt;
[pre]&lt;BR /&gt;
ID Date X Desired Y&lt;BR /&gt;
1 10000 0 1&lt;BR /&gt;
1 10000 1 1&lt;BR /&gt;
1 10010 0 2&lt;BR /&gt;
1 10119 1 2&lt;BR /&gt;
1 10121 1 1&lt;BR /&gt;
1 10125 0 1&lt;BR /&gt;
1 10131 1 0&amp;lt;-----it is zero.&lt;BR /&gt;
2 90000 0 1&lt;BR /&gt;
2 90001 1 0&lt;BR /&gt;
2 90150 1 1&amp;lt;-------is it correct ?but the above group is 0.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Assuming it is 0.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data temp;&lt;BR /&gt;
  input ID Date X;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 10000 0&lt;BR /&gt;
1 10000 1&lt;BR /&gt;
1 10010 0&lt;BR /&gt;
1 10119 1&lt;BR /&gt;
1 10121 1&lt;BR /&gt;
1 10125 0&lt;BR /&gt;
1 10131 1&lt;BR /&gt;
2 90000 0&lt;BR /&gt;
2 90001 1&lt;BR /&gt;
2 90150 1&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=temp;&lt;BR /&gt;
 by id date;&lt;BR /&gt;
run;&lt;BR /&gt;
data result(drop=_: count);&lt;BR /&gt;
 set temp end=last;&lt;BR /&gt;
 count+1;&lt;BR /&gt;
 y=0; &lt;BR /&gt;
 if not last then do; &lt;BR /&gt;
                    _count=count ;&lt;BR /&gt;
                    do until(_date-date gt 120 or id ne _id);&lt;BR /&gt;
                     _count+1;&lt;BR /&gt;
                     set temp(rename=(id=_id date=_date x=_x)) point=_count;&lt;BR /&gt;
                     if _date-date ge 1 and _date-date le 120 and id eq _id then y+_x;&lt;BR /&gt;
                    end;&lt;BR /&gt;
                   end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp

Message was edited by: Ksharp</description>
      <pubDate>Thu, 07 Apr 2011 03:00:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44972#M9217</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-07T03:00:44Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44973#M9218</link>
      <description>The quick solution is to sort your data by id and date AND add an index - try that and see how that performs.</description>
      <pubDate>Thu, 07 Apr 2011 11:34:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44973#M9218</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2011-04-07T11:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44974#M9219</link>
      <description>You are correct: that last 1 under "Desired Y" ought to be 0.&lt;BR /&gt;
&lt;BR /&gt;
I ran that code and it is still running 4 hours later.  Maybe I adapted it incorrectly, but for now I am going to work with the solution offered by Zineg.&lt;BR /&gt;
&lt;BR /&gt;
Thank for your interest and help.  This has been tremendously useful.&lt;BR /&gt;
&lt;BR /&gt;
Scott</description>
      <pubDate>Thu, 07 Apr 2011 18:31:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44974#M9219</guid>
      <dc:creator>SColby</dc:creator>
      <dc:date>2011-04-07T18:31:36Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44975#M9220</link>
      <description>May be proc sort waste lots of your time.&lt;BR /&gt;
If your data has sorted so plz remove it.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Fri, 08 Apr 2011 01:04:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44975#M9220</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-08T01:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44976#M9221</link>
      <description>Hi.&lt;BR /&gt;
But after run Zineg 's code I found a problem:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data sample_input;&lt;BR /&gt;
  input ID Date X;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 10000 0&lt;BR /&gt;
1 10000 1&lt;BR /&gt;
1 10010 0&lt;BR /&gt;
1 10119 1&lt;BR /&gt;
1 10121 1&lt;BR /&gt;
1 10125 0&lt;BR /&gt;
1 10131 1&lt;BR /&gt;
2 90000 0&lt;BR /&gt;
2 90001 1&lt;BR /&gt;
2 90150 1&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
proc sql;&lt;BR /&gt;
  create table sample_output as&lt;BR /&gt;
  select ID&lt;BR /&gt;
       , date&lt;BR /&gt;
       , x&lt;BR /&gt;
       , (select sum(b.x) from sample_input b where a.id=b.id and b.date between a.date+1 and a.date+120) as y&lt;BR /&gt;
    from sample_input a&lt;BR /&gt;
;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
proc print noobs ;run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
                                       ID     Date    X    y&lt;BR /&gt;
&lt;BR /&gt;
                                         1    10000    0    1&lt;BR /&gt;
                                         1    10000    1    1&lt;BR /&gt;
                                         1    10010    0    2&lt;BR /&gt;
                                         1    10119    1    2&lt;BR /&gt;
                                         1    10121    1    1&lt;BR /&gt;
                                         1    10125    0    1&lt;BR /&gt;
                                         1    10131    1    .&amp;lt;-------it should be zero&lt;BR /&gt;
                                         2    90000    0    1&lt;BR /&gt;
                                         2    90001    1    .&amp;lt;------it should be zero&lt;BR /&gt;
                                         2    90150    1    .&amp;lt;------it should be zero&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
It is what you can accept?&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp

Message was edited by: Ksharp</description>
      <pubDate>Fri, 08 Apr 2011 01:44:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44976#M9221</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-08T01:44:15Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44977#M9222</link>
      <description>That is interesting because when I ran it on the actual data set there are no missing values generated and everything works fine.  Where there should be zeros there are zeros.  I get the same thing as you when I run the test data.  Hm.&lt;BR /&gt;
&lt;BR /&gt;
The data is presorted so I don't sort first.&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Scott</description>
      <pubDate>Fri, 08 Apr 2011 07:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44977#M9222</guid>
      <dc:creator>SColby</dc:creator>
      <dc:date>2011-04-08T07:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44978#M9223</link>
      <description>If you like sql.I fixed it.&lt;BR /&gt;
[pre]&lt;BR /&gt;
data sample_input;&lt;BR /&gt;
  input ID Date X;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 10000 0&lt;BR /&gt;
1 10000 1&lt;BR /&gt;
1 10010 0&lt;BR /&gt;
1 10119 1&lt;BR /&gt;
1 10121 1&lt;BR /&gt;
1 10125 0&lt;BR /&gt;
1 10131 1&lt;BR /&gt;
2 90000 0&lt;BR /&gt;
2 90001 1&lt;BR /&gt;
2 90150 1&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
 &lt;BR /&gt;
proc sql;&lt;BR /&gt;
  create table sample_output as&lt;BR /&gt;
  select ID&lt;BR /&gt;
       , date&lt;BR /&gt;
       , x&lt;BR /&gt;
       , &lt;B&gt;coalesce&lt;/B&gt;((select sum(b.x) from sample_input b where a.id=b.id and b.date between a.date+1 and a.date+120),0) as y&lt;BR /&gt;
    from sample_input a&lt;BR /&gt;
;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
proc print noobs ;run;&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Fri, 08 Apr 2011 10:03:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44978#M9223</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-08T10:03:59Z</dc:date>
    </item>
    <item>
      <title>Re: Summing the Future for a Given Time Period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44979#M9224</link>
      <description>I rethink and refine my code ,maybe it will be more efficient.&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
  input ID Date X;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1 10000 0&lt;BR /&gt;
1 10000 1&lt;BR /&gt;
1 10010 0&lt;BR /&gt;
1 10119 1&lt;BR /&gt;
1 10121 1&lt;BR /&gt;
1 10125 0&lt;BR /&gt;
1 10131 1&lt;BR /&gt;
2 90000 0&lt;BR /&gt;
2 90001 1&lt;BR /&gt;
2 90150 1&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data result(drop=_: );&lt;BR /&gt;
 set temp nobs=_nobs;&lt;BR /&gt;
 y=0; &lt;BR /&gt;
 count=_n_ ;&lt;BR /&gt;
 do until(_date-date gt 120 or id ne _id or count ge _nobs);&lt;BR /&gt;
   count+1;&lt;BR /&gt;
   if count le _nobs then do;&lt;BR /&gt;
     set temp(rename=(id=_id date=_date x=_x)) point=count;&lt;BR /&gt;
     if _date-date ge 1 and _date-date le 120 and id eq _id then y+_x;&lt;BR /&gt;
   end;&lt;BR /&gt;
 end;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp

Message was edited by: Ksharp</description>
      <pubDate>Mon, 11 Apr 2011 00:59:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summing-the-Future-for-a-Given-Time-Period/m-p/44979#M9224</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-11T00:59:49Z</dc:date>
    </item>
  </channel>
</rss>

