<?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: Update information from previous row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722510#M224037</link>
    <description>&lt;P&gt;Your code would not give the updated age for 2nd and 3rd year:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;id&amp;nbsp; visit &amp;nbsp; age&amp;nbsp; gender &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;male&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;60&lt;/TD&gt;&lt;TD&gt;female&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;female&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;female&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;We need one more step to correct it:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set want;
if visit=2 then age=age+1;
if visit=3 then age=age+2;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Mon, 01 Mar 2021 00:32:38 GMT</pubDate>
    <dc:creator>Joey2</dc:creator>
    <dc:date>2021-03-01T00:32:38Z</dc:date>
    <item>
      <title>Update information from previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722466#M224008</link>
      <description>&lt;P&gt;I have a dataset with many patients first year visit information. Then I want to get some information from the first year data into the second and third year.&amp;nbsp; For example, for ID=1, then his age will be 51, 52 in the 2nd and 3rd year, while gender is still male.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id age visit gender $;
datalines;
1 50 1 male
1 .  2  .
1 .  3  .
2 60 1 female
2 .  2  .
2 .  3  .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;what I want&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;age&lt;/TD&gt;&lt;TD&gt;visit&lt;/TD&gt;&lt;TD&gt;gender&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;51&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;52&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;female&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;61&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;female&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Feb 2021 19:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722466#M224008</guid>
      <dc:creator>Joey2</dc:creator>
      <dc:date>2021-02-28T19:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: Update information from previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722469#M224010</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id age visit gender $;
datalines;
1 50 1 male
1 .  2  .
1 .  3  .
2 60 1 female
2 .  2  .
2 .  3  .
;
run;

data want(drop = a g);
   set have;
   by id;
   if first.id then do;
      g = gender;
      a = age;
   end;
   else a + 1;

   gender = g;
   age    = a;
   

   retain a g;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Feb 2021 19:31:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722469#M224010</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-02-28T19:31:44Z</dc:date>
    </item>
    <item>
      <title>Re: Update information from previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722474#M224014</link>
      <description>It works! Thank you.</description>
      <pubDate>Sun, 28 Feb 2021 20:01:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722474#M224014</guid>
      <dc:creator>Joey2</dc:creator>
      <dc:date>2021-02-28T20:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: Update information from previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722476#M224016</link>
      <description>&lt;P&gt;You can actually trick the UPDATE statement into doing this for you.&amp;nbsp; The purpose of the UPDATE statement is to allow you to apply a transaction dataset to an existing dataset.&amp;nbsp; When the variable is the transaction dataset is missing then the current value is not replaced.&lt;/P&gt;
&lt;P&gt;You have to have a BY group and normally the BY variables should uniquely identify the observations in the source dataset.&amp;nbsp; But you can have multiple transactions per BY group.&amp;nbsp; Your data already has a BY variable, ID, but it is not unique.&amp;nbsp; But if you just treat ALL of the observations as the transactions your can avoid that problem. So use the OBS=0 dataset option to start with an empty source dataset.&amp;nbsp; To output more than one observation per BY group just add an explicit OUTPUT statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update have(obs=0) have;
  by id;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Feb 2021 20:10:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722476#M224016</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-28T20:10:59Z</dc:date>
    </item>
    <item>
      <title>Re: Update information from previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722482#M224019</link>
      <description>&lt;P&gt;For future reference, consider a strategic combination of DROP=&amp;nbsp; and POINT=:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id age visit gender $;
datalines;
1 50 1 male
1 .  2  .
1 .  3  .
2 60 1 female
2 .  2  .
2 .  3  .
run;
data want;
  set have (drop=age gender);
  by id;
  if first.id then set have point=_n_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The benefit of this approach is that it avoids the need for an explicit RETAIN statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Feb 2021 20:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722482#M224019</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-02-28T20:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: Update information from previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722486#M224021</link>
      <description>&lt;P&gt;That trick will only work when the non-missing values only appear on the first observation in the BY group.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Feb 2021 20:50:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722486#M224021</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-02-28T20:50:10Z</dc:date>
    </item>
    <item>
      <title>Re: Update information from previous row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722510#M224037</link>
      <description>&lt;P&gt;Your code would not give the updated age for 2nd and 3rd year:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="branch"&gt;&lt;DIV&gt;&lt;DIV align="center"&gt;id&amp;nbsp; visit &amp;nbsp; age&amp;nbsp; gender &lt;TABLE cellspacing="0" cellpadding="5"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;male&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;50&lt;/TD&gt;&lt;TD&gt;male&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;60&lt;/TD&gt;&lt;TD&gt;female&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;female&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;female&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;We need one more step to correct it:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set want;
if visit=2 then age=age+1;
if visit=3 then age=age+2;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 01 Mar 2021 00:32:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Update-information-from-previous-row/m-p/722510#M224037</guid>
      <dc:creator>Joey2</dc:creator>
      <dc:date>2021-03-01T00:32:38Z</dc:date>
    </item>
  </channel>
</rss>

