How can I merge only one (or more specific) variables from one dataset (dataset 2) to another (dataset 1) without merging all variables from dataset 2 onto dataset 1 ?
Dataset 1:
ID Matnr Code_1 Code_2 Code_3 Code_4
19 1 P200 T500 M870
19 2 P400 M871
20 1 P400 M871
20 2 P200 T800 T812 M871
Dataset 2:
ID Matnr Proc City
19 1 500 AA
19 1 500 AA
19 2 500 AA
20 1 700 CP
20 2 700 CP
20 2 700 CP
Dataset 1 with only Proc added from dataset 2:
ID Matnr Code_1 Code_2 Code_3 Code_4 Proc
19 1 P200 T500 M870 500
19 2 P400 M871 500
20 1 P400 M871 700
20 2 P200 T800 T812 M871 700
Or..
data want;
merge one two(drop = City);
by ID Matnr;
if last.Matnr;
run;
Result:
ID Matnr Code_1 Code_2 Code_3 Code_4 Proc 19 1 P200 T500 M870 500 19 2 P400 M871 500 20 1 P400 M871 700 20 2 P200 T800 T812 M871 700
One way..
data one;
infile datalines missover;
input ID Matnr (Code_1-Code_4)($);
datalines;
19 1 P200 T500 M870
19 2 P400 M871
20 1 P400 M871
20 2 P200 T800 T812 M871
;
data two;
input ID Matnr Proc City $;
datalines;
19 1 500 AA
19 1 500 AA
19 2 500 AA
20 1 700 CP
20 2 700 CP
20 2 700 CP
;
data want(drop = rc);
if _N_ = 1 then do;
dcl hash h(dataset : "two");
h.definekey("ID", "Matnr");
h.definedata("Proc");
h.definedone();
end;
set one;
if 0 then set two(keep=Proc);
rc = h.find();
run;
Or..
data want;
merge one two(drop = City);
by ID Matnr;
if last.Matnr;
run;
Result:
ID Matnr Code_1 Code_2 Code_3 Code_4 Proc 19 1 P200 T500 M870 500 19 2 P400 M871 500 20 1 P400 M871 700 20 2 P200 T800 T812 M871 700
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.