<?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: How to do first. and last. with missing data (retain/lag?) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568525#M160051</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16552"&gt;@mdavidson&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You're welcome, of course, but your "also" gets me a bit puzzled because&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s program and mine generate different outputs. Namely, mine outputs only the "start" and "final" rows (as in your sample output), while&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s keeps all the rows, including "adjust". Neither is right or wrong, it just makes me wonder what it is that you really want.&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jun 2019 19:46:58 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-06-24T19:46:58Z</dc:date>
    <item>
      <title>How to do first. and last. with missing data (retain/lag?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568382#M159983</link>
      <description>&lt;P&gt;Sorry for the poor subject line.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm reading in a data set that is very odd and has a lot of missing data. I'll try to illustrate below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;

   input ID val comment $;

   datalines;                     

1        6    start     

.         12  adjust	

.         15  adjust	

.         6   final     

2        12   start     

2        15   final     

3        6    start     

.         12  adjust	

3         15  final		
;


data want;

   input ID val comment $;

   datalines;                     

1        6    start     

1         6   final     

2        12   start     

2        15   final     

3        6    start     

3         15  final		
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In order to get my "want" data set, I would normally sort by ID and use first/last processing to extract the records needed. However, in this case I don't always have a value in the ID field for the records that I want to capture. I'm not exactly sure if a retain or a lag statement would work in this case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas on an approach?&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jun 2019 15:02:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568382#M159983</guid>
      <dc:creator>mdavidson</dc:creator>
      <dc:date>2019-06-24T15:02:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to do first. and last. with missing data (retain/lag?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568386#M159985</link>
      <description>&lt;P&gt;Is the idea to replace the missing ID values with the earlier non-missing values?&lt;/P&gt;
&lt;P&gt;The same method can be used but without using BY statement or FIRST. and LAST. variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
   set have;
   if missing(id) then id=newid;
   else newid=id;
   retain newid ;
   drop newid;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Jun 2019 15:21:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568386#M159985</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-24T15:21:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to do first. and last. with missing data (retain/lag?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568421#M160004</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16552"&gt;@mdavidson&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;If&amp;nbsp;you can rely on the variable COMMENT (i.e. if in your sample input, it's not just for show) and the "start" row always has a non-missing ID value (as it appears from your sample input), then one variant could be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                           
  input ID val comment $ ;            
   cards ;                            
1   6  start                          
.  12  adjust                         
.  15  adjust                         
.   6  final                          
2  12  start                          
2  15  final                          
3   6  start                          
.  12  adjust                         
3  15  final                          
run ;                                 
                                      
data want (drop = _:) ;               
  retain ID ;                         
  set have (rename = (id = _id)) ;    
  if comment = "start" then ID = _id ;
  if comment in ("start", "final") ;  
run ;                                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jun 2019 16:32:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568421#M160004</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-24T16:32:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to do first. and last. with missing data (retain/lag?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568516#M160042</link>
      <description>Tom -- thanks so much, brilliant!</description>
      <pubDate>Mon, 24 Jun 2019 19:30:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568516#M160042</guid>
      <dc:creator>mdavidson</dc:creator>
      <dc:date>2019-06-24T19:30:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to do first. and last. with missing data (retain/lag?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568518#M160044</link>
      <description>Thanks Paul, this also works great!</description>
      <pubDate>Mon, 24 Jun 2019 19:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568518#M160044</guid>
      <dc:creator>mdavidson</dc:creator>
      <dc:date>2019-06-24T19:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to do first. and last. with missing data (retain/lag?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568525#M160051</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16552"&gt;@mdavidson&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You're welcome, of course, but your "also" gets me a bit puzzled because&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s program and mine generate different outputs. Namely, mine outputs only the "start" and "final" rows (as in your sample output), while&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s keeps all the rows, including "adjust". Neither is right or wrong, it just makes me wonder what it is that you really want.&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jun 2019 19:46:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-first-and-last-with-missing-data-retain-lag/m-p/568525#M160051</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-24T19:46:58Z</dc:date>
    </item>
  </channel>
</rss>

