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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
Tom
Super User Tom
Super User

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;

Dipu
Calcite | Level 5

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

CTorres
Quartz | Level 8

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,

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 3 replies
  • 971 views
  • 3 likes
  • 3 in conversation