SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Extracting patients based on number of observations

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 104
Accepted Solution

Extracting patients based on number of observations

Hi All,

 

I have a data set of patient visits. We want to extract only the patients who have at least 2 visits. How do I do this?

 

Have:

id      visit#

a         1

a         2

a         3

a         4

b         1

c         1

c         2

c         3

d         1

 

The data set I want would only include ids a and c.

 

Thanks!

 


Accepted Solutions
Solution
‎06-30-2017 10:33 AM
Frequent Contributor
Posts: 120

Re: Extracting patients based on number of observations

Something like this?

 

data test;

length id visit $1;

input id visit;

datalines;

a 1

a 2

a 3

a 4

b 1

c 1

c 2

c 3

d 1

;

run;

proc sql;

create table sel as

select id

from test

group by id

having count(visit) > 1

;

quit;

data test1;

merge test (in=a) sel (in=b);

by id;

if a and b;

run;

 

//Fredrik

View solution in original post


All Replies
Solution
‎06-30-2017 10:33 AM
Frequent Contributor
Posts: 120

Re: Extracting patients based on number of observations

Something like this?

 

data test;

length id visit $1;

input id visit;

datalines;

a 1

a 2

a 3

a 4

b 1

c 1

c 2

c 3

d 1

;

run;

proc sql;

create table sel as

select id

from test

group by id

having count(visit) > 1

;

quit;

data test1;

merge test (in=a) sel (in=b);

by id;

if a and b;

run;

 

//Fredrik

Super User
Super User
Posts: 7,401

Re: Extracting patients based on number of observations

data want;
  set have;
  by id;
  if if first.id and last.id then delete;
run;

Removes any records where there is only one recor per id.

Frequent Contributor
Posts: 120

Re: Extracting patients based on number of observations

Sweet!
Frequent Contributor
Posts: 104

Re: Extracting patients based on number of observations

I like it, but we may decide we want to include people with more observations down the line. I think I'll try FredrikE's method. Thanks!

Frequent Contributor
Posts: 120

Re: Extracting patients based on number of observations

Replace my merge with:
Proc sql;
Create table new as select a.* from test as a inner join visits as b on a.id = b.id; quit;
Then you don not need to have data sorted.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 188 views
  • 2 likes
  • 3 in conversation