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,

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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