<?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: Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Targ in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560646#M156831</link>
    <description>&lt;P&gt;I can't understand what the don't want part is in your question. Are you saying you tried that and it didn't get the answer you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From the problem description it sounds like you want to number the quarters within the groups defined by CID and OPENDATE.&lt;/P&gt;
&lt;P&gt;So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by cid opendate qtr;
  if first.opendate then count=0;
  count + first.qtr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is it possible you have gaps in your data?&amp;nbsp; So that you have one record from 2018Q1 and the next from 2018Q3 but none from 2018Q2?&amp;nbsp; If so do you want the count to go 1,3 instead of 1,2?&amp;nbsp; If so you might want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by cid opendate qtr;
  count + intck('qtr',qtr,lag(qtr));
  if first.opendate then count=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 May 2019 20:19:30 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-05-21T20:19:30Z</dc:date>
    <item>
      <title>Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Target</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560631#M156823</link>
      <description>&lt;P&gt;I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cid $ acctno $ opendate mmddyy10. hicredit memberid $ qtr mmddyy10.;
datalines;
123	x	1/1/2018	2000	ABC	3/1/2018
123	x	1/1/2018	2000	ABC	6/1/2018
123	x	1/1/2018	2000	ABC	9/1/2018
123	x	1/1/2018	2000	ABC	12/1/2018
123	y	1/1/2018	2300	XYZ	3/1/2019
123	y	1/1/2018	2300	XYZ	6/1/2019
123	y	1/1/2018	2300	XYZ	9/1/2019
123	y	1/1/2018	2300	XYZ	12/1/2019
123	z	1/1/2017	5000	XYZ	3/1/2018
123	z	1/1/2017	5000	XYZ	6/1/2018
123	z	1/1/2017	5000	XYZ	9/1/2018
123	z	1/1/2017	5000	XYZ	12/1/2018
123	z	1/1/2017	5000	XYZ	3/1/2019
123	z	1/1/2017	5000	XYZ	6/1/2019
123	z	1/1/2017	5000	XYZ	9/1/2019
123	z	1/1/2017	5000	XYZ	12/1/2019
123	w	1/1/2017	10000	XYZ	3/1/2018
123	w	1/1/2017	10000	XYZ	6/1/2018
123	w	1/1/2017	10000	XYZ	9/1/2018
123	w	1/1/2017	10000	XYZ	12/1/2018
123	w	1/1/2017	10000	XYZ	3/1/2019
123	w	1/1/2017	10000	XYZ	6/1/2019
123	w	1/1/2017	10000	XYZ	9/1/2019
123	w	1/1/2017	10000	XYZ	12/1/2019
;
run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=have;&lt;BR /&gt;by cid opendate qtr;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want a Sequence Variable, COUNT, &lt;STRONG&gt;BY CID OPENDATE&lt;/STRONG&gt; based on increasing QTR values. This will create groups by QTR within CID OPENDATE groups.&lt;/P&gt;&lt;P&gt;For example, in CID OPENDATE group (123&amp;nbsp; 01/01/2017),&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; QTR == 03/01/2018&amp;nbsp; =&amp;gt;&amp;nbsp; COUNT == 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; QTR == 06/01/2018&amp;nbsp; =&amp;gt;&amp;nbsp; COUNT == 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=want;
by cid opendate qtr;
run;

data want2;
format opendate mmddyy10. qtr mmddyy10.;
set want;
count + 1;
by cid opendate qtr;
if first.cid or first.opendate or first.qtr then count = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If this can be accomplished by using a RETAIN statement, could you also explain how the RETAIN statement functions to allow this to work?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT: Fixed missing info in code block&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 19:52:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560631#M156823</guid>
      <dc:creator>publicSynechism</dc:creator>
      <dc:date>2019-05-21T19:52:33Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Targ</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560637#M156826</link>
      <description>&lt;P&gt;This adds +1 for each new quarter BY cid and opendate. Seem right?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The retain statement holds the specified value between processing different records in the dataset. Order of operation is important here: compare values first, THEN assign the current value to the retain variable to check against it in the next pass.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cid $ acctno $ opendate mmddyy10. hicredit memberid $ qtr mmddyy10.;
format opendate qtr mmddyy10.;
datalines;
123 x 1/1/2018 2000 ABC 3/1/2018
123 x 1/1/2018 2000 ABC 6/1/2018
123 x 1/1/2018 2000 ABC 9/1/2018
123 x 1/1/2018 2000 ABC 12/1/2018
123 y 1/1/2018 2300 XYZ 3/1/2019
123 y 1/1/2018 2300 XYZ 6/1/2019
123 y 1/1/2018 2300 XYZ 9/1/2019
123 y 1/1/2018 2300 XYZ 12/1/2019
123 z 1/1/2017 5000 XYZ 3/1/2018
123 z 1/1/2017 5000 XYZ 6/1/2018
123 z 1/1/2017 5000 XYZ 9/1/2018
123 z 1/1/2017 5000 XYZ 12/1/2018
123 z 1/1/2017 5000 XYZ 3/1/2019
123 z 1/1/2017 5000 XYZ 6/1/2019
123 z 1/1/2017 5000 XYZ 9/1/2019
123 z 1/1/2017 5000 XYZ 12/1/2019
123 w 1/1/2017 10000 XYZ 3/1/2018
123 w 1/1/2017 10000 XYZ 6/1/2018
123 w 1/1/2017 10000 XYZ 9/1/2018
123 w 1/1/2017 10000 XYZ 12/1/2018
123 w 1/1/2017 10000 XYZ 3/1/2019
123 w 1/1/2017 10000 XYZ 6/1/2019
123 w 1/1/2017 10000 XYZ 9/1/2019
123 w 1/1/2017 10000 XYZ 12/1/2019
;
run;proc sort data=have;by cid opendate qtr;run;

