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

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
FredrikE
Rhodochrosite | Level 12

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

5 REPLIES 5
FredrikE
Rhodochrosite | Level 12

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26
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.

FredrikE
Rhodochrosite | Level 12
Sweet!
sarahsasuser
Quartz | Level 8

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!

FredrikE
Rhodochrosite | Level 12
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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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