<?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: Using Previous Observations for Current Visits by Group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692330#M210897</link>
    <description>&lt;P&gt;This is a good example for using the lag function embedded in an IFN function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by subjid;
  var1new=ifn(first.subjid,var1,lag(var1));
  var2new=ifn(first.subjid,var2,lag(var2));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because the lag function is inside an IFN function, it is always evaluated, even though it is not always selected based on the first argument of the ifn.&amp;nbsp; &amp;nbsp;That is a good thing, since you want the queue of values underlying the lag to always be synchronized with each new observation - i.e. the beginning of each new id get the lagged value from the end of the prior id.&amp;nbsp; But the IFN will ignore that instance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, I think you have an erroneous value for var1new in the 8th record.&amp;nbsp; You have 8, but I think it should be 6.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 18 Oct 2020 03:33:46 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-10-18T03:33:46Z</dc:date>
    <item>
      <title>Using Previous Observations for Current Visits by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692322#M210891</link>
      <description>&lt;P&gt;I have dataset&amp;nbsp; by visit in which I am trying to use the observations for the previous record for the current visit. Below are example&amp;nbsp; data (HAVE) for three subid. When correctly done it should look like data WANT. That is, the records for visit=0 is carried over to visit=1; the record for visit=1 is carried over to visit=2, etc. Because the data for&amp;nbsp; visit=0 will be missing when carried over to the visit=1, the data for visit=0 will retain its value and it is also used for visit=1. The last record within each subjid will vanish.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is needed for all variables in the dataset. The LAG function is not giving the correct output as the LAG function is not pushing the observations by subjid.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input subjid $ vis var1 var2;&lt;BR /&gt;datalines;&lt;BR /&gt;001-001 0 4 5&lt;BR /&gt;001-001 1 3 6&lt;BR /&gt;001-001 2 2 9&lt;BR /&gt;001-001 3 5 7&lt;/P&gt;
&lt;P&gt;001-002 0 5 7&lt;BR /&gt;001-002 1 8 9&lt;BR /&gt;001-002 2 6 2&lt;BR /&gt;001-002 3 9 4&lt;/P&gt;
&lt;P&gt;001-003 0 2 7&lt;BR /&gt;001-003 1 6 9&lt;BR /&gt;001-003 2 5 2&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input subjid $ vis var1 var2 var1new var2new;&lt;BR /&gt;datalines;&lt;BR /&gt;001-001 0 4 5 4 5&lt;BR /&gt;001-001 1 3 6 4 5&lt;BR /&gt;001-001 2 2 9 3 6&lt;BR /&gt;001-001 3 5 7 2 9&lt;/P&gt;
&lt;P&gt;001-002 0 5 7 5 7&lt;BR /&gt;001-002 1 8 9 5 7&lt;BR /&gt;001-002 2 6 2 8 9&lt;BR /&gt;001-002 3 9 4 8 2&lt;/P&gt;
&lt;P&gt;001-003 0 2 7 2 7&lt;BR /&gt;001-003 1 6 9 2 7&lt;BR /&gt;001-003 2 5 2 6 9&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Oct 2020 22:13:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692322#M210891</guid>
      <dc:creator>SWEETSAS</dc:creator>
      <dc:date>2020-10-17T22:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: Using Previous Observations for Current Visits by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692323#M210892</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* UNTESTED CODE */
data want;
    set have;
    by subjid;
    prev_var1=lag(var1);
    prev_var2=lag(var2);
    if first.subjid then do;
        var1new=var1;
        var2new=var2;
    end;
    else do;
        var1new=prev_var1;
        var2new=prev_var2;
    end;
    drop prev_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As a comment, I think a better naming scheme is varnew1 and varnew2 or newvar1 and newvar2. When you use this naming scheme, you can easily refer to all of them via a list or via varnew: or newvar: and if you leave the naming scheme as you stated it, then you have to type out the variable names, you can't use a list or the colon to refer to them.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Oct 2020 22:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692323#M210892</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-17T22:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using Previous Observations for Current Visits by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692330#M210897</link>
      <description>&lt;P&gt;This is a good example for using the lag function embedded in an IFN function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by subjid;
  var1new=ifn(first.subjid,var1,lag(var1));
  var2new=ifn(first.subjid,var2,lag(var2));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because the lag function is inside an IFN function, it is always evaluated, even though it is not always selected based on the first argument of the ifn.&amp;nbsp; &amp;nbsp;That is a good thing, since you want the queue of values underlying the lag to always be synchronized with each new observation - i.e. the beginning of each new id get the lagged value from the end of the prior id.&amp;nbsp; But the IFN will ignore that instance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, I think you have an erroneous value for var1new in the 8th record.&amp;nbsp; You have 8, but I think it should be 6.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 18 Oct 2020 03:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692330#M210897</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-10-18T03:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using Previous Observations for Current Visits by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692514#M210992</link>
      <description>&lt;P&gt;Thanks a million &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 13:23:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692514#M210992</guid>
      <dc:creator>SWEETSAS</dc:creator>
      <dc:date>2020-10-19T13:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using Previous Observations for Current Visits by Group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692516#M210993</link>
      <description>&lt;P&gt;Many thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 13:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Previous-Observations-for-Current-Visits-by-Group/m-p/692516#M210993</guid>
      <dc:creator>SWEETSAS</dc:creator>
      <dc:date>2020-10-19T13:24:40Z</dc:date>
    </item>
  </channel>
</rss>

