New Contributor
Posts: 4

# Help in Appending two dataset

Hi i have two dataset. dataset a and dataset b. suppose dataset a has 10 records and data set b has 3 records.. I want to append record from dataset b after every 3 records of dataset a.

data A;
do i = 1 to 10;
c=5*i;
output;
end;
run;

data B;
c=25;i=66;output;
c=75;i=99;output;
run;
I tired following:
data temp;
set a;
output;
do i = 3 to 10 by 3;
if _n_ =i then do;
set b;
output;
end;
end;
run;

My issue in the above code is sas iteration depends on the number of records of smallest dataset. hw to over above issue.. any 1 with help plz
Frequent Contributor
Posts: 94

## Re: Help in Appending two dataset

Maybe not the most ideal way, but what about:

[pre]
data A;
do i = 1 to 10;
c=5*i;
n + (mod(i-1,3)=0);
output;
end;
run;

data B;
c=25;i=66;n+1;output;
c=75;i=99;n+1;output;
run;

data C;
set A B;
by n;
run;
[/pre]

Edit: fixed to work after 3 rows of table A, instead of 2. Message was edited by: DF
Super User
Posts: 10,784

## Re: Help in Appending two dataset

[pre]

data A;
do i = 1 to 10;
c=5*i;
output;
end;
run;

data B;
c=25;i=66;output;
c=75;i=99;output;
run;

data want;
set a;
output;
if mod(_n_,3)=0 then do;
do k=1 to _nobs;
set b nobs=_nobs point=k;output;
end;
end;
run;
[/pre]

Ksharp
New Contributor
Posts: 4

## Re: Help in Appending two dataset

Thanks DF and Ksharp....but I was trying to do that in a single data step.. however in the code suggested by sharp.. al the records are getting appended each time...that is if data set A has 10 records and dataset B has 2 records.. instead of having 12 records in Want dataset i am getting 16 records...
Posts: 4,736

## Re: Help in Appending two dataset

Hi

Ksharp's approach which the little fix needed:

data A;
do i = 1 to 10;
c=5*i;
output;
end;
run;

data B;
c=25;i=66;output;
c=75;i=99;output;
run;

data want;
set a;
output;

if mod(_n_,3)=0 then
do;
ind+1;
if ind<=nobs then
set b point=ind nobs=nobs;
output;
end;
run;

proc print data=want;
run;

HTH
Patrick
Super User
Posts: 10,784

## Re: Help in Appending two dataset

Patrick give a better code.Thanks Patrick!
Since you did not post output you need, so it is hard to code exactly.
You want append only one record after every three records in A?

[pre]
data A;
do i = 1 to 10;
c=5*i;
output;
end;
run;

data B;
c=25;i=66;output;
c=75;i=99;output;
run;

data want;
set a;
output;
if mod(_n_,3)=0 then do;
count+1;
if count le _nobs then do;
set b point=count nobs=_nobs;
output;
end;
end;
run;
proc print;run;
[/pre]

Ksharp Message was edited by: Ksharp
Discussion stats
• 5 replies
• 391 views
• 0 likes
• 4 in conversation