<?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 get the value of first and last row grouped by a variable and put those in separate colum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/729450#M226991</link>
    <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id;
   retain Mark1First Mark1last Mark2first Mark2last;
   if first.id then do;
      Mark1First=mark1;
      Mark2first=mark2; 
   end;
   if last.id then do;
      Mark1last=mark1;
      Mark2last=mark2; 
      output;
   end;
   keep id Mark1First Mark1last Mark2first Mark2last; 
run;&lt;/PRE&gt;
&lt;P&gt;When you use a BY group in a data step SAS creates automatic variables that indicate whether the record is the first or last for a group. You reference these variables with the First. and Last. notation. These are numeric 1/0 for which can be used as 1=True and 0=False for use in if statements.&lt;/P&gt;
&lt;P&gt;The RETAIN statement sets up variables to keep values across iterations of the data step boundary.&lt;/P&gt;
&lt;P&gt;The explicit OUTPUT statement limits when records are written to the output data set.&lt;/P&gt;</description>
    <pubDate>Fri, 26 Mar 2021 17:33:23 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-03-26T17:33:23Z</dc:date>
    <item>
      <title>How to get the value of first and last row grouped by a variable and put those in separate columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/729449#M226990</link>
      <description>&lt;P&gt;I have a data set that I have grouped by 'ID'&lt;/P&gt;
&lt;P&gt;ID Mark1 Mark2&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;24&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;76&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;24&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;76&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;45&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;67&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;23&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;56&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;87&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;32&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want this to be transformed into&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Mark1First Mark1Last Mark2First Mark2Last&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; 24&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;45&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;76&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;67&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 23&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;87&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;56&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;32&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 17:24:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/729449#M226990</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-03-26T17:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the value of first and last row grouped by a variable and put those in separate colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/729450#M226991</link>
      <description>&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id;
   retain Mark1First Mark1last Mark2first Mark2last;
   if first.id then do;
      Mark1First=mark1;
      Mark2first=mark2; 
   end;
   if last.id then do;
      Mark1last=mark1;
      Mark2last=mark2; 
      output;
   end;
   keep id Mark1First Mark1last Mark2first Mark2last; 
run;&lt;/PRE&gt;
&lt;P&gt;When you use a BY group in a data step SAS creates automatic variables that indicate whether the record is the first or last for a group. You reference these variables with the First. and Last. notation. These are numeric 1/0 for which can be used as 1=True and 0=False for use in if statements.&lt;/P&gt;
&lt;P&gt;The RETAIN statement sets up variables to keep values across iterations of the data step boundary.&lt;/P&gt;
&lt;P&gt;The explicit OUTPUT statement limits when records are written to the output data set.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 17:33:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/729450#M226991</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-26T17:33:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the value of first and last row grouped by a variable and put those in separate colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/729452#M226992</link>
      <description>&lt;P&gt;With variable renaming and a strategic SET ... POINT= statement, you can have a relatively compact data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID Mark1 Mark2;
datalines;
1   24       76
1   24       76
1   45       67
2   23       56
2   87       32
run;
data want;
  set have (rename=(mark1=mark1last mark2=mark2last));
  by id;
  if first.id then set have (rename=(mark1=mark1first mark2=mark2first)) point=_n_;
  if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Editted addition.&amp;nbsp; An alternative that can be instructive about the use of 2 SET statements each with sequential access:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have (rename=(mark1=mark1first mark2=mark2first));
  by id;
  if first.id;
  do until (last.id);
    set have (rename=(mark1=mark1last mark2=mark2last));
    by id;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Mar 2021 17:41:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/729452#M226992</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-26T17:41:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the value of first and last row grouped by a variable and put those in separate colum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/733309#M228519</link>
      <description>&lt;P&gt;hey! the 2nd code worked! I was wondering if I could modify this to have the first populated row of mark1 i.e. if the mark1 column has missing values, I'd like to include the first row where mark1 is not missing in mark1first and also include a count variable that has the number of rows where mark1 is not missing&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 13:31:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-value-of-first-and-last-row-grouped-by-a-variable/m-p/733309#M228519</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-04-13T13:31:00Z</dc:date>
    </item>
  </channel>
</rss>

