<?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 Vertical Summation of observations between dates in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50599#M13818</link>
    <description>Hello, &lt;BR /&gt;
&lt;BR /&gt;
My data is set up like this: &lt;BR /&gt;
&lt;BR /&gt;
Date     quantity&lt;BR /&gt;
4-26          1     &lt;BR /&gt;
4-27          0&lt;BR /&gt;
4-28          1&lt;BR /&gt;
4-29          .5&lt;BR /&gt;
4-30          0&lt;BR /&gt;
5-1            1&lt;BR /&gt;
&lt;BR /&gt;
what I'd like to do is create a new variable that, for each observation, equals the sum of 'quantity' for the 20 days following that observations date. &lt;BR /&gt;
&lt;BR /&gt;
So, for example, the new variable for observation 1 (where date = 4-26) would equal sum of variable 'quantity' for observations 2 through 21. &lt;BR /&gt;
&lt;BR /&gt;
This seems simple enough but I just cant figure out a sensible way to do this. Thank you!&lt;BR /&gt;
&lt;BR /&gt;
Tim</description>
    <pubDate>Thu, 15 Jul 2010 16:55:46 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2010-07-15T16:55:46Z</dc:date>
    <item>
      <title>Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50599#M13818</link>
      <description>Hello, &lt;BR /&gt;
