DATA Step, Macro, Functions and more

how to select only required obs when concatenating datassets

Reply
Super Contributor
Posts: 272

how to select only required obs when concatenating datassets

Dear,

I need to concatenate the datasets "one and two" to  get last date from data two. 

 

In data two there are more  subjects than data one. I need to have in my output only subjects that are in dataone. Please help . Thank you

 

data one;
input id date date9.
datalines;
1 05OCT2015
2 06OCT2015
3 07OCT2015
;
data two;
input id date date9.;
datalines;
1 05DEC2015
2 06DEC2015
3 07DEC2015 
4 08DEC2015
;

code:

 

data three;

set one two;

run;

 

output needed;

id date

1 05OCT2015
2 06OCT2015
3 07OCT2015

1 05DEC2015
2 06DEC2015
3 07DEC2015

Trusted Advisor
Posts: 1,909

Re: how to select only required obs when concatenating datassets

Posted in reply to knveraraju91

UNTESTED CODE

data want;
    merge one(in=in1 rename=(date=date1)) two(in=in2 rename=(date=date2));
    by id;
    if in1 and in2;
run;
Super Contributor
Posts: 272

Re: how to select only required obs when concatenating datassets

Posted in reply to PaigeMiller

Thank you for the support.

But I need to concatenate the datasets, not merging. Thank you

Trusted Advisor
Posts: 1,909

Re: how to select only required obs when concatenating datassets

Posted in reply to knveraraju91

But I need to concatenate the datasets, not merging. Thank you



 

In that case @novinosrin has provided the solution.

PROC Star
Posts: 283

Re: how to select only required obs when concatenating datassets

Posted in reply to knveraraju91

A silly solution, forgive me but fun:

 

data one;
input id date :date9.;
format date date9.;
datalines;
1 05OCT2015
2 06OCT2015
3 07OCT2015
;
data two;
input id date :date9.;
format date date9.;
datalines;
1 05DEC2015
2 06DEC2015
3 07DEC2015
4 08DEC2015
;
data temp;
merge one(in=a) two(in=b);
by id;
if a and b;
run;
data want;
set one temp;
run;

Respected Advisor
Posts: 3,799

Re: how to select only required obs when concatenating datassets

Posted in reply to knveraraju91
data three;
   set one(in=in1) two(in=in2);
   by id;
   if first.id and in2 then delete;
   format date date9.;
   run;

Capture.PNG

Super User
Posts: 10,020

Re: how to select only required obs when concatenating datassets

Posted in reply to knveraraju91
Assuming there are not duplicated ID in both tables.



data one;
input id date :date9.;
format date date9.;
datalines;
1 05OCT2015
2 06OCT2015
3 07OCT2015
;
data two;
input id date :date9.;
format date date9.;
datalines;
1 05DEC2015
2 06DEC2015
3 07DEC2015
4 08DEC2015
;
data want;
 set one two;
 by id;
 if first.id and last.id then delete;
run;


Ask a Question
Discussion stats
  • 6 replies
  • 174 views
  • 4 likes
  • 5 in conversation