<?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: Data Step Consecutive Sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Consecutive-Sum/m-p/811650#M320156</link>
    <description>&lt;P&gt;Try this&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
	infile cards ;
	input ob $ target ;
cards ;
a 1
a 0
a 1
a 1
a 1
a 1
b 0
b 1
b 1
b 1
b 1
;
run ;

proc sort in=have out=srtd ;
	by ob ;
run ;

data want ;
	retain target_1 target_2 ;
	set have ;
	by ob ;
	if first.ob then do ;
		target_1=0 ;
		target_2=0 ;
	end ;
	sumlast3=sum(target,target_1,target_2) ;
	if sumlast3=3 then hit=1 else hit=0 ;
	target_2=target_1 ;
	target_1=target ;
	output ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 05 May 2022 12:33:58 GMT</pubDate>
    <dc:creator>AMSAS</dc:creator>
    <dc:date>2022-05-05T12:33:58Z</dc:date>
    <item>
      <title>Data Step Consecutive Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Consecutive-Sum/m-p/811492#M320086</link>
      <description>Hi: In the data set below, I want to sum the current record plus the last 2 records to create the var (sumlast3). The var hit is equal to 1 if sumlast3 equals 3. I used the lag in the following way but it is not working:&lt;BR /&gt;Sumlast3=lag(target)+lag2(target)+lag3(target);&lt;BR /&gt;Data Have&lt;BR /&gt;&lt;BR /&gt;obs target&lt;BR /&gt;a 1&lt;BR /&gt;a 0&lt;BR /&gt;a 1&lt;BR /&gt;a 1&lt;BR /&gt;a 1&lt;BR /&gt;a 1&lt;BR /&gt;b 0&lt;BR /&gt;b 1&lt;BR /&gt;b 1&lt;BR /&gt;b 1&lt;BR /&gt;b 1&lt;BR /&gt;&lt;BR /&gt;Data Want:&lt;BR /&gt;&lt;BR /&gt;obs target sumlast3 hit&lt;BR /&gt;a 1&lt;BR /&gt;a 0&lt;BR /&gt;a 1 2 0&lt;BR /&gt;a 1 2 0&lt;BR /&gt;a 1 3 1&lt;BR /&gt;a 1 3 1&lt;BR /&gt;b 0&lt;BR /&gt;b 1&lt;BR /&gt;b 1 2 0&lt;BR /&gt;b 1 3 1&lt;BR /&gt;b 1 3 1&lt;BR /&gt;&lt;BR /&gt;Thanks.</description>
      <pubDate>Wed, 04 May 2022 16:13:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Consecutive-Sum/m-p/811492#M320086</guid>
      <dc:creator>atnk</dc:creator>
      <dc:date>2022-05-04T16:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Data Step Consecutive Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-Step-Consecutive-Sum/m-p/811650#M320156</link>
      <description>&lt;P&gt;Try this&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
	infile cards ;
	input ob $ target ;
cards ;
a 1
a 0
a 1
a 1
a 1
a 1
b 0
b 1
b 1
b 1
b 1
;
run ;

proc sort in=have out=srtd ;
	by ob ;
run ;

data want ;
	retain target_1 target_2 ;
	set have ;
	by ob ;
	if first.ob then do ;
		target_1=0 ;
		target_2=0 ;
	end ;
	sumlast3=sum(target,target_1,target_2) ;
	if sumlast3=3 then hit=1 else hit=0 ;
	target_2=target_1 ;
	target_1=target ;
	output ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 May 2022 12:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-Step-Consecutive-Sum/m-p/811650#M320156</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2022-05-05T12:33:58Z</dc:date>
    </item>
  </channel>
</rss>

