<?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: How to sum lagged values if a condition is met? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-lagged-values-if-a-condition-is-met/m-p/569655#M160562</link>
    <description>&lt;P&gt;Thank you so much! It works like a champ! Really appreciate it!&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jun 2019 22:12:38 GMT</pubDate>
    <dc:creator>mamun85</dc:creator>
    <dc:date>2019-06-27T22:12:38Z</dc:date>
    <item>
      <title>How to sum lagged values if a condition is met?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-lagged-values-if-a-condition-is-met/m-p/569652#M160559</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I want to create a new dataset (or a column in the existing dataset) that adds the lagged values if a condition is met. Say, the condition is when:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;lagDays&amp;lt;7&lt;/PRE&gt;&lt;P&gt;then add the corresponding value for the variable "chrg" with its preceding value. &amp;nbsp;Here is an example for your consideration: &amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have; 
input id $3. chrg lagDays;
datalines;
001 19 .
001 30 23
001 23 45
001 35 1
001 29 2
002 66 .
002 59 34
002 70 19
003 55 .
003 90 10
003 91 4
003 19 6
003 33 1
003 45 22
003 87 1
003 90 1
003 43 45
;
run;

data want; 
input id $3. chrg lagDays adjchrg $20.;
datalines;
001 19 .	19
001 30 23 	30
001 23 45 	23
001 35 1 	23+35
001 29 2 	23+35+29
002 66 . 	66
002 59 34	59
002 70 19	19
003 55 .	55
003 90 10	90
003 91 4	90+91
003 19 6	90+91+19
003 33 1	90+91+19+33
003 45 22	45
003 87 1	45+87
003 90 1	45+87+90
003 43 45	45
;
run;

proc print data=have; run;
proc print data=want; run;&lt;/PRE&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2019 21:58:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-lagged-values-if-a-condition-is-met/m-p/569652#M160559</guid>
      <dc:creator>mamun85</dc:creator>
      <dc:date>2019-06-27T21:58:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum lagged values if a condition is met?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-lagged-values-if-a-condition-is-met/m-p/569654#M160561</link>
      <description>&lt;P&gt;I'll take a crack, but I'm at home, I have no access to SAS to test this code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want; 
set have;
by id;
if first.id or lagdays&amp;gt;=7 then sum=chrg;
if lagdays&amp;lt;7 then sum+chrg;

datalines;
001 19 .
001 30 23
001 23 45
001 35 1
001 29 2
002 66 .
002 59 34
002 70 19
003 55 .
003 90 10
003 91 4
003 19 6
003 33 1
003 45 22
003 87 1
003 90 1
003 43 45
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Jun 2019 22:07:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-lagged-values-if-a-condition-is-met/m-p/569654#M160561</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-27T22:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to sum lagged values if a condition is met?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-sum-lagged-values-if-a-condition-is-met/m-p/569655#M160562</link>
      <description>&lt;P&gt;Thank you so much! It works like a champ! Really appreciate it!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2019 22:12:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-sum-lagged-values-if-a-condition-is-met/m-p/569655#M160562</guid>
      <dc:creator>mamun85</dc:creator>
      <dc:date>2019-06-27T22:12:38Z</dc:date>
    </item>
  </channel>
</rss>

