BookmarkSubscribeRSS Feed
alabchaa
Calcite | Level 5

I have 2 SAS tables, both of them have similar IDs. I want to filter table 1 to have only the IDs in table 2. 

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Hi and welcome to the SAS Communtiy 🙂

 

See if this is applicable to your actual problem. Otherwise, don't hesitate to ask

 

data table1;
input id var1;
datalines;
1 10
1 20
2 30
2 40
3 50
3 60
;

data table2;
input id var2;
datalines;
1 10
1 20
3 50
3 60
;

data want;
    if _N_=1 then do;
        declare hash h(dataset:'table2');
        h.definekey('id');
        h.definedone();
    end;

    set table1;

    if h.check()=0;
run;
yabwon
Amethyst | Level 16

Hi.

 

If ID are integers you could try direct addressing

 

For range up to 1mln it will required ~8MB of RAM, for 10mln it will required ~8oMB, for 100mln it will required ~8ooMB, and so on...

 

All the best

Bart

data table1;
input id var1;
datalines;
1 10
1 20
2 30
2 40
3 50
3 60
;
run;

data table2;
input id var2;
datalines;
1 10
1 20
3 50
3 60
;
run;

options fullstimer msglevel=i;
%let size = 1000000;
data want;
    array t[&size.] _temporary_; 

    do until(eof1);
      set table2(keep = id) end=eof1;
      t[id] = 1;
    end;

    do until(eof2);
      set table1 end=eof2;
      if t[id] = 1 then output;
    end;
  stop;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



novinosrin
Tourmaline | Level 20
data table1;
input id var1;
datalines;
1 10
1 20
2 30
2 40
3 50
3 60
;
run;

data table2;
input id var2;
datalines;
1 10
1 20
3 50
3 60
;
run;

proc sql;
create table want as
select *
from table1
where id in (select distinct id from table2);
quit;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

Register now

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
  • 3 replies
  • 6074 views
  • 3 likes
  • 4 in conversation