Rods2292
Rods2292

Reputation: 675

Merge two datasets in SAS. How to choose the variables of the merged data?

I have two datasets and I'm trying to merge them like it would be done if I were using a lef join approach on SQL.

I've created this code:

Data final_dataset;
merge table_1 (in=a) table_2; 
if a=1;
keep Var1 Var2 Var3 Var4 Var5;
run; 

The problem is that in both datasets I have variables with the same name. For example, Var1 and Var2 are both in table_1 and table_2 but I want that the merged data has only the variables Var1 and Var2 from table_1. How can I do that?

If it were a proc sql the code would be something like that:

proc sql;
create table final_dataset
as select 
A.Var1 A.Var2 B.Var3 B.Var4 B.Var5
from table_1 as A left join table_2 as B
on A.Var1 = B.Var1;

But I am trying to learn SAS language and I want to know if there is a way to do what I want in this language.

Upvotes: 1

Views: 133

Answers (1)

Tom
Tom

Reputation: 51566

Normally to merge you need BY variables. If you don't want variables from one of the datasets then just use the DROP= or KEEP= dataset option to eliminate them. If you want to keep variables from both but as separate variables then use the RENAME= dataset option.

data final_dataset;
   merge table_1 (in=in1 keep=var1 var2)
         table_2 (keep=var1 var3 var4 var5)
   ;
   by var1;
   if in1;
run;

Upvotes: 2

Related Questions