<?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: Retain first observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475838#M122382</link>
    <description>&lt;P&gt;Here is a more detailed example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;

   input subject days grade;

datalines;

 1234              0	           0
 1234              1	           0
 1234             8	           0
 1234             15	           0
 1234             22	           1
 1234             29	           1
 1234             36	           2
 1234             43	           2
 1234             45	           2
 1234              52	           2
 1234             58	           0
 1234             64	           0
 1234             67	           1
 1234             71	           0
 1234             78	           1
 1234             85	           1
 1234             87	           0
 1234             92	           0
 1234             99	           0
 1234            106	           0
 1234            108	           1
 1234            113	           1
 1234            120	           1
 1234            127	           1
 1234            134	           0
 1234            141	           0
 1234           149	           2
 1234             155	           0
 1234            162	           0
 1234            169	           1
 1234             176	           1
 1234            184	           2
 1234            190	           0
 1234            197	           1
 1234            205	           1
 1234            211	           0
 1234            218	           1
 1234            225	           1
 1234            232	           1

 5678              0	           0
 5678              1	           0
 5678              8	           0
 5678             15	           0
 5678             22	           0
 5678             29	           0
 5678             36	           0
 5678             43	           0
 5678             50	           0
 5678             53	           0
 5678             57	           0
 5678             59	           0
run;&lt;/PRE&gt;&lt;P&gt;I expect the output to be,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;subject days grade&lt;/P&gt;&lt;P&gt;1234&amp;nbsp; &amp;nbsp;&amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; 22&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; 36&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; 58&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; 67&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp;71&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp;78&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp;87&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;1234&amp;nbsp; 108&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1234&amp;nbsp; 134&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;1234&amp;nbsp; 149&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;1234&amp;nbsp; 155&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1234&amp;nbsp; 169&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1234&amp;nbsp; 184&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;1234&amp;nbsp; 190&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1234&amp;nbsp; 197&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1234&amp;nbsp; 211&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1234&amp;nbsp; 218&amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;5678&amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Jul 2018 21:27:15 GMT</pubDate>
    <dc:creator>gpv2000</dc:creator>
    <dc:date>2018-07-05T21:27:15Z</dc:date>
    <item>
      <title>Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475814#M122373</link>
      <description>&lt;P&gt;In my dataset, i have variables subject day, grade.&lt;/P&gt;&lt;PRE&gt;data have;

   input subject days grade;

   datalines;
   
&lt;STRONG&gt;   1234    0     0   / *retain  */&lt;/STRONG&gt;
   1234    8     0
   1234   10     0
   1234   12     0
   &lt;STRONG&gt;5678    0     0   / *retain  */&lt;/STRONG&gt;
   5678    8     0
  &lt;STRONG&gt; 5678   10     1   /* retain */&lt;/STRONG&gt;
   5678   12     1
   &lt;STRONG&gt;5678   15     2&lt;/STRONG&gt;  &lt;STRONG&gt;/* retain*/&lt;/STRONG&gt;&lt;BR /&gt;   5678   22     2 &lt;BR /&gt;   5678   35     2&lt;BR /&gt;   &lt;STRONG&gt;5678   42     3  /* retain*/&lt;/STRONG&gt;&lt;BR /&gt; run;&lt;/PRE&gt;&lt;P&gt;I want to retain the first observation every time the grade changes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I need the output to look as below,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;STRONG&gt; subject    days  grade  &lt;BR /&gt;&lt;/STRONG&gt;   1234    0     0    &lt;BR /&gt;&lt;BR /&gt;   5678    0     0

  &lt;STRONG&gt; 5678   10     1       &lt;/STRONG&gt;
 
   &lt;STRONG&gt;5678   15     2&lt;/STRONG&gt;  &lt;BR /&gt; &lt;BR /&gt;   &lt;STRONG&gt;5678   42     3  &lt;/STRONG&gt;&lt;/PRE&gt;&lt;P&gt;I tried doing,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data&amp;nbsp;want;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;set&amp;nbsp;have;&lt;BR /&gt;&amp;nbsp; by&amp;nbsp;subject days;&lt;BR /&gt;&amp;nbsp; retain&amp;nbsp;grade;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;if NOT first.subject&amp;nbsp;then prev_grade = lag(grade);&lt;BR /&gt;&amp;nbsp; IF grade ne&amp;nbsp; &lt;SPAN&gt;prev_grade&amp;nbsp;then output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The result was almost ok except for the first subject, where there is only one grade.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Any suggestions?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 20:36:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475814#M122373</guid>
      <dc:creator>gpv2000</dc:creator>
      <dc:date>2018-07-05T20:36:25Z</dc:date>
    </item>
    <item>
      <title>Re: Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475819#M122376</link>
      <description>&lt;P&gt;Since you are looking for observations where GRADE changes, use that in the BY statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data&amp;nbsp;want;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;set&amp;nbsp;have;&lt;BR /&gt;&amp;nbsp; by&amp;nbsp;subject grade notsorted;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if first.grade;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 20:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475819#M122376</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-05T20:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475832#M122379</link>
      <description>&lt;P&gt;I&amp;nbsp; think the sample data i shared is pretty simple. But the real data has many changes in grade and it needs to be captured in the sequence it happens in regard with my days. The code you suggested is capturing the overall grade change. Please let me know if i am not being clear. I will try to work a more detailed example of data.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 21:05:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475832#M122379</guid>
      <dc:creator>gpv2000</dc:creator>
      <dc:date>2018-07-05T21:05:00Z</dc:date>
    </item>
    <item>
      <title>Re: Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475835#M122380</link>
      <description>&lt;P&gt;I'll have to see the updated question.&amp;nbsp; Remember, I'm not suggesting that you sort the data ... just leave it in its current order.&amp;nbsp; That's the purpose of adding "notsorted" on the BY statement.&amp;nbsp; The data isn't sorted by GRADE, but you can still detect when GRADE changes.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 21:13:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475835#M122380</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-05T21:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475838#M122382</link>
      <description>&lt;P&gt;Here is a more detailed example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;

   input subject days grade;

