Solved
Contributor
Posts: 36

# Remove Duplicates and merge records efficiently

Hi All

Please please help. I have this really large dataset that I need to clean up so I need this process to be optimized the best way possible.

Please see below for sample records:

 INPUT: SEQ Num1 Num2 Class Cost Time ID 1 12 13 . 0 50 A123 2 12 13 A 0 40 A123 3 12 18 A 60 10 A123 OUTPUT: SEQ Num1 Num2 Class Cost Time ID 2 12 13 A 0 90 A123 3 12 18 A 60 10 A123

I need to search through all the data to see where num1 and num2 match and where cost = 0 and summarize the time accordingly in 1 record along with the class as per the above. I require the desired output.

Thanks for the help.

Accepted Solutions
Solution
‎02-07-2013 09:45 AM
Super Contributor
Posts: 578

## Re: Remove Duplicates and merge records efficiently

just to enforce the sort...

proc sort in=have out=have;

by num1 num2 seq;

run;

data want;

set have;

by num1 num2;

if last num2;

run;

All Replies
Super User
Posts: 5,876

## Re: Remove Duplicates and merge records efficiently

If data is already sorted (as in your example), it could be done in one step.

What about Class and ID, to you want the value from the last row of the ones that you want to condense?

Try to elaborate using BY, FIRST./LAST. and RETAIN.

Data never sleeps
Contributor
Posts: 36

## Re: Remove Duplicates and merge records efficiently

Thanks for the response LinusH. Ok Ignore the question above. Let me rephrase.

Ideally I want to retain the last records class and ID where num1 and num2 are the same. My condition will be if num1 and num2 are the same and cost = 0 than take the last record.

eg:

 INPUT: SEQ Num1 Num2 Class Cost Time ID 1 12 13 . 0 50 A123 2 12 13 A 0 60 A123 3 12 18 A 60 10 A123 OUTPUT: SEQ Num1 Num2 Class Cost Time ID 2 12 13 A 0 60 A123 3 12 18 A 60 10 A123

Thank you.

Solution
‎02-07-2013 09:45 AM
Super Contributor
Posts: 578

## Re: Remove Duplicates and merge records efficiently

just to enforce the sort...

proc sort in=have out=have;

by num1 num2 seq;

run;

data want;

set have;

by num1 num2;

if last num2;

run;

Contributor
Posts: 36

## Re: Remove Duplicates and merge records efficiently

Is this way the best way to select the last record for a dataset were num1 and num2 are the same?? Also cost will have to be 0.

My data set has 80612210 obs. I dont want this process to take too long as I have quite a few data steps after this one.

Not applicable
Posts: 1

## Re: Remove Duplicates and merge records efficiently

You need to have the data sorted in the order you want it, and exclude the data you don't want. If your data is already sorted, put the where clause on the set statement. To save time/space, only keep the variables you need. (I'm not sure if you need to keep seq after the data is sorted, for example.)

proc sort data=have(keep=num1 num2 seq class cost time ID where=(cost=0)) out=haveSorted(drop=cost seq);

by num1 num2 seq;

run;

data want;

set haveSorted;

by num1 num2;

if last.num2 then output;

run;

Posts: 3,167

## Re: Remove Duplicates and merge records efficiently

Not very sure if this is what you are after, but it seems to have the expected output for your as-is incoming data:

data have;

input SEQ Num1 Num2 Class:\$1. Cost Time ID :\$4.;

cards;

1 12 13 . 0 50 A123

2 12 13 A 0 60 A123

3 12 18 A 60 10 A123

;

/*if pre-sorted by num1 num2*/

data want;

set have;

by num1 num2;

if first.num2*last.num2=1 then output;

else if last.num2 and cost=0 then output;

run;

Haikuo

🔒 This topic is solved and locked.