<?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: using lag function in a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335836#M272370</link>
    <description>&lt;P&gt;Thanks for your reply!&lt;/P&gt;&lt;P&gt;The input data:&lt;/P&gt;&lt;P&gt;brand_lastindicator&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;My desired output data(which can be realized by code them one by one)&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/7462i32ADAC72ECA0E085/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="lag 1.png" title="lag 1.png" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if No=3 and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);
lag_brandloy(i) = lag(brandloy(i));
if No=3 and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);
lag_brandloy(i) = lag(brandloy(i));
if No=4 and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);
lag_brandloy(i) = lag(brandloy(i));
if No=5 and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);&lt;/PRE&gt;&lt;P&gt;I want to do the same thing from No=2 to 160.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 25 Feb 2017 08:22:42 GMT</pubDate>
    <dc:creator>robynn</dc:creator>
    <dc:date>2017-02-25T08:22:42Z</dc:date>
    <item>
      <title>using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335827#M272367</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am using a do loop to create lag variable. The point is I have to use the last observation's data to compute the lagged variable for the next observation.&amp;nbsp;I try the &amp;nbsp;following:&lt;/P&gt;&lt;PRE&gt;data data.coffee;
set data.coffee;
by PANID;
if first.PANID then NO = 0;
NO + 1;
array brandloy(6) brandloy1 - brandloy6;
array lag_brandloy(6) lag_brandloy1 - lag_brandloy6;
do i = 1 to 6;
if NO = 1 then brandloy(i) = 0.04;
if NO = 1 and brandID = i then brandloy(i) = 0.8;
do k = 2 to 160;
lag_brandloy(i) = lag(brandloy(i));
if NO = k and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);
end;
end;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the result is not what I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/7461i964D539DA5A1B694/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="lag.png" title="lag.png" /&gt;&lt;/P&gt;&lt;P&gt;The lag does not work here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I do it one by one like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if NO = 2&amp;nbsp;then brandloy(i) = 0.8*lag_brandloy(i) + 0.2*brand_lastindicator;
lag_brandloy(i) = lag(brandloy(i));
if NO = 3 then brandloy(i) = 0.8*lag_brandloy(i) + 0.2*brand_lastindicator;
lag_brandloy(i) = lag(brandloy(i));
if NO = 4 then brandloy(i) = 0.8*lag_brandloy(i) + 0.2*brand_lastindicator;
lag_brandloy(i) = lag(brandloy(i));&lt;/PRE&gt;&lt;P&gt;the result is correct. So I wonder why the loop code does not work. And any suggestions about what I should do?&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2017 07:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335827#M272367</guid>
      <dc:creator>robynn</dc:creator>
      <dc:date>2017-02-25T07:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335831#M272368</link>
      <description>&lt;P&gt;You have shown output data that you don't want. But you haven't shown (1) sampel input data, (2) output data you DO want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Make your objectives clear -&amp;nbsp;show the input data, and desired output data.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2017 08:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335831#M272368</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-25T08:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335836#M272370</link>
      <description>&lt;P&gt;Thanks for your reply!&lt;/P&gt;&lt;P&gt;The input data:&lt;/P&gt;&lt;P&gt;brand_lastindicator&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;0&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;My desired output data(which can be realized by code them one by one)&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/7462i32ADAC72ECA0E085/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="lag 1.png" title="lag 1.png" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;if No=3 and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);