&lt;BR /&gt;
My data is set up like this: &lt;BR /&gt;
&lt;BR /&gt;
Date     quantity&lt;BR /&gt;
4-26          1     &lt;BR /&gt;
4-27          0&lt;BR /&gt;
4-28          1&lt;BR /&gt;
4-29          .5&lt;BR /&gt;
4-30          0&lt;BR /&gt;
5-1            1&lt;BR /&gt;
&lt;BR /&gt;
what I'd like to do is create a new variable that, for each observation, equals the sum of 'quantity' for the 20 days following that observations date. &lt;BR /&gt;
&lt;BR /&gt;
So, for example, the new variable for observation 1 (where date = 4-26) would equal sum of variable 'quantity' for observations 2 through 21. &lt;BR /&gt;
&lt;BR /&gt;
This seems simple enough but I just cant figure out a sensible way to do this. Thank you!&lt;BR /&gt;
&lt;BR /&gt;
Tim</description>
      <pubDate>Thu, 15 Jul 2010 16:55:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50599#M13818</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-15T16:55:46Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50600#M13819</link>
      <description>Sort by "date" in descending-order (presuming it's a SAS numeric DATE variable or convert your char string to a SAS DATE using INPUT). Then accumulate a new SAS numeric variable while keeping track with a count variable -- you can use the RETAIN statement to retain the accumulation variable across DATA step passes.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Thu, 15 Jul 2010 17:30:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50600#M13819</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-15T17:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50601#M13820</link>
      <description>Sorry if I seem a bit thick, but I don't quite understand what you suggest to "finish it off" &lt;BR /&gt;
&lt;BR /&gt;
I have sorted, created a variable that accumulates the quantity variable, and created a count variable, but how can i then use the count variable to create a new variable that which shows the 20 day totals for each date?  &lt;BR /&gt;
&lt;BR /&gt;
Thanks for taking the time to respond. &lt;BR /&gt;
&lt;BR /&gt;
Tim</description>
      <pubDate>Thu, 15 Jul 2010 20:40:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50601#M13820</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-15T20:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50602#M13821</link>
      <description>Sorry - forgot to mention - using the LAGnn function.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Suggested Google advanced search, this topic / post:&lt;BR /&gt;
&lt;BR /&gt;
lag function site:sas.com</description>
      <pubDate>Thu, 15 Jul 2010 21:02:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50602#M13821</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-07-15T21:02:35Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50603#M13822</link>
      <description>Of course. Why didn't I think of that!&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
&lt;BR /&gt;
Tim</description>
      <pubDate>Thu, 15 Jul 2010 22:10:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50603#M13822</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-07-15T22:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50604#M13823</link>
      <description>The LAG function will do the trick, but I have always had trouble with the logic to make it work.  An alternative is to use an array as a stack.  The following calculates the total of the current and previous 2 values (instead of 20)..  To make the array act like a stack the index is circular (MOD function).&lt;BR /&gt;
[pre]&lt;BR /&gt;
data quantity;&lt;BR /&gt;
input md $ quantity;&lt;BR /&gt;
date = mdy(scan(md,1),scan(md,2),2010);&lt;BR /&gt;
format date date9.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
4-26 1 &lt;BR /&gt;
4-27 1.1&lt;BR /&gt;
4-28 1.2&lt;BR /&gt;
4-29 1.5&lt;BR /&gt;
4-30 2.5&lt;BR /&gt;
5-1 3.5&lt;BR /&gt;
5-2 4.5&lt;BR /&gt;
5.3 5.5&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
* Three day running total;&lt;BR /&gt;
data total;*(keep=date quantity total);&lt;BR /&gt;
   set quantity nobs=nobs;&lt;BR /&gt;
   * dimension of array is number to be totaled;&lt;BR /&gt;
   retain temp0-temp2;&lt;BR /&gt;
   array stack {0:2} temp0-temp2;&lt;BR /&gt;
   index = mod(_n_,3);&lt;BR /&gt;
   stack{index} = quantity;&lt;BR /&gt;
   total = sum(of temp:);&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
proc print data=total;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Does anyone know of a true stack routine in the DATA step?  They exist otherplaces within SAS.</description>
      <pubDate>Thu, 15 Jul 2010 22:16:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50604#M13823</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-07-15T22:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50605#M13824</link>
      <description>I would be tempted to pre-process the data first converting your date to a SAS date, creating two new dates to use with the summing,  then use SQL like so:&lt;BR /&gt;
&lt;BR /&gt;
data new;&lt;BR /&gt;
  set old;&lt;BR /&gt;
  sas_date = mdy(scan(date,1,'-'), scan(date,2,'-'), 2010);&lt;BR /&gt;
  start_date = sas_date + 1;&lt;BR /&gt;
  end_date = sas_date + 20;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
  create data new2 as&lt;BR /&gt;
  select n1.sas_date&lt;BR /&gt;
           ,sum(n2.value)&lt;BR /&gt;
  from new as n1,&lt;BR /&gt;
         new as n2&lt;BR /&gt;
  where n1.start_date &amp;gt;= n2.sas_date&lt;BR /&gt;
  and    n1.end_date &amp;lt;= n2.sas_date&lt;BR /&gt;
  group by n1.sas_date&lt;BR /&gt;
  ;&lt;BR /&gt;
quit;</description>
      <pubDate>Thu, 15 Jul 2010 22:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50605#M13824</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2010-07-15T22:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50606#M13825</link>
      <description>Oops, forgot the GROUP BY&lt;BR /&gt;
&lt;BR /&gt;
I would be tempted to pre-process the data first converting your date to a SAS date, creating two new dates to use with the summing, then use SQL like so:&lt;BR /&gt;
&lt;BR /&gt;
data new;&lt;BR /&gt;
set old;&lt;BR /&gt;
sas_date = mdy(scan(date,1,'-'), scan(date,2,'-'), 2010);&lt;BR /&gt;
start_date = sas_date + 1;&lt;BR /&gt;
end_date = sas_date + 20;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create data new2 as&lt;BR /&gt;
select n1.sas_date&lt;BR /&gt;
,sum(n2.value)&lt;BR /&gt;
from new as n1,&lt;BR /&gt;
new as n2&lt;BR /&gt;
where n1.start_date &amp;gt;= n2.sas_date&lt;BR /&gt;
and n1.end_date &lt;BR /&gt;
group by n1.sas_date</description>
      <pubDate>Thu, 15 Jul 2010 22:49:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50606#M13825</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2010-07-15T22:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation of observations between dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50607#M13826</link>
      <description>Oops 2, my SQL got chopped.&lt;BR /&gt;
&lt;BR /&gt;
Oops, forgot the GROUP BY&lt;BR /&gt;
&lt;BR /&gt;
I would be tempted to pre-process the data first converting your date to a SAS date, creating two new dates to use with the summing, then use SQL like so:&lt;BR /&gt;
&lt;BR /&gt;
data new;&lt;BR /&gt;
set old;&lt;BR /&gt;
sas_date = mdy(scan(date,1,'-'), scan(date,2,'-'), 2010);&lt;BR /&gt;
start_date = sas_date + 1;&lt;BR /&gt;
end_date = sas_date + 20;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create data new2 as&lt;BR /&gt;
select n1.sas_date&lt;BR /&gt;
,sum(n2.value)&lt;BR /&gt;
from new as n1,&lt;BR /&gt;
new as n2&lt;BR /&gt;
where n1.start_date ge n2.sas_date&lt;BR /&gt;
and n1.end_date le n2.sas_date&lt;BR /&gt;
group by n1.sas_date&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: SASKiwi

Message was edited by: SASKiwi</description>
      <pubDate>Fri, 16 Jul 2010 02:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Vertical-Summation-of-observations-between-dates/m-p/50607#M13826</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2010-07-16T02:16:56Z</dc:date>
    </item>
  </channel>
</rss>

