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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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