<?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 First and Last Rows by Group with Conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812394#M320538</link>
    <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to assign First and Last to a row that meets a number of conditions. I have sorted the table by ID# and Location and Key Date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A row must meet all 3 conditions (A,B,C) = 'Yes', otherwise it will skip to the next row (within the group ID# and Location) to assign First or Last. Is there a way to do this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="75%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="57px"&gt;ID#&lt;/TD&gt;
&lt;TD width="12.5%" height="57px"&gt;Location&lt;/TD&gt;
&lt;TD width="6.25%" height="57px"&gt;Key Date&lt;/TD&gt;
&lt;TD width="6.25%" height="57px"&gt;Key Time&lt;/TD&gt;
&lt;TD width="12.5%" height="57px"&gt;Condition A&lt;/TD&gt;
&lt;TD width="12.5%" height="57px"&gt;Condition B&lt;/TD&gt;
&lt;TD width="6.25%" height="57px"&gt;Condition C&lt;/TD&gt;
&lt;TD width="3.125%" height="57px"&gt;First&lt;/TD&gt;
&lt;TD width="3.125%" height="57px"&gt;Last&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&amp;nbsp;&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;8:00 AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;8:05 AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;9:00AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;No&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;No&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;9:05AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;5&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;9:08AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD height="30px"&gt;6&lt;/TD&gt;
&lt;TD height="30px"&gt;3&lt;/TD&gt;
&lt;TD height="30px"&gt;1/2/2021&lt;/TD&gt;
&lt;TD height="30px"&gt;9:00AM&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;1&lt;/TD&gt;
&lt;TD height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD height="33px"&gt;7&lt;/TD&gt;
&lt;TD height="33px"&gt;3&lt;/TD&gt;
&lt;TD height="33px"&gt;1/2/2021&lt;/TD&gt;
&lt;TD height="33px"&gt;9:15AM&lt;/TD&gt;
&lt;TD height="33px"&gt;Yes&lt;/TD&gt;
&lt;TD height="33px"&gt;Yes&lt;/TD&gt;
&lt;TD height="33px"&gt;No&lt;/TD&gt;
&lt;TD height="33px"&gt;0&lt;/TD&gt;
&lt;TD height="33px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD height="30px"&gt;8&lt;/TD&gt;
&lt;TD height="30px"&gt;3&lt;/TD&gt;
&lt;TD height="30px"&gt;1/2/2021&lt;/TD&gt;
&lt;TD height="30px"&gt;9:10AM&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;0&lt;/TD&gt;
&lt;TD height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
    <pubDate>Tue, 10 May 2022 13:25:46 GMT</pubDate>
    <dc:creator>ysk</dc:creator>
    <dc:date>2022-05-10T13:25:46Z</dc:date>
    <item>
      <title>First and Last Rows by Group with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812394#M320538</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to assign First and Last to a row that meets a number of conditions. I have sorted the table by ID# and Location and Key Date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A row must meet all 3 conditions (A,B,C) = 'Yes', otherwise it will skip to the next row (within the group ID# and Location) to assign First or Last. Is there a way to do this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="75%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="57px"&gt;ID#&lt;/TD&gt;
