BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

 

I have a question. 

 

I am merging two datasets. why only first OBS is populated in my output data.  Please help. Thank you

 

code:

data data3;
merge data2(in=a) data1(in=b);
by id;
if a;
run;

 

data1

id      value

1         10

2          20

3          30

 

data2

id 

1

1

2

2

 

output getting;

id    val

1     10

1

2     20

2

2

output needed;

id    val

1     10

1      10

2     20

2      20

2     20

 

4 REPLIES 4
LinusH
Tourmaline | Level 20
Not thought about this consequence when using the if a; concept.
Not tested but my theory is that for observation 2 there is no corresponding observation in b: hence the missing value.
Regardless I think a simple SQL inner join is better in this situation.
Data never sleeps
Shmuel
Garnet | Level 18

I'm using SAS 9.4 with SAS UE on linux.

Attached is the code and result.

 

The code is a copy of your (@knveraraju91 ) post, addaptedf to create test data.

The result is as you wanted.

 

Check your log and code again. It should work.

If not - what SAS version and platform you use ?

Astounding
PROC Star

The results you are getting would typically occur if your second data set also contained VAL (or is it VALUE?), but with all missing values.  To get rid of it, you can drop it as late as on the MERGE statement:

 

data want;

merge data2 (in=a drop=val) data1;

by id;

if a;

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
  • 4 replies
  • 866 views
  • 2 likes
  • 5 in conversation