timothy gowers
timothy gowers

Reputation:

Merging Datasets in SAS

I have a rather simplistic question.

Is there any way to merge $n$ data sets in SAS where $n > 2$. I know how to merge 2 data sets.

Thanks

gowers

Upvotes: 0

Views: 950

Answers (3)

Mozan Sykol
Mozan Sykol

Reputation: 367

Even though you have said that you want to "merge" datasets, note that the MERGE statement is not the only option. If your merging key has duplicates in more than 1 dataset, then using the MERGE statement will might give logically wrong results even though it would work without complaining. In that case, you can use PROC SQL - I also recall that PROC SQL can be more efficient from SAS 9.1 onwards.

Example -

proc sql;
select <fieldlist>
from data1 t1, data2 t2, data3 t3, data4 t4
where <join condition>;
quit;

Upvotes: 1

Jay Corbett
Jay Corbett

Reputation: 28391

In addition to the code @itzy provided, you can identify your data sets using an IN= option on the MERGE statement. This allows you only accept the matching you need. Also, you must have common variable names to use in your BY statement. You can include a RENAME= statement to create a common variable for use in your BY statement.

(Untested code)

data all;
 merge ds1(in=one rename=(ds1_id=id)) 
       ds2(in=two rename=(ds2_id=id))
       ds3(in=three rename=(ds3_id=id))
       ;
 by some_list_of_variables;
 if one and two and three ; /* Creates only matching records from all */ 
run;

Upvotes: 2

itzy
itzy

Reputation: 11755

You can merge multiple data sets using the same syntax as for just two:

data all;
 merge ds1 ds2 ds3 ...;
 by some_list_of_variables;
run;

If you have many data sets you want to merge, you may want to right a macro that lists them all.

Upvotes: 2

Related Questions