Merging many to few

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Merging many to few

Hello all,

I would be happy to get your guidance in the following query.

I have two data sets- Data1 and Data2    and I need Dataset called data3 as shown below                                                                                                    

Data1- Looks like                                                    Data2-Looks like                                           Data3- Output required          

Var1 (Num)  Var2 (Char) (it is blank)                         Var1 (Num)       Var2 (Char)                            Var1 (Num)       Var2 (Char)

1                  Blank                                                 1                     green                                      1                     green      

1                  Blank                                                 2                     Red                                        1                     green 

1                                                                           3                     Orange                                   1                      green

2                                                                           4                     White                                     2                      Red

2                                                                                                                                               2                      Red

2                                                                                                                                               .

2

2                                                                                                                                                .

3                                                                                                                                                3                    Orange and for 4 White and so on.  

3

3

3                                                                        I used the following- after sorting-----data data3; merge data1 (in=a) data2 (in=b); by var1; if a; run;

3

4

4

4

4

4

4

Please guide me

Thank you


Accepted Solutions
Solution
‎01-10-2014 12:59 PM
Super User
Super User
Posts: 6,495

Re: Merging many to few

Problem is the existence of VAR2 on your 'many' table.  Since it reads only one record from the 'few' table only the first value will be replaced.  Easiest solution is to DROP or RENAME the variable.

data data3;

  merge data1 (in=in1 drop=var2) data2 (in=in2);

  by var1;

  if in1;

run;

View solution in original post


All Replies
Solution
‎01-10-2014 12:59 PM
Super User
Super User
Posts: 6,495

Re: Merging many to few

Problem is the existence of VAR2 on your 'many' table.  Since it reads only one record from the 'few' table only the first value will be replaced.  Easiest solution is to DROP or RENAME the variable.

data data3;

  merge data1 (in=in1 drop=var2) data2 (in=in2);

  by var1;

  if in1;

run;

Contributor
Posts: 43

Re: Merging many to few

Thank you Tom, I thought the same thinkg but could not do it. thanks.
Thank you so much Torres.

Regular Contributor
Posts: 180

Re: Merging many to few

Try this:

Data Data1;

  INPUT VAR1;

  VAR2='        ';

cards;

1       

1          

1         

2        

2        

2        

2

2        

3           

3

3

3  

3

4

4

4

4

4

4

;

RUN;

DATA Data2;

  LENGTH VAR1 8 VAR2 $ 8;

  INPUT VAR1 VAR2;

CARDS;

1     green

2     Red

3     Orange

4     White

;

RUN;

data data3;

  merge data1 (keep=var1) data2;

  by var1;

run;

Regards,

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 195 views
  • 3 likes
  • 3 in conversation