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!
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
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
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.
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!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.