<?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: cumulative sum by group on different conditions in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495913#M15453</link>
    <description>&lt;P&gt;Disregard...I see what you're looking for is a little different. Try this.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=had_surgery);
set have;
retain had_surgery;
by id;
count+1;
if surgery = 1 then do;
 count=0;
 had_surgery=1;
 end;
if had_surgery=1 then count=0;
if last.id then had_surgery=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 15 Sep 2018 13:47:54 GMT</pubDate>
    <dc:creator>bobpep212</dc:creator>
    <dc:date>2018-09-15T13:47:54Z</dc:date>
    <item>
      <title>cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495907#M15451</link>
      <description>&lt;P&gt;hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been searching the solution a while, but I couldn't find solutions in SAS in communities.&lt;/P&gt;&lt;P&gt;i want to make cumulative sum by group on different conditions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the following data is raw data. it's about patient's medical utilization.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id date surgery_YN&lt;/P&gt;&lt;P&gt;1 2002/01/28 0&lt;BR /&gt;1 2002/02/15 0&lt;BR /&gt;1 2002/04/06 0&lt;BR /&gt;1 2002/06/07 1&lt;BR /&gt;1 2002/08/10 0&lt;BR /&gt;1 2003/01/12 0&lt;BR /&gt;2 2003/01/15 0&lt;BR /&gt;2 2003/02/10 1&lt;BR /&gt;2 2003/03/10 0&lt;BR /&gt;2 2003/04/08 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i want calculate how many times patient visited clinics before he had surgery. so i want make data like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id date surgery_YN count&lt;/P&gt;&lt;P&gt;1 2002/01/28 0 &lt;STRONG&gt;1&lt;/STRONG&gt;&lt;BR /&gt;1 2002/02/15 0 &lt;STRONG&gt;2&lt;/STRONG&gt;&lt;BR /&gt;1 2002/04/06 0 &lt;STRONG&gt;3&lt;/STRONG&gt;&lt;BR /&gt;1 2002/06/07 &lt;STRONG&gt;1&lt;/STRONG&gt; 0&lt;BR /&gt;1 2002/08/10 0 0&lt;BR /&gt;1 2003/01/12 0 0&lt;BR /&gt;2 2003/01/15 0 &lt;STRONG&gt;1&lt;/STRONG&gt;&lt;BR /&gt;2 2003/02/10&amp;nbsp;0&amp;nbsp;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;BR /&gt;2 2003/03/10 1 0&lt;BR /&gt;2 2003/04/08 0 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i used 'retain' code and i can make count '0' after surgery. but i don't know how to make cumulative sum before surgery. I wonder if anyone can help me a little bit. Thank you very much for your help!&lt;/P&gt;</description>
      <pubDate>Sat, 15 Sep 2018 13:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495907#M15451</guid>
      <dc:creator>km0927</dc:creator>
      <dc:date>2018-09-15T13:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495910#M15452</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
count+1;
if surgery = 1 then count=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Try something like this.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Sep 2018 13:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495910#M15452</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2018-09-15T13:38:44Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495913#M15453</link>
      <description>&lt;P&gt;Disregard...I see what you're looking for is a little different. Try this.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=had_surgery);
set have;
retain had_surgery;
by id;
count+1;
if surgery = 1 then do;
 count=0;
 had_surgery=1;
 end;
if had_surgery=1 then count=0;
if last.id then had_surgery=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Sep 2018 13:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495913#M15453</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2018-09-15T13:47:54Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495914#M15454</link>
      <description>Also, if you post in the future, make sure to put the post in the correct community group. You will get a faster response. Something like this would be better in the SAS Programming community.</description>
      <pubDate>Sat, 15 Sep 2018 13:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495914#M15454</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2018-09-15T13:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495920#M15455</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date $10. surgery_YN;
cards;
1 2002/01/28 0
1 2002/02/15 0
1 2002/04/06 0
1 2002/06/07 1
1 2002/08/10 0
1 2003/01/12 0
2 2003/01/15 0
2 2003/02/10 1
2 2003/03/10 0
2 2003/04/08 0
;
proc sort data=have;
	by id date;
