<?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: where is the mistake in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435745#M108279</link>
    <description>&lt;P&gt;Post your entire data step. The LAG and DIF functions (&amp;nbsp;val2 = (x - lag1(x)); is the same as val2 = DIF1(x);) have separate results depending on if you use any conditional statements such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if somevar &amp;gt; 10 then &amp;nbsp;val2 = (x - lag1(x));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which lag only looks at the last record when the condition was true.&lt;/P&gt;
&lt;P&gt;So when "if" is used likely you need to create temporary variables to hold the lagged values and use those such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;L1x= lag(x);&lt;/P&gt;
&lt;P&gt;L2x= lag(x);&lt;/P&gt;
&lt;P&gt;if somevar &amp;gt; 10 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var2 = x- L1x;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var3 = L1x-L2X;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop L1x L2x:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Feb 2018 17:55:53 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-02-09T17:55:53Z</dc:date>
    <item>
      <title>where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435732#M108271</link>
      <description>&lt;P&gt;&lt;SPAN class="short_text"&gt;&lt;SPAN class=""&gt;simple code&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val2 = (x - lag1(x));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val3 = (lag1(x) - lag2(x));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if val2 = val3 then val1 = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;why once the code works (val1 = 0&amp;nbsp;&amp;nbsp; (-0,004 = -0,004)) and once (val1 =. (-0,00025 = -0,00025))&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN class="short_text"&gt;I do not understand and I can not fix it&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN class="short_text"&gt;thank you in advance for your help&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 17:45:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435732#M108271</guid>
      <dc:creator>makset7</dc:creator>
      <dc:date>2018-02-09T17:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435738#M108275</link>
      <description>&lt;P&gt;I'm not typing out data from images.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please produce a reproducible example of the issue you're talking about, including sample data, your full code and log?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create sample data using the techniques here:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or a simple data step to read the values in your word doc will suffice.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The usual issue people run into with LAG is not realizing it's a queue not really the last value. And using conditionally with an IF statement but I don't see any of that in your code.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51900"&gt;@makset7&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN class="short_text"&gt;&lt;SPAN class=""&gt;simple code&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val2 = (x - lag1(x));&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val3 = (lag1(x) - lag2(x));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if val2 = val3 then val1 = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;why once the code works (val1 = 0&amp;nbsp;&amp;nbsp; (-0,004 = -0,004)) and once (val1 =. (-0,00025 = -0,00025))&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN class="short_text"&gt;I do not understand and I can not fix it&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=""&gt;&lt;SPAN class="short_text"&gt;thank you in advance for your help&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 17:50:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435738#M108275</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-09T17:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435745#M108279</link>
      <description>&lt;P&gt;Post your entire data step. The LAG and DIF functions (&amp;nbsp;val2 = (x - lag1(x)); is the same as val2 = DIF1(x);) have separate results depending on if you use any conditional statements such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if somevar &amp;gt; 10 then &amp;nbsp;val2 = (x - lag1(x));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which lag only looks at the last record when the condition was true.&lt;/P&gt;
&lt;P&gt;So when "if" is used likely you need to create temporary variables to hold the lagged values and use those such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;L1x= lag(x);&lt;/P&gt;
&lt;P&gt;L2x= lag(x);&lt;/P&gt;
&lt;P&gt;if somevar &amp;gt; 10 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var2 = x- L1x;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var3 = L1x-L2X;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop L1x L2x:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 17:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435745#M108279</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-02-09T17:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435773#M108288</link>
      <description>&lt;P&gt;example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="short_text"&gt;&lt;SPAN class=""&gt;change&lt;/SPAN&gt;&lt;/SPAN&gt; example.doc to example.sas7bdat&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 18:23:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435773#M108288</guid>
      <dc:creator>makset7</dc:creator>
      <dc:date>2018-02-09T18:23:55Z</dc:date>
    </item>
    <item>
      <title>Re: where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435784#M108290</link>
      <description>&lt;P&gt;Ok, it works as expected to me. What are you expecting that you're not achieving with this code?&lt;/P&gt;
&lt;P&gt;What, exactly, is your desired output from this input data set?&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 18:36:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435784#M108290</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-09T18:36:01Z</dc:date>
    </item>
    <item>
      <title>Re: where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435795#M108291</link>
      <description>&lt;P&gt;if val2 = val3 then val1 = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if _n_ = 3&amp;nbsp; val1 = 0;&lt;/P&gt;
&lt;P&gt;if _n_ = 6&amp;nbsp; val1 = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="short_text"&gt;&lt;SPAN class=""&gt;why&lt;/SPAN&gt;&lt;/SPAN&gt;?????????&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 18:51:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435795#M108291</guid>
      <dc:creator>makset7</dc:creator>
      <dc:date>2018-02-09T18:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435796#M108292</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51900"&gt;@makset7&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="short_text"&gt;&lt;SPAN&gt;change&lt;/SPAN&gt;&lt;/SPAN&gt; example.doc to example.sas7bdat&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Binary SAS data sets are not always useable by everyone due to language or character encoding, version (SAS9.2 can have issues with 9.4 sets) and sometime operating system. The data step code will create a version anyone can use.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And you still haven't show the entire code.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 18:51:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435796#M108292</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-02-09T18:51:53Z</dc:date>
    </item>
    <item>
      <title>Re: where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435800#M108294</link>
      <description>&lt;P&gt;data example;&lt;/P&gt;
&lt;P&gt;set example; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;val2 = (x - lag1(x));&lt;BR /&gt;val3 = (lag1(x) - lag2(x));&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example;&lt;/P&gt;
&lt;P&gt;set example; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if val2 = val3 then val1 = 0;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 18:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435800#M108294</guid>
      <dc:creator>makset7</dc:creator>
      <dc:date>2018-02-09T18:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: where is the mistake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435805#M108297</link>
      <description>&lt;P&gt;Numerical precision. It's a think with how computers stores numbers.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Round the values to whatever decimal point you want to use before your comparison using the ROUND function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if round(val2, 0.00001) = round(val3, 0.00001) then ...&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="http://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;http://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS this is an issue in most software packages, everyone encounters it at some point &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Feb 2018 19:03:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/where-is-the-mistake/m-p/435805#M108297</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-09T19:03:25Z</dc:date>
    </item>
  </channel>
</rss>

