<?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: Vertical Summation with a Condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481753#M124683</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;Sir, I tried a slight tweak to your code taking OP's dataset as is with date30 var from source&amp;nbsp; and tweak in between and adding date30 to group by . I don't know why I didn't think of that earlier&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	Date :date9.	SIN	Date30 :date9. ;
format date: date9.;
cards;
1	12Jul2017	318	11Aug2017
1	11Aug2017	197	10Sep2017
1	10Sep2017	700	10Oct2017
1	10Oct2017	524	09Nov2017
1	09Nov2017	702	09Dec2017
2	29Sep2017	642	29Oct2017
2	09Dec2017	446	08Jan2018
3	04Dec2010	182	03Jan2011
3	02Feb2011	0	04Mar2011
3	03Apr2011	0	03May2011
4	05Dec2010	317	04Jan2011
4	06Dec2010	272	05Jan2011
4	07Dec2010	648	06Jan2011
4	08Dec2010	778	07Jan2011
4	03Mar2011	839	02Apr2011
4	27May2011	784	26Jun2011
;

proc sql;
create table want as
select 
    a.ID, a.date, a.SIN, 
  a.date30 ,sum(b.SIN) as SIN30
from
    have as a inner join
    have as b 
    on  a.ID=b.ID  and b.date between a.date and a.date30
group by a.ID, a.date, a.SIN,a.date30;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 27 Jul 2018 03:59:43 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-07-27T03:59:43Z</dc:date>
    <item>
      <title>Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481646#M124623</link>
      <description>&lt;P&gt;I have&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input ID Date :date9. SIn Date30 :date9. ;&lt;BR /&gt;format date: date9.;&lt;BR /&gt;cards;&lt;BR /&gt;1 12Jul2017 318 11Aug2017&lt;BR /&gt;1 11Aug2017 197 10Sep2017&lt;BR /&gt;1 10Sep2017 700 10Oct2017&lt;BR /&gt;1 10Oct2017 524 09Nov2017&lt;BR /&gt;1 09Nov2017 702 09Dec2017&lt;BR /&gt;2 29Sep2017 642 29Oct2017&lt;BR /&gt;2 09Dec2017 446 08Jan2018&lt;BR /&gt;3 04Dec2010 182 03Jan2011&lt;BR /&gt;3 02Feb2011 0 04Mar2011&lt;BR /&gt;3 03Apr2011 10 20May2011&lt;BR /&gt;4 05Dec2010 317 04Jan2011&lt;BR /&gt;4 06Dec2010 272 05Jan2011&lt;BR /&gt;4 07Dec2010 648 06Jan2011&lt;BR /&gt;4 08Dec2010 778 07Jan2011&lt;BR /&gt;4 03Mar2011 839 02Apr2011&lt;BR /&gt;4 27May2011 784 26Jun2011&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Date30 is just the variable date + 30 days&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to vertically sum the SIN variable By ID where the new variable SIN30 is the cumulative sum&amp;nbsp;of all SIN values for the 30 days after the current date. the output should look like this&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;SIn&lt;/TD&gt;&lt;TD&gt;Date30&lt;/TD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;&lt;TD&gt;03Mar2011&lt;/TD&gt;&lt;TD&gt;839&lt;/TD&gt;&lt;TD&gt;02Apr2011&lt;/TD&gt;&lt;TD&gt;839&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;27May2011&lt;/TD&gt;&lt;TD&gt;784&lt;/TD&gt;&lt;TD&gt;26Jun2011&lt;/TD&gt;&lt;TD&gt;784&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In earlier post &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Conditional-Vertical-Summation/m-p/478242#M123312" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Conditional-Vertical-Summation/m-p/478242#M123312&lt;/A&gt; , I was able to find a solution to this issue using the following code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; _N_ &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; do&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;rename&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;sin&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;_sin&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;declare&lt;/SPAN&gt; hash h&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;dataset:&lt;SPAN class="token string"&gt;'have(rename=(sin=_sin)'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;defineKey&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'id'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'date'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;defineData&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;'_sin'&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;defineDone&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
