<?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: Grouping and counting by ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Grouping-and-counting-by-ID/m-p/766456#M242909</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input ID :$10. appt_type :$10. qtr_1;
datalines;
1852558 	Specialty 	1
1852558 	Future 	.
1852558 	Future 	.
1852558 	Future 	.
0001D9DA82 	Future 	.
00026A2508 	Specialty 	1
;
run;

proc sort
	data = have;
		by id descending qtr_1;
run;

data want;
	set have;
	by id descending qtr_1;
		if first.id and first.qtr_1 = 1 then call missing(count);
			else count + 1;
		if (first.id and last.id and qtr_1 = 1) or (qtr_1 = . and count = .) then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs 	ID 	appt_type 	qtr_1 	count
1 	1852558 	Specialty   1   .
2 	1852558 	Future 	     . 	1
3 	1852558 	Future 	     . 	2
4 	1852558 	Future 	     . 	3&lt;/PRE&gt;
&lt;P&gt;May run into issues depending on the complexity of your underlying data.&lt;/P&gt;</description>
    <pubDate>Tue, 07 Sep 2021 18:44:48 GMT</pubDate>
    <dc:creator>maguiremq</dc:creator>
    <dc:date>2021-09-07T18:44:48Z</dc:date>
    <item>
      <title>Grouping and counting by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-and-counting-by-ID/m-p/766447#M242907</link>
      <description>&lt;P&gt;Hello everyone,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a question that I can not for the life of me figure out. I have the following sample dataset.&lt;/P&gt;
&lt;TABLE width="254"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="88"&gt;ID&lt;/TD&gt;
&lt;TD width="102"&gt;appt_type&lt;/TD&gt;
&lt;TD width="64"&gt;qtr_1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="88"&gt;1852558&lt;/TD&gt;
&lt;TD width="102"&gt;Specialty&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="88"&gt;1852558&lt;/TD&gt;
&lt;TD width="102"&gt;Future&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="88"&gt;1852558&lt;/TD&gt;
&lt;TD width="102"&gt;Future&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="88"&gt;1852558&lt;/TD&gt;
&lt;TD width="102"&gt;Future&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="88"&gt;0001D9DA82&lt;/TD&gt;
&lt;TD width="102"&gt;Future&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="88"&gt;00026A2508&lt;/TD&gt;
&lt;TD width="102"&gt;Specialty&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;What I would like to do is keep the ids that have qtr_1 = 1 and get a count of how many&amp;nbsp;&lt;/P&gt;
&lt;P&gt;appt_types happened after the first appt_type. If an id has qtr_1 = 1 and but only has one appt_type then that id can be excluded.&lt;/P&gt;
&lt;P&gt;From the sample data above, I am trying to&amp;nbsp; achieve this outcome:&lt;/P&gt;
&lt;TABLE width="277"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;ID&lt;/TD&gt;
&lt;TD width="85"&gt;appt_type&lt;/TD&gt;
&lt;TD width="64"&gt;qtr_1&lt;/TD&gt;
&lt;TD width="64"&gt;count&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;1852558&lt;/TD&gt;
&lt;TD width="85"&gt;Specialty&lt;/TD&gt;
&lt;TD width="64"&gt;1&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;1852558&lt;/TD&gt;
&lt;TD width="85"&gt;Future&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;1852558&lt;/TD&gt;
&lt;TD width="85"&gt;Future&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;1852558&lt;/TD&gt;
&lt;TD width="85"&gt;Future&lt;/TD&gt;
&lt;TD width="64"&gt;.&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 07 Sep 2021 18:13:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-and-counting-by-ID/m-p/766447#M242907</guid>
      <dc:creator>luvscandy27</dc:creator>
      <dc:date>2021-09-07T18:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping and counting by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-and-counting-by-ID/m-p/766456#M242909</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input ID :$10. appt_type :$10. qtr_1;
datalines;
1852558 	Specialty 	1
1852558 	Future 	.
1852558 	Future 	.
1852558 	Future 	.
0001D9DA82 	Future 	.
00026A2508 	Specialty 	1
;
run;

proc sort
	data = have;
		by id descending qtr_1;
run;

data want;
	set have;
	by id descending qtr_1;
		if first.id and first.qtr_1 = 1 then call missing(count);
			else count + 1;
		if (first.id and last.id and qtr_1 = 1) or (qtr_1 = . and count = .) then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs 	ID 	appt_type 	qtr_1 	count
1 	1852558 	Specialty   1   .
2 	1852558 	Future 	     . 	1
3 	1852558 	Future 	     . 	2
4 	1852558 	Future 	     . 	3&lt;/PRE&gt;
&lt;P&gt;May run into issues depending on the complexity of your underlying data.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Sep 2021 18:44:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-and-counting-by-ID/m-p/766456#M242909</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-09-07T18:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Grouping and counting by ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Grouping-and-counting-by-ID/m-p/766563#M242952</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input ID :$10. appt_type :$10. qtr_1;
datalines;
1852558 	Specialty 	1
1852558 	Future 	.
1852558 	Future 	.
1852558 	Future 	.
0001D9DA82 	Future 	.
00026A2508 	Specialty 	1
;
run;

data temp;
 set have;
 by id appt_type notsorted;
 if first.appt_type then n=0;
 n+1;
 if appt_type='Specialty' then n=.;
run;
proc sql;
create table want as
select * from temp group by id having count(distinct appt_type)&amp;gt;1 order by id,appt_type desc,n;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Sep 2021 12:56:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Grouping-and-counting-by-ID/m-p/766563#M242952</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-08T12:56:19Z</dc:date>
    </item>
  </channel>
</rss>

