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;
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.