BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jay_Aguilar
Calcite | Level 5

Hello everyone,

 

I have a list which looks like this (table 1):

IDinformation
1abc
2def
3ghi

I now want to get data from another table (table 2) which has many more ID's  (e.g. ID 1 to 10) but I only want those datasets which have the ID defined in my first table (table 1). I do not want to indicate which ID's to take via a WHERE statement and write every ID which should be taken but somehow make it more flexible. So if at one point in time I need to add an ID to my first table (table 1),  it automatically takes the added ID into account when getting data from my second table (table 2). I guess I am looking for something like the VLOOKUP in excel, just that I do not want to join the the tables.

I hope I could make clear what I need and would be very happy if someone could help me with that.

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Are you asking to make a new dataset that is a subset of an existing dataset?

Let's call you list of id table as LIST, the existing dataset has HAVE and the desired result as WANT.

 

You can use data step with MERGE.

data want;
  merge have list(in=inlist);
  by id;
  if inlist;
run;

Note this requires that both dataset as sorted.

You can use an SQL query.

proc sql;
create table want as
select * from have
where id in (select id from list)
;
quit;

Either of these can be created as views instead of tables if you the results to automatically reflect changes to HAVE and LIST.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Are you asking to make a new dataset that is a subset of an existing dataset?

Let's call you list of id table as LIST, the existing dataset has HAVE and the desired result as WANT.

 

You can use data step with MERGE.

data want;
  merge have list(in=inlist);
  by id;
  if inlist;
run;

Note this requires that both dataset as sorted.

You can use an SQL query.

proc sql;
create table want as
select * from have
where id in (select id from list)
;
quit;

Either of these can be created as views instead of tables if you the results to automatically reflect changes to HAVE and LIST.

Jay_Aguilar
Calcite | Level 5

Thank you very much fot the quick response!

Yes, I have to create a new dataset.

Kurt_Bremser
Super User

A more modern approach to do a lookup is the hash object:

data want;
set have;
if _n_ = 1
then do;
  declare hash lookup (dataset:"list (keep=id)");
  lookup.definekey("id");
  lookup.definedone();
end;
if lookup.find() = 0;
run;

Note that this needs to be re-run anytime one of the input datasets changes (a view would not need this), but if the result is to be used multiple times, it will provide better performance.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 301 views
  • 0 likes
  • 3 in conversation