<?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: Creating new data set with variables from most recent visit date in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398908#M66596</link>
    <description>&lt;P&gt;Sort by patient and date.&lt;/P&gt;
&lt;P&gt;In a data step:&lt;/P&gt;
&lt;P&gt;Retain new variables for each value. Everytime a valid value is encountered, set the respective retained variable.&lt;/P&gt;
&lt;P&gt;At last.patient, output.&lt;/P&gt;
&lt;P&gt;Drop the original variables.&lt;/P&gt;</description>
    <pubDate>Tue, 26 Sep 2017 18:51:45 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2017-09-26T18:51:45Z</dc:date>
    <item>
      <title>Creating new data set with variables from most recent visit date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398907#M66595</link>
      <description>&lt;P&gt;For a project we received a data set containing biomarker measurements from a number of physician visits for each subject. Not all variables of interest are present at each visit and we were wanting to create a new data set containing the values from the most recent visit that measure was taken at. I'm not sure how or if it is possible to set this up.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To give a bit more detail about what I would like to do, for each subject I would like to find a way to check if a measure has a value from the most recent visit date (i.e. 25AUG2015). if there is one keep that value(BMI=30) in the new data and stop searching for that variable in that subject. If not (BMI=.) go back to the next most recent date (16AUG105) and check there and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not real sure where to start on this issue so any help would be appreciated. Please let me know if I can clarify more and thank you all in advance.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2017 18:43:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398907#M66595</guid>
      <dc:creator>jarnaez</dc:creator>
      <dc:date>2017-09-26T18:43:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new data set with variables from most recent visit date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398908#M66596</link>
      <description>&lt;P&gt;Sort by patient and date.&lt;/P&gt;
&lt;P&gt;In a data step:&lt;/P&gt;
&lt;P&gt;Retain new variables for each value. Everytime a valid value is encountered, set the respective retained variable.&lt;/P&gt;
&lt;P&gt;At last.patient, output.&lt;/P&gt;
&lt;P&gt;Drop the original variables.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2017 18:51:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398908#M66596</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-09-26T18:51:45Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new data set with variables from most recent visit date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398924#M66598</link>
      <description>&lt;P&gt;The time-saving idea would be to change your data once.&amp;nbsp; Instead of searching through previous observations each time you use the data, add the most recent value as a new variable and save it that way.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by subject date;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by subject;&lt;/P&gt;
&lt;P&gt;if first.subject then most_recent_bmi = bmi;&lt;/P&gt;
&lt;P&gt;retain most_recent_bmi;&lt;/P&gt;
&lt;P&gt;if bmi &amp;gt; . then most_recent_bmi = bmi;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now you can refer to either variable:&amp;nbsp; BMI (actual measurement at that visit) or MOST_RECENT_BMI (most recent measurement, including the value on the current visit).&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2017 19:21:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398924#M66598</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-09-26T19:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new data set with variables from most recent visit date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398964#M66599</link>
      <description>&lt;P&gt;This program will do what you want for all numeric measures.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
  by sub date;
run;

data want (drop=_:);
  set have (keep=sub date);
  by sub;

  retain _sentinel1;
  set have;
  retain _sentinel2;

  array orig {*}   _sentinel1-numeric-_sentinel2;  /*all numerics except SUB and DATE*/
  array tmpn {100} _temporary_;  /* Assumes &amp;lt;=100 numeric vars */

  if first.sub then call missing(of tmpn{*});

  do I=2 to dim(orig)-1;
    if missing(orig{I}) then orig{I}=tmpn{I};
    else tmpn{I}=orig{i};
  end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are other vars that you do NOT want to do LOCF (last observation carried forward), then include them in the "Keep=" parameter in the first SET statement.&amp;nbsp; That way they will not fall between SENTINEL1 and SENTINEL2 in the program data vector and therefore not be in the ORIG array.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2017 22:08:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398964#M66599</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-26T22:08:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new data set with variables from most recent visit date</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398979#M66601</link>
      <description>&lt;P&gt;Here's a better way than my prior suggestion:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;&lt;BR /&gt;  by sub date;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;
  update have (obs=0) have (in=inright);
  by sub;
  if inright then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Remember, unlike MERGE,&amp;nbsp;the UPDATE statement will apply only &lt;EM&gt;&lt;STRONG&gt;non-missing values&lt;/STRONG&gt;&lt;/EM&gt; from the right-hand dataset to the PDV.&amp;nbsp; (Merge would take missing values from the right side as well).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why the "obs=0" on the left hand dataset?&amp;nbsp; That's because it's just being used as a template, but not tp contribute&amp;nbsp;actual data values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Usually the UPDATE statement is meant to apply a series of "transactions" to a master file, and therefore outputs only one record per BY level (one per sub below).&amp;nbsp; But you want to ouput one record per incoming record.&amp;nbsp; Hence the "if inright then output" statement.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Sep 2017 23:54:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Creating-new-data-set-with-variables-from-most-recent-visit-date/m-p/398979#M66601</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-09-26T23:54:23Z</dc:date>
    </item>
  </channel>
</rss>