datalines;

 1234              0	           0
 1234              1	           0
 1234             8	           0
 1234             15	           0
 1234             22	           1
 1234             29	           1
 1234             36	           2
 1234             43	           2
 1234             45	           2
 1234              52	           2
 1234             58	           0
 1234             64	           0
 1234             67	           1
 1234             71	           0
 1234             78	           1
 1234             85	           1
 1234             87	           0
 1234             92	           0
 1234             99	           0
 1234            106	           0
 1234            108	           1
 1234            113	           1
 1234            120	           1
 1234            127	           1
 1234            134	           0
 1234            141	           0
 1234           149	           2
 1234             155	           0
 1234            162	           0
 1234            169	           1
 1234             176	           1
 1234            184	           2
 1234            190	           0
 1234            197	           1
 1234            205	           1
 1234            211	           0
 1234            218	           1
 1234            225	           1
 1234            232	           1

 5678              0	           0
 5678              1	           0
 5678              8	           0
 5678             15	           0
 5678             22	           0
 5678             29	           0
 5678             36	           0
 5678             43	           0
 5678             50	           0
 5678             53	           0
 5678             57	           0
 5678             59	           0
run;&lt;/PRE&gt;&lt;P&gt;I expect the output to be,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;subject days grade&lt;/P&gt;&lt;P&gt;1234&amp;nbsp; &amp;nbsp;&amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; 22&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; 36&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; 58&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; 67&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp;71&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp;78&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp;87&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;1234&amp;nbsp; 108&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1234&amp;nbsp; 134&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;1234&amp;nbsp; 149&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;1234&amp;nbsp; 155&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1234&amp;nbsp; 169&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1234&amp;nbsp; 184&amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;1234&amp;nbsp; 190&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1234&amp;nbsp; 197&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;1234&amp;nbsp; 211&amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1234&amp;nbsp; 218&amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;5678&amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 21:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475838#M122382</guid>
      <dc:creator>gpv2000</dc:creator>
      <dc:date>2018-07-05T21:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475843#M122383</link>
      <description>&lt;P&gt;Yes you are right. I sorted before your suggested step and it messed it up. Thanks. Your code works.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 22:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475843#M122383</guid>
      <dc:creator>gpv2000</dc:creator>
      <dc:date>2018-07-05T22:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475845#M122384</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if lag(grade) ne grade then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Try the lag function and conditionally output.&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jul 2018 22:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475845#M122384</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2018-07-05T22:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475857#M122390</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/111380"&gt;@bobpep212&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if lag(grade) ne grade then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Try the lag function and conditionally output.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since the values are nested with subject you would also need to test if subject changed.&amp;nbsp; In the sample data your test would not find the second subject's first value since the first subject ends with GRADE=0 and the second subject starts with GRADE=0.&lt;/P&gt;
&lt;P&gt;Either test that using similar LAG() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  if lag(subject) ne subject OR lag(grade) ne grade then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or use BY group for SUBJECT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by subject;
  if first.subject OR lag(grade) ne grade then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 05 Jul 2018 22:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/475857#M122390</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-05T22:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Retain first observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/476545#M122654</link>
      <description>&lt;P&gt;Thank you very much. Very useful suggestions. I tried them and they worked too.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Jul 2018 18:09:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-first-observation/m-p/476545#M122654</guid>
      <dc:creator>gpv2000</dc:creator>
      <dc:date>2018-07-09T18:09:21Z</dc:date>
    </item>
  </channel>
</rss>

