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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

2 REPLIES 2
Tom
Super User Tom
Super User
What are the BY variables for your merge? Is it just NO_DWEL?
Tom
Super User Tom
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2137 views
  • 1 like
  • 2 in conversation