<?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: Anything wrong with the codes? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82923#M23790</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You are using LAG() with a conditon which usually is wrong.&lt;/P&gt;&lt;P&gt;You should split it into two different data step.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data x;
call streaminit(123);
do i=1 to 1000;
x=rand('normal', 0, 1);
output;
end;
run;

data want;
 set x;
y=lag(x)+x;
if i=1 then y=x;
run;



&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 28 Nov 2012 06:00:48 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2012-11-28T06:00:48Z</dc:date>
    <item>
      <title>Anything wrong with the codes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82922#M23789</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to generate one random walk sequence, but can't achieve it with the codes below. Anything wrong with the codes?&amp;nbsp; Any saser could help me out? Thanks in advance..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;call streaminit(123);&lt;/P&gt;&lt;P&gt;do i=1 to 1000;&lt;/P&gt;&lt;P&gt;x=rand('normal', 0, 1);&lt;/P&gt;&lt;P&gt;if i=1 then y=x;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; else y=lag(y)+x;&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Nov 2012 04:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82922#M23789</guid>
      <dc:creator>TomiKong</dc:creator>
      <dc:date>2012-11-28T04:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Anything wrong with the codes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82923#M23790</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You are using LAG() with a conditon which usually is wrong.&lt;/P&gt;&lt;P&gt;You should split it into two different data step.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data x;
call streaminit(123);
do i=1 to 1000;
x=rand('normal', 0, 1);
output;
end;
run;

data want;
 set x;
y=lag(x)+x;
if i=1 then y=x;
run;



&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Nov 2012 06:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82923#M23790</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2012-11-28T06:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: Anything wrong with the codes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82924#M23791</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A __default_attr="645292" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt;,&lt;/P&gt;&lt;P&gt; Your comment of using lag() conditionally is absolutely appropriate, and I think you are reading OP's mind correctly. However, just in case that OP wants to use lag() on the fly (y=lag(y)), and to avoid conditional lag() as well as 50% obs being missing,&amp;nbsp; we can try:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want1;&lt;/P&gt;&lt;P&gt;call streaminit(123);&lt;/P&gt;&lt;P&gt;do i=1 to 1000;&lt;/P&gt;&lt;P&gt;x=rand('normal', 0, 1);&lt;/P&gt;&lt;P&gt;y=ifn(i&amp;lt;3,x,lag(y)+x);&lt;/P&gt;&lt;P&gt;/*if i=1 then y=x;*/&lt;/P&gt;&lt;P&gt;/* else y=lag(y)+x;*/&lt;/P&gt;&lt;P&gt;output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Nov 2012 14:45:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82924#M23791</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2012-11-28T14:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: Anything wrong with the codes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82925#M23792</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Or RETAIN may be appropriate:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;data want; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;call streaminit(123); &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;retain y .; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;do i=1 to 1000; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;x=rand('normal', 0, 1); &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if i=1 then y=x; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;else y=y+x; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;output; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;end; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Nov 2012 15:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82925#M23792</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2012-11-28T15:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: Anything wrong with the codes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82926#M23793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, 'retain' is better, and it is&amp;nbsp; for sure worth introducing it to OP instead of trying squeezing lag() and making it more complex. Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Nov 2012 15:46:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82926#M23793</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2012-11-28T15:46:07Z</dc:date>
    </item>
    <item>
      <title>Re: Anything wrong with the codes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82927#M23794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I may be wrong here, but it seems to me that the easiest way to fix this is to remove the lag function since it is not needed.&amp;nbsp; Since the overall implicit data loop runs only once, won't the following code generate the desired random walk?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp; call streaminit(123);&lt;BR /&gt;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; do i=1 to 1000;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x=rand('normal', 0, 1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if i=1 then y=x;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else y=y+x;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Nov 2012 00:24:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Anything-wrong-with-the-codes/m-p/82927#M23794</guid>
      <dc:creator>LarryWorley</dc:creator>
      <dc:date>2012-11-29T00:24:33Z</dc:date>
    </item>
  </channel>
</rss>

