<?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 can I get the first and the last NON-MISSING value from a particular column(s) by group? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733726#M228630</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID ID1 Mark1 Mark2 $ GetSum;
datalines;
1 1 .  N 1 
1 1 76 N 2
1 1 67 Y 3
2 2 .  N 4
2 2 32 N 5
2 2 45 Y 6
;
run;


data want;
length Mark2First Mark2Last $1.;
call missing(Mark1First,Mark1Last,Mark2First,Mark2Last,Sum_GetSum);
do until(last.id);
	set have;
	by id ;

	Sum_GetSum=sum(GetSum,Sum_GetSum);
	if  missing(Mark1First) then do;
			Mark1First=Mark1;
			Mark2First=Mark2;
	end;
end;
Mark1Last=Mark1;
Mark2Last=Mark2
;

drop Mark1 Mark2 GetSum; 

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 14 Apr 2021 13:08:06 GMT</pubDate>
    <dc:creator>r_behata</dc:creator>
    <dc:date>2021-04-14T13:08:06Z</dc:date>
    <item>
      <title>How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733385#M228534</link>
      <description>&lt;P&gt;I have a dataset as follows:&lt;/P&gt;
&lt;PRE&gt;data have;
  input ID ID1 Mark1;
datalines;
1   1       .
1   1       76
1   1       67
2   2       .
2   2       32&lt;BR /&gt;2   2       45
run;&lt;/PRE&gt;
&lt;P&gt;I would like to group by ID and ID1 and extract the first and last non-missing values of mark for each group so that the resultant dataset looks like this:&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp;ID1&amp;nbsp; &amp;nbsp;Mark1First&amp;nbsp; &amp;nbsp;Mark1Last&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; 1&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; &amp;nbsp; 67&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;32&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 45&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 17:15:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733385#M228534</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-04-13T17:15:42Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733392#M228537</link>
      <description>&lt;P&gt;Please try this:&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 ID ID2;
run;

data have_1;
set have;
by ID ID1;
retain Mark1First Mark1Last;
retain first last;
if first.ID1 then do;
   first=.;
   last=.;

   Mark1First=.;
   Mark1Last=.;
end;
if first=. and Mark1^=. then do; 
   first=1; 
   Mark1First=Mark1;
end;
if Mark1^=. then Mark1Last=Mark1;
if last.ID1 then output;

keep ID ID1 Mark1First Mark1Last;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 17:44:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733392#M228537</guid>
      <dc:creator>Jerrya00</dc:creator>
      <dc:date>2021-04-13T17:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733394#M228539</link>
      <description>&lt;P&gt;Use the WHERE statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;&lt;BR /&gt;data have;
  input ID ID1 Mark1;
datalines;
1   1       .
1   1       76
1   1       67
2   2       .
2   2       32
2   2       45
run;

data want (drop=mark1);
  set have (where=(mark1^=.));
  by id id1;
  retain mark1first;
  if first.id1 then mark1first=mark1;
  if last.id1;
  mark1last=mark1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Apr 2021 17:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733394#M228539</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-04-13T17:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733417#M228541</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;

Mark1First=.;
Mark1Last=.;
do until(last.id);
	set have;
	by id ;
	if  missing(Mark1First) then Mark1First=Mark1;
end;
Mark1Last=Mark1;
drop Mark1;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Apr 2021 18:46:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733417#M228541</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-04-13T18:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733450#M228543</link>
      <description>&lt;P&gt;what if there's also a Mark2 and I have to do this for both Mark1 and Mark2?&lt;/P&gt;
&lt;P&gt;i.e. Mark1First, Mark2First, Mark1Last, Mark2Last?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 19:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733450#M228543</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-04-13T19:32:12Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733477#M228545</link>
      <description>&lt;P&gt;what if there is also a Mark2 and I have to do this for both Mark1 and Mark2?&lt;/P&gt;
&lt;P&gt;i.e. Mark1First, Mark2First, Mark1Last, Mark2Last?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 19:33:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733477#M228545</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-04-13T19:33:48Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733478#M228546</link>
      <description>&lt;P&gt;what if there's also a Mark2 and I need to do this for both Mark1 and Mark2?&lt;/P&gt;
