DATA Step, Macro, Functions and more

Conditional merging

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 111
Accepted Solution

Conditional merging

Hello,

I have a dataset1 that looks like this :

NOIND

ALT

NO_DWEL

CHOICE

X1

X2

1

1

842

1

1

1

1

2

773

0

1

1

1

3

5543

0

1

1

2

1

76

1

0

1

2

2

6087

0

0

1

2

3

8228

0

0

1

3

1

737

1

1

0

3

2

7339

0

1

0

3

3

3006

0

1

0

 

And a dataset2 that looks like this:

NO_DWEL

X1

X2

1

1

0

2

0

0

3

1

1

4

1

0

   

 

I want to perform a conditional merging only when CHOICE=0, so the value of X1 and X2 of the dataset1 will be replaced by those of the dataset2 for lines when CHOICE=0 and will be keep as it is when CHOICE=1. The solution I find is probably not the best one and become complicated for further works… I rename variables of dataset2, perform a normal merge and then replace variables in the new dataset with IF THEN DO.

Can someone please help me to find a more efficient way to do this?


Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Super User
Super User
Posts: 7,039

Re: Conditional merging

Posted in reply to Demographer

Add the variable CHOICE to DATASET2 and then include CHOICE in the BY statement for the MERGE.

 

data fix2 ; 
  set dataset2;
  choice=0;
run;

data want ;
  merge dataset1 (in=in1) fix2;
  by no_dwel choice;
  if in1;
run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,039

Re: Conditional merging

Posted in reply to Demographer
What are the BY variables for your merge? Is it just NO_DWEL?
Solution
‎09-25-2015 06:23 AM
Super User
Super User
Posts: 7,039

Re: Conditional merging

Posted in reply to Demographer

Add the variable CHOICE to DATASET2 and then include CHOICE in the BY statement for the MERGE.

 

data fix2 ; 
  set dataset2;
  choice=0;
run;

data want ;
  merge dataset1 (in=in1) fix2;
  by no_dwel choice;
  if in1;
run;
🔒 This topic is solved and locked.

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

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