&lt;TD width="12.5%" height="57px"&gt;Location&lt;/TD&gt;
&lt;TD width="6.25%" height="57px"&gt;Key Date&lt;/TD&gt;
&lt;TD width="6.25%" height="57px"&gt;Key Time&lt;/TD&gt;
&lt;TD width="12.5%" height="57px"&gt;Condition A&lt;/TD&gt;
&lt;TD width="12.5%" height="57px"&gt;Condition B&lt;/TD&gt;
&lt;TD width="6.25%" height="57px"&gt;Condition C&lt;/TD&gt;
&lt;TD width="3.125%" height="57px"&gt;First&lt;/TD&gt;
&lt;TD width="3.125%" height="57px"&gt;Last&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&amp;nbsp;&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;8:00 AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;8:05 AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;9:00AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;No&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;No&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;9:05AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="12.5%" height="30px"&gt;5&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;1/1/2021&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;9:08AM&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="12.5%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="6.25%" height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="3.125%" height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD height="30px"&gt;6&lt;/TD&gt;
&lt;TD height="30px"&gt;3&lt;/TD&gt;
&lt;TD height="30px"&gt;1/2/2021&lt;/TD&gt;
&lt;TD height="30px"&gt;9:00AM&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;1&lt;/TD&gt;
&lt;TD height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD height="33px"&gt;7&lt;/TD&gt;
&lt;TD height="33px"&gt;3&lt;/TD&gt;
&lt;TD height="33px"&gt;1/2/2021&lt;/TD&gt;
&lt;TD height="33px"&gt;9:15AM&lt;/TD&gt;
&lt;TD height="33px"&gt;Yes&lt;/TD&gt;
&lt;TD height="33px"&gt;Yes&lt;/TD&gt;
&lt;TD height="33px"&gt;No&lt;/TD&gt;
&lt;TD height="33px"&gt;0&lt;/TD&gt;
&lt;TD height="33px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD height="30px"&gt;8&lt;/TD&gt;
&lt;TD height="30px"&gt;3&lt;/TD&gt;
&lt;TD height="30px"&gt;1/2/2021&lt;/TD&gt;
&lt;TD height="30px"&gt;9:10AM&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;Yes&lt;/TD&gt;
&lt;TD height="30px"&gt;0&lt;/TD&gt;
&lt;TD height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 10 May 2022 13:25:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812394#M320538</guid>
      <dc:creator>ysk</dc:creator>
      <dc:date>2022-05-10T13:25:46Z</dc:date>
    </item>
    <item>
      <title>Re: First and Last Rows by Group with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812399#M320540</link>
      <description>If I understand well, first and last positions are fixed on by group. we might use these for other data manipulation but not create first and last positions. May be other experts give depth on this.</description>
      <pubDate>Tue, 10 May 2022 13:42:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812399#M320540</guid>
      <dc:creator>KANDIPATI</dc:creator>
      <dc:date>2022-05-10T13:42:55Z</dc:date>
    </item>
    <item>
      <title>Re: First and Last Rows by Group with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812408#M320546</link>
      <description>&lt;P&gt;I'm not clear on what you are attempting to do, for example the 1st observations first=1 last=0, why isn't last=1 as that's the only occurrence where ID=1. In fact all of the observations where Condition A, B &amp;amp; C = Yes, should have both first=1 and last=1 as all the ID values are unique. At least that's how I understand your description.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;You are probably going to need to use&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lepg/p181g1p4bw3phkn1vt5p67xvynd5.htm" target="_self"&gt;FIRST. and LAST. DATA Step Variables&lt;/A&gt;&amp;nbsp;in some capacity&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 14:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812408#M320546</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2022-05-10T14:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: First and Last Rows by Group with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812412#M320548</link>
      <description>&lt;P&gt;This example is difficult to follow, and I'm not sure if there are other idiosyncrasies that need to be captured. This seems to output what you want, and I don't get any errors in my log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines delimiter = ',';
input id $ location $ key_date :mmddyy10. key_time :time5. condition_a $ condition_b $ condition_c $;
format key_date mmddyy10. key_time time5.;
datalines;
1,1,1/1/2021,8:00 AM,Yes,Yes,Yes
2,1,1/1/2021,8:05 AM,Yes,Yes,Yes
3,2,1/1/2021,9:00 AM,No,Yes,No
4,2,1/1/2021,9:05 AM,Yes,Yes,Yes
5,2,1/1/2021,9:08 AM,Yes,Yes,Yes
6,3,1/2/2021,9:00 AM,Yes,Yes,Yes
7,3,1/2/2021,9:15 AM,Yes,Yes,No
8,3,1/2/2021,9:10 AM,Yes,Yes,Yes
;
run;

