BookmarkSubscribeRSS Feed
luvscandy27
Quartz | Level 8

Hello everyone, 

I have a question that I can not for the life of me figure out. I have the following sample dataset.

ID appt_type qtr_1
1852558 Specialty 1
1852558 Future .
1852558 Future .
1852558 Future .
0001D9DA82 Future .
00026A2508 Specialty 1

What I would like to do is keep the ids that have qtr_1 = 1 and get a count of how many 

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.

From the sample data above, I am trying to  achieve this outcome:

ID appt_type qtr_1 count 
1852558 Specialty 1  
1852558 Future . 1
1852558 Future . 2
1852558 Future . 3
2 REPLIES 2
maguiremq
SAS Super FREQ
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;
Obs 	ID 	appt_type 	qtr_1 	count
1 	1852558 	Specialty   1   .
2 	1852558 	Future 	     . 	1
3 	1852558 	Future 	     . 	2
4 	1852558 	Future 	     . 	3

May run into issues depending on the complexity of your underlying data.

Ksharp
Super User
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)>1 order by id,appt_type desc,n;
quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 455 views
  • 0 likes
  • 3 in conversation