DATA Step, Macro, Functions and more

combine two dataset

Reply
N/A
Posts: 0

combine two dataset

Hi
I have two dataset. A and B. combine these 2 dataset and create another dataset that is C.
A contain
a
b
c


B contains
p
q
r

I have to create another dataset combine these two so the result C dataset is
a
p
b
q
c
r
Please tell me how I create C datasets.
thanks,
regards,
Leena
Super Contributor
Super Contributor
Posts: 3,174

Re: combine two dataset

Posted in reply to deleted_user
A DATA set with a SET statement referencing your two files, however it appears you want to interleave individual observations from each of the two files.

So, consider setting a temporary SAS variable in your code, such as OBSNUM = _N_; and then use that variable in your BY stmt to get your obs in the right sequence.

Scott Barry
SBBWorks, Inc.
SAS Super FREQ
Posts: 8,862

Re: combine two dataset

Hi:
The documentation on interleaving is here:
http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001318366.htm

A simplified example from the doc, using Scott's suggestion for some kind of OBSNUM variable is shown below (I called the variable ROWVAR). Note that in my "fake" data, I have put more rows in data set B. Using the ROWVAR ensures that the DATA step keeps reading until all the BY variables have been accounted for in either file.

cynthia
[pre]
data a;
infile datalines;
input var1 $;
rowvar = _n_;
return;
datalines;
a
b
c
;
run;


data b;
infile datalines;
input var1 $;
rowvar = _n_;
return;
datalines;
p
q
r
x
y
z
;
run;

data c;
set a b; by rowvar;
run;

ods listing;
proc print data=c;
run;

[/pre]
N/A
Posts: 0

Re: combine two dataset

Posted in reply to Cynthia_sas
Thanks a lot Cynthia .This code work perfectly.
Regards,
Leena
Respected Advisor
Posts: 3,799

Re: combine two dataset

Posted in reply to deleted_user
You don't need to create new variables as was suggested but you do need to keep the data step from stopping prematurely if one of the data sets has fewer records than the other. This example weaves 3 data set of different size.

[pre]
data One Two Three;
do _n_ = 1 to 50;
y = rantbl(1,.3,.4);
select(y);
when(1) output One;
when(2) output Two;
when(3) output Three;
end;
end;
run;
data weave;
if not end1 then do;
set One end=end1;
output;
end;
if not end2 then do;
set Two end=end2;
output;
end;
if not end3 then do;
set Three end=end3;
output;
end;
if sum(of endSmiley Happy eq 3 then stop;
run;
proc print;
run;
[/pre]
N/A
Posts: 0

Re: combine two dataset

Posted in reply to deleted_user
Try this

data temp (keep = var count);
set a (in = x) b (in = z);
retain aa bb;
if x then do; aa + 1; count = aa; end;
if z then do; bb + 1; count = bb; end;
run;

proc sort data = temp out = c (keep = var);
by count var;
run;

proc print data = c;
run;


cheers,
Abhishek
N/A
Posts: 0

Re: combine two dataset

Posted in reply to deleted_user
Hi Abhi,
Your coding does not work.Can u explain how it works.

Thanks and regards,
Leena
N/A
Posts: 0

Re: combine two dataset

Posted in reply to deleted_user
data a;
var = 'a'; output; var = 'b'; output; var = 'c'; output;
run;

data b;
var = 'p'; output; var = 'q'; output; var = 'r'; output;
run;

data temp (keep = var count);
set a (in = x) b (in = z);
retain aa bb;
if x then do; aa + 1; count = aa; end;
if z then do; bb + 1; count = bb; end;
run;

proc sort data = temp out = c (keep = var);
by count var;
run;

proc print data = c;
run;
Ask a Question
Discussion stats
  • 7 replies
  • 235 views
  • 0 likes
  • 4 in conversation