sin30&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
do &lt;SPAN class="token function"&gt;n&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;date&lt;/SPAN&gt; to date30&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; h&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;find&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;key:&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;key:&lt;SPAN class="token function"&gt;n&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; sin30&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;_sin&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
end&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;drop&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;n&lt;/SPAN&gt; _sin&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;While the code works perfectly, I can not use it because my computer memory is insufficient to execute the hash based code (i'm also creating 36 variables similar to SIN30). I was wondering if there is any other way to solve this problem without using the hash function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for all your help in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 18:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481646#M124623</guid>
      <dc:creator>melsaid04</dc:creator>
      <dc:date>2018-07-26T18:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481674#M124634</link>
      <description>&lt;P&gt;In most cases, splitting your data set up into smaller data sets is not recommended, but in this case, where you are running out of memory on the whole data set, I believe this is a good thing to do.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Split your data into smaller data sets by ID, or perhaps several IDs into one data set.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 20:14:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481674#M124634</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-26T20:14:00Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481676#M124636</link>
      <description>&lt;P&gt;Thank you for your response. I tried doing that, however, it is not practical, as I have to split the data into 800 smaller datasets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 20:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481676#M124636</guid>
      <dc:creator>melsaid04</dc:creator>
      <dc:date>2018-07-26T20:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481682#M124638</link>
      <description>&lt;P&gt;If the dates are a sparsely spaced as in your example, then for each record you're looking for 30 dates just to harvest 2 or 3 for SIN30.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it weren't for memory problems, you could just make the hash an ordered hash (by id date), and associate a hash iterator with it.&amp;nbsp; Then you could use the SETCUR method to get the current date and SIN. Then a sequence of NEXT methods to get successive observations until you bypass DATE30.&amp;nbsp; This way there is no excess of unsatisfied FIND methods.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to memory issues, you probably could populate the hash one ID at a time, process all the records for that id, and then clear the hash object in preparation for the next id.&amp;nbsp; In that case you could just use DATE as the hash key.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, the program below is almost certainly faster, as it uses an array (less memory overhead than hash), and since the array is accessed for N=1, 2, ..., it is a key-indexed array providing faster lookup than hash:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;

  array _dat{100} ;
  array _sin{100} ;

  do _n=1 by 1 until (last.id);
    set have;
    by id;
    _dat{_n}=date;
    _sin{_n}=sin;
  end;

  do _n2=1 to _n;
    set have;
    sin30=0;
    do _n3=_n2 to _n while (_dat{_n3}&amp;lt;=date30);
      if _dat{_n3}&amp;gt;date30 then leave;
      else sin30=sin30 + _sin{_n3};
    end;
    output;
  end;

  drop _: ;
run;
 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program assumes no ID has more than 100 dates.&amp;nbsp; If that's not true, just set the dimension of arrays _dat and _sin to exceed the maximum expected date count.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program reads a single ID once to populate the arrays.&amp;nbsp; Then it re-reads the same ID, and retrieves data from the array as needed.&amp;nbsp; This type of re-read is probably relatively efficient, since the re-reading probably comes from memory buffers rather than from the disk.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Like your program, this reads the dataset twice. but unlike your program it doesn't read the entire dataset once, and then restarts at the beginning to re-read the entire dataset.&amp;nbsp;&amp;nbsp; Much less efficient use of disk channel resources.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited improvement.&amp;nbsp; You can replace&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    do _n3=_n2 to _n while (_dat{_n3}&amp;lt;=date30);
      if _dat{_n3}&amp;gt;date30 then leave;
      else sin30=sin30 + _sin{_n3};
    end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    do _n3=_n2 to _n while (_dat{_n3}&amp;lt;=date30);
      sin30=sin30 + _sin{_n3};
    end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "if test" was needed in an earlier version, but no longer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 20:46:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481682#M124638</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-07-26T20:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481687#M124641</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	Date :date9.	SIN	Date30 :date9. ;
format date: date9.;
cards;
1	12Jul2017	318	11Aug2017
1	11Aug2017	197	10Sep2017
1	10Sep2017	700	10Oct2017
1	10Oct2017	524	09Nov2017
1	09Nov2017	702	09Dec2017
2	29Sep2017	642	29Oct2017
2	09Dec2017	446	08Jan2018
3	04Dec2010	182	03Jan2011
3	02Feb2011	0	04Mar2011
3	03Apr2011	0	03May2011
4	05Dec2010	317	04Jan2011
4	06Dec2010	272	05Jan2011
4	07Dec2010	648	06Jan2011
4	08Dec2010	778	07Jan2011
4	03Mar2011	839	02Apr2011
4	27May2011	784	26Jun2011
;

data w;
set have;
by id;
sin30=0;
do n=date to date30;
output;
end;
format n date9.;
run;

proc sql;
create table want as
select a.*,b.sin30
from have a left join 
(select a.id,a.date as date,a.sin, sum(b.sin) as sin30
from w a left join have b
on a.id=b.id 
where a.n=b.date
group by a.id, a.date,a.sin) b
on a.id=b.id and a.date=b.date
order by a.id,a.date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Jul 2018 21:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481687#M124641</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-26T21:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481689#M124643</link>
      <description>&lt;P&gt;Thank you so much the code you provided worked perfectly, I just have to replace it to create 30 some similar variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your response you mentioned:&lt;/P&gt;&lt;P&gt;"&lt;SPAN&gt;As to memory issues, you probably could populate the hash one ID at a time, process all the records for that id, and then clear the hash object in preparation for the next id.&amp;nbsp; In that case you could just use DATE as the hash key."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;for my learning purposes, how would the hash code provided above change to reflect&amp;nbsp;on the approach you suggested.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Best Regards,&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Melsaid&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 21:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481689#M124643</guid>
      <dc:creator>melsaid04</dc:creator>
      <dc:date>2018-07-26T21:15:13Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481691#M124644</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/221159"&gt;@melsaid04&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for your response. I tried doing that, however, it is not practical, as I have to split the data into 800 smaller datasets.&lt;/P&gt;
&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;A macro would get the job done, and append the results back together as well.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jul 2018 21:28:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481691#M124644</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-07-26T21:28:23Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481748#M124679</link>
      <description>&lt;P&gt;Here's how you would use your original logic and hash programming one ID at a time.&amp;nbsp; Of course this assumes that the data are sorted by ID (although they don't have to be sorted by date within id):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
  if _N_ = 1 then do;
    if 0 then set have(rename=(sin=_sin));
    declare hash h(dataset:'have(obs=0 rename=(sin=_sin)');
      h.defineKey('date');
      h.defineData('_sin');
      h.defineDone();
  end;
  do until (last.id);
    set have;
    by id;
    h.add(key:date,data:sin);
  end;

  do until (last.id);
    set have;
    by id;
    sin30=0;
    do n=date to date30;
      if h.find(key:n)=0 then sin30+_sin;
    end;
    output;
  end;
  h.clear();
  drop n _sin;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, this program does 30 FIND methods even if only 2 or 3 actually get data.&amp;nbsp; As I mentioned earlier, you could eliminate that wastage by introducing a hash iterator, and use of the SETCUR and NEXT methods instead of FIND over 30 dates:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want3;
  if _N_ = 1 then do;
    if 0 then set have(rename=(sin=_sin date=_date));
    declare hash h(dataset:'have(obs=0 rename=(sin=_sin date=_date)',ordered:'a');
      h.defineKey('_date');
      h.defineData('_date','_sin');
      h.defineDone();
    declare hiter hi ('h');
  end;
  do until (last.id);
    set have (rename=(sin=_sin date=_date));
    by id;
    h.add();
  end;

  do until (last.id);
    set have;
    by id;
    sin30=0;
    do rc=hi.setcur(key:date) by 0 while(_date&amp;lt;=date30);
      sin30=sin30+_sin;
      if hi.next()^=0 then leave;
    end;
    output;
  end;
  h.clear();
  drop rc _: ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Jul 2018 02:38:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481748#M124679</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-07-27T02:38:04Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481749#M124680</link>
      <description>&lt;P&gt;Depending on how your data is indexed, this simple approach can be quite efficient:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	Date :date9. SIN;
