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

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
;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 928 views
  • 1 like
  • 2 in conversation