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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 620 views
  • 4 likes
  • 2 in conversation