<?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: Use previous row value of one variable for a new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/390812#M93798</link>
    <description>my apologies! i got the code wrong. chris's code is indeed correct.</description>
    <pubDate>Fri, 25 Aug 2017 03:34:47 GMT</pubDate>
    <dc:creator>eemrun</dc:creator>
    <dc:date>2017-08-25T03:34:47Z</dc:date>
    <item>
      <title>Use previous row value of one variable for a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/389443#M93373</link>
      <description>&lt;P&gt;I have a table where I need a new variable (P_Current) that takes value of the previous value of variable (Current)&amp;nbsp;. The following highlights what I am looking for:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Current&lt;/TD&gt;&lt;TD&gt;P_Current&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was trying the following but thats probably not correct&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
set test1;
by ID;
P_Current = lag(Current);
if first.ID then P_Current = Current;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2017 03:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/389443#M93373</guid>
      <dc:creator>eemrun</dc:creator>
      <dc:date>2017-08-21T03:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous row value of one variable for a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/389444#M93374</link>
      <description>&lt;P&gt;Your output seems to macth this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test2;
set test1;
P_Current = lag(Current);
if _N_=1 then P_Current = Current;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Aug 2017 02:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/389444#M93374</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-08-21T02:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous row value of one variable for a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/389445#M93375</link>
      <description>Thanks Chris. However, that code does not seem to be working. I have slightly edited the original post table. Ideally I am looking to populate the P_Current field with the last Current value (i.e. value from the previous row). With the code you suggested, the P_Current is populated with all 1s. Any idea where I might be going wrong?</description>
      <pubDate>Mon, 21 Aug 2017 03:06:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/389445#M93375</guid>
      <dc:creator>eemrun</dc:creator>
      <dc:date>2017-08-21T03:06:44Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous row value of one variable for a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/389446#M93376</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;'s code is working:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
input ID Current;
datalines;
1 1
2 0
2 1
3 0
4 0
5 1
6 1
;

data test2;
set test1;
P_Current = lag(Current);
if _N_=1 then P_Current = Current;
run;

proc print data=test2 noobs; run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                            ID    Current    P_Current

                              1       1           1
                              2       0           1
                              2       1           0
                              3       0           1
                              4       0           0
                              5       1           0
                              6       1           1
&lt;/PRE&gt;
&lt;P&gt;Please post the code you tried.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Aug 2017 04:07:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/389446#M93376</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-08-21T04:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Use previous row value of one variable for a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/390812#M93798</link>
      <description>my apologies! i got the code wrong. chris's code is indeed correct.</description>
      <pubDate>Fri, 25 Aug 2017 03:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-previous-row-value-of-one-variable-for-a-new-variable/m-p/390812#M93798</guid>
      <dc:creator>eemrun</dc:creator>
      <dc:date>2017-08-25T03:34:47Z</dc:date>
    </item>
  </channel>
</rss>

