Help using Base SAS procedures

Removing select cases from a dataset

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Removing select cases from a dataset

Hi,

I have two datasets in long format that have a variable “ID” in common.

If DatasetB doesn’t have a matching ID with DatasetA, I want to delete unmatched cases in DatasetB.

Is there any way to do this without merging the two datasets?

Have DatasetA:

ID

VarA

VarB

1

Abcd

1232

1

Erfsdjdgr

94847

2

Fakjsdf;asie

5342

3

Kjdafjdh

0589576

3

Hfajdhfdh

143

Have DatasetB:

ID

VarX

VarY

2

124363

Abjdkafh

2

5463

Giagaojdfl

3

9378464

Nadfaei

5

23756

Nann

6

987543

Ueyrf

7

625364

Qodiafe

Want new_DatasetB

ID

VarX

VarY

2

124363

Abjdkafh

2

5463

Giagaojdfl

3

9378464

Nadfaei

Thank you for your help!


Accepted Solutions
Solution
‎02-02-2015 10:48 PM
Respected Advisor
Posts: 4,654

Re: Removing select cases from a dataset

The simplest way (create a new table, no sorting) would be:

proc sql;

create table new_datasetB as

select * from datasetB

where ID in (select ID from datasetA);

quit;

if you would prefer to modify datasetB (could be faster if datasetB is large) :

proc sql;

delete from datasetB

where ID not in (select ID from datasetA);

quit;

PG

PG

View solution in original post


All Replies
Solution
‎02-02-2015 10:48 PM
Respected Advisor
Posts: 4,654

Re: Removing select cases from a dataset

The simplest way (create a new table, no sorting) would be:

proc sql;

create table new_datasetB as

select * from datasetB

where ID in (select ID from datasetA);

quit;

if you would prefer to modify datasetB (could be faster if datasetB is large) :

proc sql;

delete from datasetB

where ID not in (select ID from datasetA);

quit;

PG

PG
Occasional Contributor
Posts: 17

Re: Removing select cases from a dataset

Thank you very much!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 204 views
  • 1 like
  • 2 in conversation