BookmarkSubscribeRSS Feed
Miracle
Barite | Level 11

Dear All,

How do we write the proc sql to retrieve only the observation rows when the same ID and the same date are present in both the data please?

Thanking you all in advance.

6 REPLIES 6
PeterClemmensen
Super User

What do you mean by "both the data" ? 

 

Please be more specific and show us your data.

Kurt_Bremser
Super User
proc sql;
create table want as
  select t1.*
  from t1
  where id in (select distinct id from t2)
;
quit;

Replace table and column names as needed

Depending on dataset size, a DATA step with hash approach might perform better.

PGStats
Opal | Level 21

You can't use the IN operator for subsetting on two variables, but you can use the EXISTS operator:

 

proc sql;
create table want as
  select *
  from t1 as a
  where exists (select * from t2 where id=a.id and date=a.date);
quit;
PG
Miracle
Barite | Level 11

Thanks @PeterClemmensen , @Kurt_Bremser  and @PGStats for your response.
I tried the proc sql as you advised but the code never stop running even for an entire day.
I wonder if it is due to the huge datasets i.e 10000k rows of data.
What other ways would you suggest please?
I am completely new to hash object so I don't understand how to write the syntax.
Thanking you in advance.

Kurt_Bremser
Super User

Then a hash approach should be tried.

 

Which would look like this:

data want;
set t1;
if _n_ = 1
then do;
  declare hash t2 (dataset:("t2");
  t2.definekey("id","date");
  t2.definedone();
end;
if t2.check() = 0;
run;

This will work as long as the two variables * observations from t2 fit into memory.

Tom
Super User Tom
Super User

@Miracle wrote:

Dear All,

How do we write the proc sql to retrieve only the observation rows when the same ID and the same date are present in both the data please?

Thanking you all in advance.


Why would you use SQL for such a simple request?

data want ;
  merge dataset1 (in=in1)  dataset2(in=in2);
  by id date;
  if in1 and in2;
run;

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
  • 6 replies
  • 413 views
  • 0 likes
  • 5 in conversation