BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MABRINCH
Fluorite | Level 6

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

      

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

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;
PeterClemmensen
Tourmaline | Level 20

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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