format date: date9.;
cards;
1	12Jul2017	318
1	11Aug2017	197
1	10Sep2017	700
1	10Oct2017	524
1	09Nov2017	702
2	29Sep2017	642
2	09Dec2017	446
3	04Dec2010	182
3	02Feb2011	0
3	03Apr2011	0
4	05Dec2010	317
4	06Dec2010	272
4	07Dec2010	648
4	08Dec2010	778
4	03Mar2011	839
4	27May2011	784
;

proc sql;
create table want as
select 
    a.ID, a.date, a.SIN, 
    sum(b.SIN) as SIN30
from
    have as a inner join
    have as b 
    on  a.ID=b.ID and 
        b.date between a.date and a.date+30
group by a.ID, a.date, a.SIN;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Jul 2018 02:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481749#M124680</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-07-27T02:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481753#M124683</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;Sir, I tried a slight tweak to your code taking OP's dataset as is with date30 var from source&amp;nbsp; and tweak in between and adding date30 to group by . I don't know why I didn't think of that earlier&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID	Date :date9.	SIN	Date30 :date9. ;
format date: date9.;
cards;
1	12Jul2017	318	11Aug2017
1	11Aug2017	197	10Sep2017
1	10Sep2017	700	10Oct2017
1	10Oct2017	524	09Nov2017
1	09Nov2017	702	09Dec2017
2	29Sep2017	642	29Oct2017
2	09Dec2017	446	08Jan2018
3	04Dec2010	182	03Jan2011
3	02Feb2011	0	04Mar2011
3	03Apr2011	0	03May2011
4	05Dec2010	317	04Jan2011
4	06Dec2010	272	05Jan2011
4	07Dec2010	648	06Jan2011
4	08Dec2010	778	07Jan2011
4	03Mar2011	839	02Apr2011
4	27May2011	784	26Jun2011
;