lag_brandloy(i) = lag(brandloy(i));
if No=3 and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);
lag_brandloy(i) = lag(brandloy(i));
if No=4 and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);
lag_brandloy(i) = lag(brandloy(i));
if No=5 and brandloy(i)= . then brandloy(i) = sum(0.8*lag_brandloy(i),0.2*brand_lastindicator);&lt;/PRE&gt;&lt;P&gt;I want to do the same thing from No=2 to 160.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2017 08:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335836#M272370</guid>
      <dc:creator>robynn</dc:creator>
      <dc:date>2017-02-25T08:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335840#M272371</link>
      <description>&lt;P&gt;I do not see the variables PANID, BRANDLOY1, ... BRANDLOY6 in your sample input data.&amp;nbsp; Can you provide some actual data?&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2017 08:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335840#M272371</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-25T08:33:16Z</dc:date>
    </item>
    <item>
      <title>Re: using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335841#M272372</link>
      <description>&lt;P&gt;Here is a data sample&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2017 08:41:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335841#M272372</guid>
      <dc:creator>robynn</dc:creator>
      <dc:date>2017-02-25T08:41:01Z</dc:date>
    </item>
    <item>
      <title>Re: using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335842#M272373</link>
      <description>&lt;P&gt;I don't have the brandloy1-brandloy6 at first, I need to compute them.&lt;/P&gt;&lt;P&gt;For each PANID, I have to compute the brandloy(i) &amp;nbsp;for the first observation, then the second and so on.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2017 08:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335842#M272373</guid>
      <dc:creator>robynn</dc:creator>
      <dc:date>2017-02-25T08:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335850#M272374</link>
      <description>&lt;P&gt;As you have seen, the LAG function is more complex than it looks. &amp;nbsp;For your application, I would recommend skipping it entirely. &amp;nbsp;You can still get the results you want through this combination of steps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(a) compute the lagged values before computing the unlagged values&lt;/P&gt;
&lt;P&gt;(b) retain the unlagged values&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data data.coffee;&lt;/P&gt;
&lt;P&gt;set data.coffee;&lt;/P&gt;
&lt;P&gt;by panid;&lt;/P&gt;
&lt;P&gt;if first.PANID then NO=0;&lt;/P&gt;
&lt;P&gt;NO + 1;&lt;/P&gt;
&lt;P&gt;array brandloy (6) brandloy1 - brandloy6;&lt;/P&gt;
&lt;P&gt;array lag_brandloy (6) lag_brandloy1-lag_brandloy_6;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So far, no changes. &amp;nbsp;But now the idea is this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;retain brandloy1 - brandloy6;&lt;/P&gt;
&lt;P&gt;if first.panid=0 then do i=1 to 6;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;lag_brandloy(i) = brandloy(i);&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;do i=1 to 6;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;** Compute only the brandloy&amp;nbsp;values, not the lag_brandloy values;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You still need to fill &amp;nbsp;in the details ... computing brandloy values, when to compute lag_brandloy values, etc. &amp;nbsp;But this approach will get around the complexities of the LAG function.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2017 12:11:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335850#M272374</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-02-25T12:11:55Z</dc:date>
    </item>
    <item>
      <title>Re: using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335854#M272375</link>
      <description>&lt;P&gt;The way the LAG&amp;lt;n&amp;gt;() functions work you need to have a separate statement for each stack of lagged values you want to create.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input x @@ ;
  x_lag1 = lag1(x);
  x_lag2 = lag2(x);
  x_lag3 = lag3(x);
  x_lag4 = lag4(x);
  x_lag5 = lag5(x);
  x_lag6 = lag6(x);
cards;
0 1 0 0 1 1 0 1 0 1
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could generate this repetitive code using a macro if you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro genlag(var,n);
%local i;
%do i=1 %to &amp;amp;n ;
&amp;amp;var._lag&amp;amp;i = lag&amp;amp;i(&amp;amp;var);
%end;
%mend ;

data want ;
  set have ;
  %genlag(x,6);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 25 Feb 2017 12:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335854#M272375</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-02-25T12:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: using lag function in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335855#M272376</link>
      <description>Many thanks!!</description>
      <pubDate>Sat, 25 Feb 2017 13:00:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-lag-function-in-a-do-loop/m-p/335855#M272376</guid>
      <dc:creator>robynn</dc:creator>
      <dc:date>2017-02-25T13:00:42Z</dc:date>
    </item>
  </channel>
</rss>