run;
data want;
	retain had_surgery;
	set have;
	by Id date;
	if first.id then
		do;
			had_surgery = 0;
			count = 0;
		end;
	if surgery_yn = 0 and had_surgery = 0 then count+1;
	else if surgery_yn = 1 then
		do;
			had_surgery = 1;
			count=0 ;
		end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Sep 2018 14:05:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495920#M15455</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-09-15T14:05:02Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495926#M15456</link>
      <description>ID 2 record 2 in the want data is incorrect because the incoming data doesn't support the result to add +1 to the count.</description>
      <pubDate>Sat, 15 Sep 2018 14:25:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495926#M15456</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-09-15T14:25:24Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495928#M15457</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date $10. surgery_YN;
cards;
1 2002/01/28 0
1 2002/02/15 0
1 2002/04/06 0
1 2002/06/07 1
1 2002/08/10 0
1 2003/01/12 0
2 2003/01/15 0
2 2003/02/10 1
2 2003/03/10 0
2 2003/04/08 0
;
data temp;
 set have;
 by id surgery_YN notsorted;
 if first.id then n=0;
 n+first.surgery_YN;
run;
data want;
 set temp;
 by n notsorted;
 if first.n then count=0;
 count+1;
 if n ne 1 then count=0;
 drop n;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just for fun, second way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 by id;
 retain found;
 if first.id then do;found=0;count=0;end;
 count+1;
 if  surgery_YN=1 then found=1;
 if found then count=0;
 drop found;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Sep 2018 14:53:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495928#M15457</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-09-15T14:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495935#M15458</link>
      <description>&lt;P&gt;thank you for all advice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to bobpep212, it worked out cumulative before surgery, but it didn't seperated by group. thank you for advice and correcting community group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to VDD, I&amp;nbsp;&lt;SPAN&gt;Intentionally make 'visiting data(billing data)' brief, and i think i made some error. but it worked out right.&amp;nbsp;i really appreciate that.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;for Ksharp, I tried VDD's code first. and he or she posted before your code. sorry for not trying your code.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Sep 2018 14:44:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495935#M15458</guid>
      <dc:creator>km0927</dc:creator>
      <dc:date>2018-09-15T14:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495937#M15459</link>
      <description>&lt;P&gt;Sorry too late ,but my contrib&amp;nbsp;&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 : yymmdd10. surgery_YN;
format date yymmdd10.;
cards;
1 2002/01/28 0
1 2002/02/15 0
1 2002/04/06 0
1 2002/06/07 1
1 2002/08/10 0
1 2003/01/12 0
2 2003/01/15 0
2 2003/02/10 0
2 2003/03/10 1
2 2003/04/08 0
;

data want;
set have;
by id date;
if first.id then do;f=0;count=0;end;
f+surgery_YN;
if f&amp;lt;1 then count+1;else count=0;
drop f;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Sep 2018 15:00:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495937#M15459</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-15T15:00:04Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495953#M15460</link>
      <description>if the data supports that an ID could have 2 or more surgeries that had no records between the surgeries where surgery_YN = 0, you could change the had_surgery =1 to had_surgery +1.&lt;BR /&gt;this would give you a count of sequential surgeries the patient had on the final surgery record for the span of the sequenced surgery records.</description>
      <pubDate>Sat, 15 Sep 2018 15:52:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495953#M15460</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2018-09-15T15:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum by group on different conditions</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495958#M15461</link>
      <description>You're welcome. Glad you got it to work. Yeah, I realized my first post wasn't what you were looking for so I posted a second version that matched what you had in your want output. But you have lots of options that work here!</description>
      <pubDate>Sat, 15 Sep 2018 16:11:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/cumulative-sum-by-group-on-different-conditions/m-p/495958#M15461</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2018-09-15T16:11:37Z</dc:date>
    </item>
  </channel>
</rss>