proc sql;
create table want as
select 
    a.ID, a.date, a.SIN, 
  a.date30 ,sum(b.SIN) as SIN30
from
    have as a inner join
    have as b 
    on  a.ID=b.ID  and b.date between a.date and a.date30
group by a.ID, a.date, a.SIN,a.date30;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Jul 2018 03:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/481753#M124683</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-27T03:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/482131#M124823</link>
      <description>&lt;P&gt;I think this will work too (not tested), but honestly, if memory is not an issue, the SQL solution above is simplest and I've used that technique for very large data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by id date; run;

proc sql noprint; 
select max(n) into :maxn from 
	(select id, count(*) as n from have 
	group by id);
quit;

data want;
set have;
by id;
array stack {&amp;amp;maxn,2} _temporary_;
length n 3 sln30 d d30 5;
format d d30 date9.;
if first.id then do;
	do i=1 to dim(stack,1);
		stack[i,1]=.;
		stack[i,2]=.;
	end;
	n=0;
end;
n+1;
stack[n,1]=date;
stack[n,2]=sln;
if last.id then do;
	do i=1 to n;
		sln30=0;
		d=stack[i,1];
		d30=d+30;
		do j=i to n;
			if stack[j,1]&amp;gt;d30 then do;
				output;
				leave;
			end;
			sln30+stack[j,2];
			if j=n then output;
		end;
	end;
end;
keep d d30 sln30;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm a little confused by the purpose of this, though, as it seems like it double counts stuff, e.g. the '197' in line 2 is counted both in the 12Jul - 11Aug range AND in the 11Aug - 10Sep range.&amp;nbsp; But I'm probably just not following.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jul 2018 02:34:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/482131#M124823</guid>
      <dc:creator>quickbluefish</dc:creator>
      <dc:date>2018-07-28T02:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/482288#M124890</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;thank you so much for taking the time to draft the code for the edited hash function. I studied the code and it makes so much sense.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I really appreciate all your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Melsaid&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jul 2018 15:03:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/482288#M124890</guid>
      <dc:creator>melsaid04</dc:creator>
      <dc:date>2018-07-29T15:03:16Z</dc:date>
    </item>
    <item>
      <title>Re: Vertical Summation with a Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/482289#M124891</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;the code you provided worked like a charm.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for taking the time to help out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Melsaid&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jul 2018 15:05:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Vertical-Summation-with-a-Condition/m-p/482289#M124891</guid>
      <dc:creator>melsaid04</dc:creator>
      <dc:date>2018-07-29T15:05:14Z</dc:date>
    </item>
  </channel>
</rss>