data have_2;
	set have;
		if condition_a = 'Yes' and condition_b = 'Yes' and condition_c = 'Yes' then yes = 1;
			else yes = 0;
run;

proc sort data = have_2;
	by location yes id;
run;  

data want (drop = yes);
	set have_2;
	by location yes;
		if first.yes and yes = 1 then first = 1;
			else first = 0;
		if last.yes and yes = 1then last = 1;
			else last = 0;
run;

proc sort data = want;
	by id location key_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="maguiremq_0-1652194037308.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71266i9C895B0E66015849/image-size/medium?v=v2&amp;amp;px=400" role="button" title="maguiremq_0-1652194037308.png" alt="maguiremq_0-1652194037308.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would generally advise against naming variables that conflict with any SAS functions/automatic variables.&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 14:48:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812412#M320548</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2022-05-10T14:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: First and Last Rows by Group with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812416#M320550</link>
      <description>sorry i can't seem to edit my post but i'm using Key Date and Location to sort and therefore, even with unique IDs, if they have the same key date and location it will be grouped together and essentially first and last will be determined by the key time and whether they meet the 3 conditions.</description>
      <pubDate>Tue, 10 May 2022 14:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812416#M320550</guid>
      <dc:creator>ysk</dc:creator>
      <dc:date>2022-05-10T14:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: First and Last Rows by Group with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812446#M320563</link>
      <description>&lt;P&gt;First determine first and last in a filtered dataset, then merge back:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data first_last;
set have (where=(condition_a = "yes" and condition_b = "yes" and condition_c = "yes"));
by id location;
first = first.location;
last = last.location;
drop condition_:;
run;

data want;
merge
  have
  first_last (in=fl)
;
by id location key_date key_time;
if not fl
then do;
  first = 0
  last = 0;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested; for tested code, provide usable example data in a data step with datalines.&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 15:57:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812446#M320563</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-05-10T15:57:58Z</dc:date>
    </item>
    <item>
      <title>Re: First and Last Rows by Group with Conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812478#M320578</link>
      <description>&lt;P&gt;You can use BY group processing to do most of it.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by keydate location conditiona&amp;nbsp;conditionb&amp;nbsp;conditionc&amp;nbsp;NOTSORTED;
&amp;nbsp;&amp;nbsp;first&amp;nbsp;=&amp;nbsp;first.conditionc;
&amp;nbsp;&amp;nbsp;last&amp;nbsp;=&amp;nbsp;last.conditionc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You seem to want to clear the FIRST and LAST flags when at least one of the conditions are not met.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by keydate location conditiona&amp;nbsp;conditionb&amp;nbsp;conditionc&amp;nbsp;NOTSORTED.
&amp;nbsp;&amp;nbsp;first&amp;nbsp;=&amp;nbsp;first.conditionc and 3=count('Yes',cats(conditiona,conditionb,conditionc));
&amp;nbsp;&amp;nbsp;last&amp;nbsp;=&amp;nbsp;last.conditionc and 3=count('Yes',cats(conditiona,conditionb,conditionc));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it might be better to make a separate flag variable to indicate whether or not the condition was met.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by keydate location conditiona&amp;nbsp;conditionb&amp;nbsp;conditionc&amp;nbsp;NOTSORTED;
  all_yes = 3=count('Yes',cats(conditiona,conditionb,conditionc));
&amp;nbsp;&amp;nbsp;first&amp;nbsp;=&amp;nbsp;first.conditionc ; 
&amp;nbsp;&amp;nbsp;last&amp;nbsp;=&amp;nbsp;last.conditionc ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Do not use spaces in your variable names. It makes the code impossible to read and type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 19:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-and-Last-Rows-by-Group-with-Conditions/m-p/812478#M320578</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-10T19:33:12Z</dc:date>
    </item>
  </channel>
</rss>

