BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
7 REPLIES 7
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
Cynthia_sas
SAS Super FREQ
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]
deleted_user
Not applicable
Thanks a lot Cynthia .This code work perfectly.
Regards,
Leena
data_null__
Jade | Level 19
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 end:) eq 3 then stop;
run;
proc print;
run;
[/pre]
deleted_user
Not applicable
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
deleted_user
Not applicable
Hi Abhi,
Your coding does not work.Can u explain how it works.

Thanks and regards,
Leena
deleted_user
Not applicable
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;

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
  • 7 replies
  • 941 views
  • 0 likes
  • 4 in conversation