&lt;P&gt;i.e. Mark1First, Mark2First, Mark1Last, Mark2Last?&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 19:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733478#M228546</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-04-13T19:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733488#M228548</link>
      <description>&lt;P&gt;Try this :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID ID1 Mark1 Mark2;
datalines;
1 1 .  .
1 1 76 33
1 1 67 58
2 2 .  .
2 2 32 89
2 2 45 11
;
run;

data want;

call missing(Mark1First,Mark1Last,Mark2First,Mark2Last);
do until(last.id);
	set have;
	by id ;
	if  missing(Mark1First) then Mark1First=Mark1;
	if  missing(Mark2First) then Mark2First=Mark2;

end;
Mark1Last=Mark1;
Mark2Last=Mark2
;

drop Mark1 Mark2;

run;



proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Apr 2021 19:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733488#M228548</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-04-13T19:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733501#M228550</link>
      <description>&lt;P&gt;Actually my data set looks a little like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID ID1 Mark1 Mark2;
datalines;
1 1 .  N
1 1 76 N
1 1 67 Y
2 2 .  N
2 2 32 N
2 2 45 Y
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So I need to make sure that Mark2First for ID 1 is the 'N' coressponding to the row with Mark1 as 76 and not the very first 'N' that you see in the column&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 20:12:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733501#M228550</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-04-13T20:12:53Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733510#M228552</link>
      <description>&lt;P&gt;Try this code. I have changed the First N to X to show the distinction.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID ID1 Mark1 Mark2 $;
datalines;
1 1 .  X
1 1 76 N
1 1 67 Y
2 2 .  X
2 2 32 N
2 2 45 Y
;
run;


data want;
length Mark2First Mark2Last $1.;
call missing(Mark1First,Mark1Last,Mark2First,Mark2Last);
do until(last.id);
	set have;
	by id ;
	if  missing(Mark1First) then do;
			Mark1First=Mark1;
			Mark2First=Mark2;
	end;
end;
Mark1Last=Mark1;
Mark2Last=Mark2
;

drop Mark1 Mark2;

run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 13 Apr 2021 20:36:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733510#M228552</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-04-13T20:36:35Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733720#M228627</link>
      <description>&lt;P&gt;Oh that works! Here's one last update (I promise!)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID ID1 Mark1 Mark2 GetSum;
datalines;
1 1 .  N 1 
1 1 76 N 2
1 1 67 Y 3
2 2 .  N 4
2 2 32 N 5
2 2 45 Y 6
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I also want to get the sum of ALL values from the getsum column by the group so the output should be 6 and 15 for the 1st and 2nd row and not 5 and 11&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 12:58:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733720#M228627</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-04-14T12:58:43Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733725#M228629</link>
      <description>&lt;P&gt;There is too much mission creep in this topic.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 13:06:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733725#M228629</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-04-14T13:06:59Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733726#M228630</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID ID1 Mark1 Mark2 $ GetSum;
datalines;
1 1 .  N 1 
1 1 76 N 2
1 1 67 Y 3
2 2 .  N 4
2 2 32 N 5
2 2 45 Y 6
;
run;


data want;
length Mark2First Mark2Last $1.;
call missing(Mark1First,Mark1Last,Mark2First,Mark2Last,Sum_GetSum);
do until(last.id);
	set have;
	by id ;

	Sum_GetSum=sum(GetSum,Sum_GetSum);
	if  missing(Mark1First) then do;
			Mark1First=Mark1;
			Mark2First=Mark2;
	end;
end;
Mark1Last=Mark1;
Mark2Last=Mark2
;

drop Mark1 Mark2 GetSum; 

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 14 Apr 2021 13:08:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733726#M228630</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2021-04-14T13:08:06Z</dc:date>
    </item>
    <item>
      <title>Re: How can I get the first and the last NON-MISSING value from a particular column(s) by group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733737#M228632</link>
      <description>&lt;P&gt;that worked like a charm, thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 13:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-get-the-first-and-the-last-NON-MISSING-value-from-a/m-p/733737#M228632</guid>
      <dc:creator>aalluru</dc:creator>
      <dc:date>2021-04-14T13:22:41Z</dc:date>
    </item>
  </channel>
</rss>

