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

I have two dataset, I want to do like that:

Data 1

IDY
110
220

Data 2

IDY
1100
1200
2300
3100
4500

 

Wanted Data 3 (based on data 1 ID)

IDY
110
1100
1200
220
2300

 

I used the code:

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

But the y of data 1 with same ID will be overwrite, so how should I create data3?

 

Thank you very much.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

It appears you don't want to merge but to interleave the data sets - but then only keep rows with id's that exist in your first source table.

data have1;
  input id y$;
datalines;
1 10
2 20
;
data have2;
  input id y$;
datalines;
1 100
1 200
2 300
3 100
4 500
;

data want;
  set have1 (in=in1) have2;
  by id;
  retain keep_id;
  if first.id then keep_id=in1;
  if keep_id=1;
  drop keep_id;
run;

proc print data=want;
run;

 

View solution in original post

1 REPLY 1
Patrick
Opal | Level 21

It appears you don't want to merge but to interleave the data sets - but then only keep rows with id's that exist in your first source table.

data have1;
  input id y$;
datalines;
1 10
2 20
;
data have2;
  input id y$;
datalines;
1 100
1 200
2 300
3 100
4 500
;

data want;
  set have1 (in=in1) have2;
  by id;
  retain keep_id;
  if first.id then keep_id=in1;
  if keep_id=1;
  drop keep_id;
run;

proc print data=want;
run;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1 reply
  • 143 views
  • 4 likes
  • 2 in conversation