☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Obsidian | Level 7

## Loop to apply previous value to subsequent calculation

I have monthly datasets which contain counts. The probability from the previous month is used to calculate the probability for the subsequent month. I am trying to find a non-repetitive way to do this.

Here is an example:

``````data mnth1;
input beta x n mnth;
cards;
0 38 50 1
0.1 38 50 1
0.2 38 50 1
0.3 38 50 1
0.4 38 50 1
0.5 38 50 1
0.6 38 50 1
0.7 38 50 1
0.8 38 50 1
0.9 38 50 1
1.0 38 50 1
;
run;

data mnth2;
input beta x n mnth;
cards;
0 20 50 2
0.1 20 50 2
0.2 20 50 2
0.3 20 50 2
0.4 20 50 2
0.5 20 50 2
0.6 20 50 2
0.7 20 50 2
0.8 20 50 2
0.9 20 50 2
1.0 20 50 2
;
run;

data mnth3;
input beta x n mnth;
cards;
0 10 50 3
0.1 10 50 3
0.2 10 50 3
0.3 10 50 3
0.4 10 50 3
0.5 10 50 3
0.6 10 50 3
0.7 10 50 3
0.8 10 50 3
0.9 10 50 3
1.0 10 50 3
;
run;

*month 1;
data mnth1_post;
set mnth1;
prior=PDF('BETA', beta, 1, 1);
*likelihood based on binomial distribution;
lk=PDF('BINOMIAL', x, beta, N);
p1=prior*lk;
keep beta p1;
run;

*month 2;
data mnth2_post;
merge mnth2 mnth1_post;
by beta;
lk=PDF('BINOMIAL', x, beta, N);
p2 = p1*lk;
keep beta p2;
run;

*month 3;
data mnth3_post;
merge mnth3 mnth2_post;
by beta;
lk=PDF('BINOMIAL', x, beta, N);
p3 = p2*lk;
keep beta p3;
run;

``````

I would like to get the results without a separate data step for each month. I tried using a loop, but it produced all combinations for each month, rather than using the probability from the previous month for the subsequent month.

``````%macro pr;
%do i = 1 %to 3;
%do j = 2 %to 3;
data mnth&j._post;
merge mnth&j mnth&i._post;
by beta;
lk=PDF('BINOMIAL', x, beta, N);
p&j = p&i*lk;
keep beta p&j;
run;
%end;
%end;
%mend pr;
%pr;``````

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

## Re: Loop to apply previous value to subsequent calculation

You don't need macro coding for this.  You can reach all three records for each BETA level in succession, and calculate p1 from prior, p2 from p1, and p3 from p2.  Use the SET command to interleave the observations from each MNTH dataset:

``````data want (keep=beta p1 p2 p3);
do i=1 by 1 until (last.beta);
set mnth1 mnth2  mnth3  ;
by beta;
array prob {0:3} prior p1-p3;
lk=PDF('BINOMIAL', x, beta, N);
if i=1 then prior=PDF('BETA', beta, 1, 1);
prob{i}=prob{i-1}*lk;
end;
run;
``````

This program assumes each MNTH dataset has the same set of BETA values, and has one observation for each BETA.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
2 REPLIES 2
PROC Star

## Re: Loop to apply previous value to subsequent calculation

You don't need macro coding for this.  You can reach all three records for each BETA level in succession, and calculate p1 from prior, p2 from p1, and p3 from p2.  Use the SET command to interleave the observations from each MNTH dataset:

``````data want (keep=beta p1 p2 p3);
do i=1 by 1 until (last.beta);
set mnth1 mnth2  mnth3  ;
by beta;
array prob {0:3} prior p1-p3;
lk=PDF('BINOMIAL', x, beta, N);
if i=1 then prior=PDF('BETA', beta, 1, 1);
prob{i}=prob{i-1}*lk;
end;
run;
``````

This program assumes each MNTH dataset has the same set of BETA values, and has one observation for each BETA.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Obsidian | Level 7

## Re: Loop to apply previous value to subsequent calculation

Works great. Thanks!

Discussion stats
• 2 replies
• 1026 views
• 1 like
• 2 in conversation