hello, I have two data set 'have' and 'id'. I would like to filter 'have' data based on the subject in 'id' and keep record order unchanged. What should I do? sort and them merge will not work. also I have a record with empty 'subject' that should be retained, as ' " is in subject value in data 'id'.
data have;
input subject $ year disease1 disease2 disease3;
datalines;
a 2019 1 1 1
2020 0 0 0
a 2021 0 0 0
a 2022 0 0 0
b 2019 0 1 1
f 2020 1 0 0
c 2021 1 0 0
w 2022 0 0 1
;
data id;
input subject $;
datalines;
a
b
c
d
e
f
g
;
You create an order variable to allow the sequence to be maintained. You filter on whether or not subject in data set HAVE is also in data set ID or subject is blank.
data have;
infile cards dlm=',' dsd;
input subject $ year disease1 disease2 disease3;
datalines;
a,2019,1,1,1
,2020,0,0,0
a,2021,0,0,0
a,2022,0,0,0
b,2019,0,1,1
f,2020,1,0,0
c,2021,1,0,0
w,2022,0,0,1
;
data id;
input subject $;
datalines;
a
b
c
d
e
f
g
;
data have1;
set have;
n=_n_;
run;
proc sql;
create table want as select *
from have1
where subject in (select distinct subject from id) or subject=' '
order by n;
quit;
You create an order variable to allow the sequence to be maintained. You filter on whether or not subject in data set HAVE is also in data set ID or subject is blank.
data have;
infile cards dlm=',' dsd;
input subject $ year disease1 disease2 disease3;
datalines;
a,2019,1,1,1
,2020,0,0,0
a,2021,0,0,0
a,2022,0,0,0
b,2019,0,1,1
f,2020,1,0,0
c,2021,1,0,0
w,2022,0,0,1
;
data id;
input subject $;
datalines;
a
b
c
d
e
f
g
;
data have1;
set have;
n=_n_;
run;
proc sql;
create table want as select *
from have1
where subject in (select distinct subject from id) or subject=' '
order by n;
quit;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.