data want;
	set have;
	by cid opendate;
	retain prev_qtr;
	if first.opendate then do;
		count=0;
		call missing(prev_qtr);
	end;
	if qtr ne prev_qtr then count+1;

	*hold value for next record;
	prev_qtr= qtr;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 20:15:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560637#M156826</guid>
      <dc:creator>noling</dc:creator>
      <dc:date>2019-05-21T20:15:04Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Targ</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560644#M156830</link>
      <description>&lt;P&gt;try this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test1(drop=acctno hicredit memberid);&lt;BR /&gt;set have;&lt;BR /&gt;retain count 0;&lt;BR /&gt;by cid opendate qtr;&lt;BR /&gt;cid_f=first.cid;&lt;BR /&gt;cid_l=last.cid;&lt;BR /&gt;opendate_f= first.opendate;&lt;BR /&gt;opendate_l=last.opendate;&lt;BR /&gt;qtr_f= first.qtr;&lt;BR /&gt;qtr_l=last.qtr;&lt;BR /&gt;if last.qtr=1 then count=count+1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 20:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560644#M156830</guid>
      <dc:creator>kulbshar</dc:creator>
      <dc:date>2019-05-21T20:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Targ</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560646#M156831</link>
      <description>&lt;P&gt;I can't understand what the don't want part is in your question. Are you saying you tried that and it didn't get the answer you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From the problem description it sounds like you want to number the quarters within the groups defined by CID and OPENDATE.&lt;/P&gt;
&lt;P&gt;So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by cid opendate qtr;
  if first.opendate then count=0;
  count + first.qtr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is it possible you have gaps in your data?&amp;nbsp; So that you have one record from 2018Q1 and the next from 2018Q3 but none from 2018Q2?&amp;nbsp; If so do you want the count to go 1,3 instead of 1,2?&amp;nbsp; If so you might want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by cid opendate qtr;
  count + intck('qtr',qtr,lag(qtr));
  if first.opendate then count=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 May 2019 20:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560646#M156831</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-21T20:19:30Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Targ</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560647#M156832</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/115150"&gt;@noling&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used this as a workaround.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cid $ acctno $ opendate mmddyy10. hicredit memberid $ qtr mmddyy10.;
datalines;
123	x	1/1/2018	2000	ABC	3/1/2018
123	x	1/1/2018	2000	ABC	6/1/2018
123	x	1/1/2018	2000	ABC	9/1/2018
123	x	1/1/2018	2000	ABC	12/1/2018
123	y	1/1/2018	2300	XYZ	3/1/2019
123	y	1/1/2018	2300	XYZ	6/1/2019
123	y	1/1/2018	2300	XYZ	9/1/2019
123	y	1/1/2018	2300	XYZ	12/1/2019
123	z	1/1/2017	5000	XYZ	3/1/2018
123	z	1/1/2017	5000	XYZ	6/1/2018
123	z	1/1/2017	5000	XYZ	9/1/2018
123	z	1/1/2017	5000	XYZ	12/1/2018
123	z	1/1/2017	5000	XYZ	3/1/2019
123	z	1/1/2017	5000	XYZ	6/1/2019
123	z	1/1/2017	5000	XYZ	9/1/2019
123	z	1/1/2017	5000	XYZ	12/1/2019
123	w	1/1/2017	10000	XYZ	3/1/2018
123	w	1/1/2017	10000	XYZ	6/1/2018
123	w	1/1/2017	10000	XYZ	9/1/2018
123	w	1/1/2017	10000	XYZ	12/1/2018
123	w	1/1/2017	10000	XYZ	3/1/2019
123	w	1/1/2017	10000	XYZ	6/1/2019
123	w	1/1/2017	10000	XYZ	9/1/2019
123	w	1/1/2017	10000	XYZ	12/1/2019
;
run;

proc sql;
create table counts as
select distinct cid, opendate, qtr
	from have
order by cid, opendate, qtr
;
quit;

data counts;
format opendate mmddyy10. qtr mmddyy10.;
set counts;
count+1;
by cid opendate qtr;
if first.opendate then count=1;
run;

proc sql;
create table want as
select a.*, b.count
	from have a left join counts b
	on a.cid=b.cid and a.opendate=b.opendate and a.qtr=b.qtr
order by cid, opendate, qtr
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But you're code is what I was looking for.&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 20:21:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560647#M156832</guid>
      <dc:creator>publicSynechism</dc:creator>
      <dc:date>2019-05-21T20:21:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Targ</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560653#M156835</link>
      <description>&lt;P&gt;This did not produce the results I was looking for, but Noling's provided a solution. Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 20:29:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560653#M156835</guid>
      <dc:creator>publicSynechism</dc:creator>
      <dc:date>2019-05-21T20:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Targ</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560660#M156838</link>
      <description>&lt;P&gt;Sorry I didn't read your requirement fully. glad you got the result. Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 20:40:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560660#M156838</guid>
      <dc:creator>kulbshar</dc:creator>
      <dc:date>2019-05-21T20:40:05Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a Sequence Count with By Variables and Increasing Count Only when Value Changes in Targ</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560664#M156842</link>
      <description>&lt;P&gt;I provided it as clarification if my description wasn't clear. Colleagues were confused by what I was trying to do and I received several suggestions producing the "don't want" results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Correct, your solution is producing the desired results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, there are gaps in the actual dataset and the second code block may come in handy.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for sharing &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 21 May 2019 20:52:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-Sequence-Count-with-By-Variables-and-Increasing-Count/m-p/560664#M156842</guid>
      <dc:creator>publicSynechism</dc:creator>
      <dc:date>2019-05-21T20:52:06Z</dc:date>
    </item>
  </channel>
</rss>

