<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Loop to apply previous value to subsequent calculation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-to-apply-previous-value-to-subsequent-calculation/m-p/826018#M326272</link>
    <description>&lt;P&gt;Works great. Thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 28 Jul 2022 17:57:10 GMT</pubDate>
    <dc:creator>Caetreviop543</dc:creator>
    <dc:date>2022-07-28T17:57:10Z</dc:date>
    <item>
      <title>Loop to apply previous value to subsequent calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-to-apply-previous-value-to-subsequent-calculation/m-p/825993#M326259</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro pr;
%do i = 1 %to 3;
%do j = 2 %to 3;
data mnth&amp;amp;j._post;
merge mnth&amp;amp;j mnth&amp;amp;i._post;
by beta;
lk=PDF('BINOMIAL', x, beta, N);
p&amp;amp;j = p&amp;amp;i*lk;
keep beta p&amp;amp;j;
run;
%end;
%end;
%mend pr;
%pr;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 16:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-to-apply-previous-value-to-subsequent-calculation/m-p/825993#M326259</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2022-07-28T16:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Loop to apply previous value to subsequent calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-to-apply-previous-value-to-subsequent-calculation/m-p/826014#M326269</link>
      <description>&lt;P&gt;You don't need macro coding for this.&amp;nbsp; You can reach all three records for each BETA level in succession, and calculate p1 from prior, p2 from p1, and p3 from p2.&amp;nbsp; Use the SET command to interleave the observations from each MNTH dataset:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program assumes each MNTH dataset has the same set of BETA values, and has one observation for each BETA.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 17:27:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-to-apply-previous-value-to-subsequent-calculation/m-p/826014#M326269</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-28T17:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: Loop to apply previous value to subsequent calculation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-to-apply-previous-value-to-subsequent-calculation/m-p/826018#M326272</link>
      <description>&lt;P&gt;Works great. Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 17:57:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-to-apply-previous-value-to-subsequent-calculation/m-p/826018#M326272</guid>
      <dc:creator>Caetreviop543</dc:creator>
      <dc:date>2022-07-28T17:57:10Z</dc:date>
    </item>
  </channel>
</rss>

