BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
awardell
Obsidian | Level 7

I have a dataset that looks like the following table:

 

IDDXDATEVISIT_SEQUENCE.... other different vars
112-03-20211 
112-03-20212 
206-04-20150 
305-23-20201 
305-23-20202 
404-11-20191 
407-24-20202 

I need to count the number of individuals (determined by ID number) that have a sequence number 1 and 2 with the same diagnosis date. In this example, I would be looking to count 2 individuals. 

I think this could be done by creating a data set with just these individuals and counting the first ID number, or a different way. Thank you for any help! I am fairly new to data sets with multiple rows for each ID. 

1 ACCEPTED SOLUTION

Accepted Solutions
r_behata
Barite | Level 11

One Way :

 

data have;
input ID $ DXDATE:mmddyy10.	VISIT_SEQUENCE;
format DXDATE mmddyy10.;
cards;
1 12-03-2021 1	 
1 12-03-2021 2	 
2 06-04-2015 0	 
3 05-23-2020 1	 
3 05-23-2020 2	 
4 04-11-2019 1	 
4 07-24-2020 2
;
run;

data want;
	merge have(in=a) have(in=b rename=(VISIT_SEQUENCE=_VISIT_SEQUENCE DXDATE=_DXDATE ) where=(_VISIT_SEQUENCE=2));
	by id;
	
	if a;
	
	if VISIT_SEQUENCE=1 and  DXDATE=_DXDATE then count=1;
	
	drop _:;
run;

View solution in original post

8 REPLIES 8
r_behata
Barite | Level 11

One Way :

 

data have;
input ID $ DXDATE:mmddyy10.	VISIT_SEQUENCE;
format DXDATE mmddyy10.;
cards;
1 12-03-2021 1	 
1 12-03-2021 2	 
2 06-04-2015 0	 
3 05-23-2020 1	 
3 05-23-2020 2	 
4 04-11-2019 1	 
4 07-24-2020 2
;
run;

data want;
	merge have(in=a) have(in=b rename=(VISIT_SEQUENCE=_VISIT_SEQUENCE DXDATE=_DXDATE ) where=(_VISIT_SEQUENCE=2));
	by id;
	
	if a;
	
	if VISIT_SEQUENCE=1 and  DXDATE=_DXDATE then count=1;
	
	drop _:;
run;
Reeza
Super User
This can get you started.

proc sql;
create table want as
select id, dxdate, sequence, count(distinct sequence) as num_sequences
from have
group by ID, dxdate
where visit_sequences in (1, 2);
quit;
awardell
Obsidian | Level 7
Thank you! I will experiment in SQL more!
Astounding
PROC Star
I would use a tool that is built to count:

proc freq data=have noprint;
tables id * dxdate / out=want (drop=percent where=(count > 1) );
where visit_sequence in (1, 2);
run;

Whether or not you select this approach, it's a common tool that is worth learning.
awardell
Obsidian | Level 7
Thank you! I had never heard of this feature! It will prove to be a handy tool!
Ksharp
Super User
data have;
input ID $ DXDATE:mmddyy10.	VISIT_SEQUENCE;
format DXDATE mmddyy10.;
cards;
1 12-03-2021 1	 
1 12-03-2021 2	 
2 06-04-2015 0	 
3 05-23-2020 1	 
3 05-23-2020 2	 
4 04-11-2019 1	 
4 07-24-2020 2
;
run;

proc sql;
select count(distinct id) from
(
select id
 from have
  where VISIT_SEQUENCE ne 0
   group by id,dxdate
    having count(*)>1
);
quit;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2214 views
  • 4 likes
  • 5 in conversation