<?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: extract last entry without losing data in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787586#M251674</link>
    <description>&lt;P&gt;Thanks for posting data in usable form!&lt;/P&gt;
&lt;P&gt;Is it sure, that the first obs (for each patient) contains all data, or is it necessary to find the most complete observation first?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
   if 0 then set work.fake_data;
   set work.fake_data(rename=(
         dob = _dob
         eth = _eth
         status = _status
         dx_date = _dx_date         
      ));
   by pat_id;
   
   retain dob eth status dx_date;

   if first.pat_id then do;
      dob = _dob;
      eth = _eth;
      status = _status;
      dx_date = _dx_date;
   end;
   
   if last.pat_id then do;
      output;
   end;
   
   drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 29 Dec 2021 07:12:01 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2021-12-29T07:12:01Z</dc:date>
    <item>
      <title>extract last entry without losing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787580#M251672</link>
      <description>&lt;P&gt;I have the below data. I am interested in getting the dx_date as the start date and the last entry for snap date as the end date. The problem is for many patients the last couple of entries for dob and status are missing, and for eth Z is coded for unknown. How can I extract the last entry for snap date as the end date while keeping all other data?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data fake_data;&lt;BR /&gt;input pat_id $ dob:MMDDYY10. eth $ status $ dx_date:MMDDYY10. snap_date:MMDDYY10. ;&lt;BR /&gt;format dob MMDDYY10. dx_date MMDDYY10. snap_date MMDDYY10.;&lt;BR /&gt;datalines;&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 08/1/2020&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 09/1/2020&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 10/1/2020&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 11/1/2020&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 12/1/2020&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 01/1/2021&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 02/1/2021&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 03/1/2021&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 04/1/2021&lt;BR /&gt;A45926 09/26/1975 C A 05/1/2020 05/1/2021&lt;BR /&gt;A45926 . Z A . 06/1/2021&lt;BR /&gt;A45926 . Z A . 07/1/2021&lt;BR /&gt;;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Dec 2021 06:33:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787580#M251672</guid>
      <dc:creator>axescot78</dc:creator>
      <dc:date>2021-12-29T06:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: extract last entry without losing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787586#M251674</link>
      <description>&lt;P&gt;Thanks for posting data in usable form!&lt;/P&gt;
&lt;P&gt;Is it sure, that the first obs (for each patient) contains all data, or is it necessary to find the most complete observation first?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;
   if 0 then set work.fake_data;
   set work.fake_data(rename=(
         dob = _dob
         eth = _eth
         status = _status
         dx_date = _dx_date         
      ));
   by pat_id;
   
   retain dob eth status dx_date;

   if first.pat_id then do;
      dob = _dob;
      eth = _eth;
      status = _status;
      dx_date = _dx_date;
   end;
   
   if last.pat_id then do;
      output;
   end;
   
   drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Dec 2021 07:12:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787586#M251674</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-12-29T07:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: extract last entry without losing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787590#M251676</link>
      <description>&lt;P&gt;Here is a solution for filling out missing values of DOB and DX_DATE, and replacing ETH='Z' with the last non-Z value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;                             
  set fake_data;                       
  by pat_id;                           
              
  if first.pat_id then do;         
    _dx_date=dx_date;     
    _dob=dob;           
    _eth=eth;  
    end;     
  else do;
    if not missing(dx_date) then          
      _dx_date=dx_date;     
    else
      dx_date=_dx_date;             
    if not missing(dob) then              
      _dob=dob;           
    else 
      dob=_dob;               
    if eth ne 'Z' then                   
      _eth=eth;  
    else
      eth=_eth;       
    end;     
  retain _:;                           
  drop _:;                             
run;                                   
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Dec 2021 08:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787590#M251676</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-12-29T08:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: extract last entry without losing data</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787696#M251737</link>
      <description>&lt;P&gt;Thanks for both solutions. They both worked!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Dec 2021 22:22:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extract-last-entry-without-losing-data/m-p/787696#M251737</guid>
      <dc:creator>axescot78</dc:creator>
      <dc:date>2021-12-29T22:22:57Z</dc:date>
    </item>
  </channel>
</rss>

