BookmarkSubscribeRSS Feed
Lani
Calcite | Level 5
Hi all,

I have two data sets A and B.
Data set A (15 obs):
name
---------
aaa
bbb
ccc
...
zzz (obs #15)

Data set B (10 obs):
id_nbr
-------------
id-num1
id-num2
id-num3
....
id-num10 (obs #10)

I merge the above two data sets so that my new data set will get the var id_nbr in B to be assigned for each obs in A begin with obs#1 till the last obs in B and go back to obs#1, like below:

Data set New (15 obs):
obs# --------- id_nbr ---------- name -------- recno
1 --------- id-num1 ------- aaa ----------- 1
2 --------- id-num2 ------- bbb ----------- 2
3 --------- id-num3 ------- ccc ----------- 3
4
5
6
7
8
9
10 --------- id-num10 ------- mmm -------- 10
11 --------- id-num1 ------- nnn ----------- 1
12
13
14
15 --------- id-num5 ------- zzz ----------- 5

This is the code for the above:
data new;
retain recno;
set A;
if _n_=1 or recno=10 then recno=1;
else recno+1;
run;

Now I need your help.
The data set B should be used for each next run. For this run, I will have a data set named A2 that has 7 obs. I need to do like the above steps and this time I should start with what has been left in the data set B from the first run.

Data set A2 (7 obs):
name
-----------
name1
name2
name3
name4
name5
name6
name7

After I am going to merge data set A2 and B, I should have:
Data set New2 (7 obs):
obs --------- id_nbr ----------- name ----------- recno
1 --------- id-num6 ------- name1 ---------- 6
2 --------- id-num7 ------- name2 ---------- 7
3 --------- id-num8 ------- name3 ---------- 8
4 --------- id-num9 ------- name4 ---------- 9
5 --------- id-num10 ------- name5 ----------10
6 --------- id-num1 ------- name6 ---------- 1
7 --------- id-num2 ------- name7 ---------- 2

If I run again with another data set A3, it will begin with obs#3 to obs#10 (from data set B). Please give me an idea how to this.

Thanks so much.
Lani
2 REPLIES 2
rusyba
Calcite | Level 5
Lani,

maybe can create a macro variable to the end of the merge, which will be the number last used row. And then to use such a construction:
data firstobs = &macro_variable;
...
run;

Yuri.
Patrick
Opal | Level 21
HTH
Patrick


%macro IterOverDs(IterDS=,OneTimeDs=);

%if not %SYMGLOBL(LastIterObs) %then
%do;
%global LastIterObs;
%let LastIterObs=0;
%end;

%global newds;
%let newds=new_&OneTimeDs;

data new_&OneTimeDs;
set &OneTimeDs end=last;

if _n_=1 then
CurrentIterObs=&LastIterObs+1;
else
if mod(CurrentIterObs,NIterObs)=0 then
do;
CurrentIterObs=1;
end;
else
CurrentIterObs+1;

recno=CurrentIterObs;

set &IterDS nobs=NIterObs point=CurrentIterObs;

if last then
do;
if CurrentIterObs=NIterObs then call symput('LastIterObs','0');
else call symput('LastIterObs',cats(CurrentIterObs));
end;
run;

%mend;

%let LastIterObs=0;
data iter;
do iter_var=0 to 90 by 10;
output;
end;
run;

data a1;
do var_a1=0 to -15 by-1;
output;
end;
run;

%IterOverDs(IterDS=iter,OneTimeDs=a1);

title "DS is &newds";
proc print data=&newds;
run;



data a2;
do var_a1=1 to 7;
output;
end;
run;

%IterOverDs(IterDS=iter,OneTimeDs=a2);

title "DS is &newds";
proc print data=&newds;
run;


/* and so on and so on */

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
  • 2 replies
  • 1121 views
  • 0 likes
  • 3 in conversation