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?
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;
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.