<?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: Column that displays value from the previous row (per policy) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303726#M270474</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by policy_id;
want = lag(policy_version);
if first.policy_id then want=policy_version;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 11 Oct 2016 07:53:50 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2016-10-11T07:53:50Z</dc:date>
    <item>
      <title>Column that displays value from the previous row (per policy)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303725#M270473</link>
      <description>&lt;P&gt;&amp;nbsp;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a table that includes policies, with one row for each version of the policy. Each time a policy is changed, a new policy version is triggered. The policy versions are always in correct chronological order, however they&amp;nbsp;can skip numbers, going from for example 5 straight to 7, and doesn't always start at 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want a new column that always displays the previous policy version (per policy_id). When there is no previous policy&amp;nbsp;version, then the first policy version should be displayed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="280"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="80"&gt;POLICY_ID&lt;/TD&gt;
&lt;TD width="120"&gt;POLICY_VERSION&lt;/TD&gt;
&lt;TD width="80"&gt;WANT&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;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t;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would appreciate help with setting up this code. Thanks for your time!&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 07:48:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303725#M270473</guid>
      <dc:creator>EinarRoed</dc:creator>
      <dc:date>2016-10-11T07:48:30Z</dc:date>
    </item>
    <item>
      <title>Re: Column that displays value from the previous row (per policy)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303726#M270474</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by policy_id;
want = lag(policy_version);
if first.policy_id then want=policy_version;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Oct 2016 07:53:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303726#M270474</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-11T07:53:50Z</dc:date>
    </item>
    <item>
      <title>Re: Column that displays value from the previous row (per policy)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303727#M270475</link>
      <description>Aha there's a lag function, so practical! Thanks, it gives me exactly what I want.</description>
      <pubDate>Tue, 11 Oct 2016 08:02:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303727#M270475</guid>
      <dc:creator>EinarRoed</dc:creator>
      <dc:date>2016-10-11T08:02:16Z</dc:date>
    </item>
    <item>
      <title>Re: Column that displays value from the previous row (per policy)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303728#M270476</link>
      <description>&lt;P&gt;To expand on the lag() function:&lt;/P&gt;
&lt;P&gt;Every time it is called, it puts the current value of the named variable into it's FIFO queue, and yields the last value from that queue. If it is not called in a certain iteration of&amp;nbsp; the data step, this action is not performed, and the current value is lost (never gets into the queue).&lt;/P&gt;
&lt;P&gt;So it is usually a bad idea to call lag() in a conditional construct.&lt;/P&gt;
&lt;P&gt;That is why I first use lag() unconditionally and &lt;U&gt;then&lt;/U&gt; override with first.policy_id.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2016 08:09:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Column-that-displays-value-from-the-previous-row-per-policy/m-p/303728#M270476</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-11T08:09:05Z</dc:date>
    </item>
  </channel>
</rss>

