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

Hi All,

I'm trying to merge 2 data sets

data one;

input id $ year ;

datalines;

a 1

a 2

;

run;

data two;

input id $ sc pt;

datalines;

a 1 50

a 2 60

a 3 40

;

run;

This is the code I used

proc sort data= one;by id;run;

proc sort data= two;by id;run;

data one_two;

merge one(in=a) two(in=b);

if a and b;

by id;

run;

This is the output I got

idyearscpt
a1150
a2260
a2340

Instead, How do I get this output

idyearscpt
a1150
a2260
a.340

THANK YOU!!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

When doing a MANY to MANY merge with a data step SAS will match the records one by one until one of the datasets does not have enough records in the group. The values from that dataset are then retained onto the rest of the records in the group.

To get what you want you just need to add an OUTPUT and CALL MISSING to the end of the data step.

data one_two;

  merge one(in=a) two(in=b);

  by id;

  if a and b;

  output;

  call missing(of _all_) ;

run;


View solution in original post

5 REPLIES 5
LinusH
Tourmaline | Level 20

You can't get that output in a secure way unless you can't define an extended primary key. Is year and sc related?

Data never sleeps
Tom
Super User Tom
Super User

When doing a MANY to MANY merge with a data step SAS will match the records one by one until one of the datasets does not have enough records in the group. The values from that dataset are then retained onto the rest of the records in the group.

To get what you want you just need to add an OUTPUT and CALL MISSING to the end of the data step.

data one_two;

  merge one(in=a) two(in=b);

  by id;

  if a and b;

  output;

  call missing(of _all_) ;

run;


MikeZdeb
Rhodochrosite | Level 12

Hi,  another way to get the desired output for those small data sets is to get rid of the BY statement .(by the way, you don't need a RUN statement after a DATALINES file) ...

data one;

input id $ year @@;

datalines;

a 1 a 2

;

data two;

input id $ sc pt @@;

datalines;

a 1 50 a 2 60 a 3 40

;

data one_two;

merge one two;

run;

DATA SET ONE-TWO ...

Obs    id    year    sc    pt

1     a       1      1    50

2     a       2      2    60

3     a       .      3    40

Ksharp
Super User

Code: Program

data one;
input id $ year @@;
datalines;
a 1 a 2
;

data two;
input id $ sc pt @@;
datalines;
a 1 50 a 2 60 a 3 40
;
data one;
set one;
sc=year;
run;
data want;
merge one two;
by id sc;
run;


Ksharp
Super User

Code: Program

data one;
input id $ year @@;
datalines;
a 1 a 2
;

data two;
input id $ sc pt @@;
datalines;
a 1 50 a 2 60 a 3 40
;
run;
/****Alternative Way*****/
proc sql;
create table _want as
  select b.*,a.year
   from one as a right join two as b
   on a.id=b.id and a.year=b.sc;
quit;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 904 views
  • 0 likes
  • 5 in conversation