BookmarkSubscribeRSS Feed
Rockers
Calcite | Level 5
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
5 REPLIES 5
DF
Fluorite | Level 6 DF
Fluorite | Level 6
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
Ksharp
Super User
How about:
[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
Rockers
Calcite | Level 5
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...
Patrick
Opal | Level 21
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
Ksharp
Super User
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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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