<?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: First. And Last. question in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854755#M82413</link>
    <description>My goal is to count the number of groups for which the first value is B and&lt;BR /&gt;the last value is A. The number should be by date, i.e. in this case:&lt;BR /&gt;Date Count Group&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;02JAN2023:17:01:33 1 X/27580&lt;BR /&gt;&lt;BR /&gt;02JAN2023:19:10:10 1 X/27583&lt;BR /&gt;&lt;BR /&gt;It doesn't need to count the total number, just assign a value of one next&lt;BR /&gt;to a newly created column (let's call it Count) with the date of the last&lt;BR /&gt;record in the group.&lt;BR /&gt;</description>
    <pubDate>Fri, 20 Jan 2023 07:53:04 GMT</pubDate>
    <dc:creator>rzuf</dc:creator>
    <dc:date>2023-01-20T07:53:04Z</dc:date>
    <item>
      <title>First. And Last. question</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854700#M82411</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;I'm relatively new to SAS and I'm struggling with determining the first and last values in a group if they are different from each other.&amp;nbsp;I have brought the data to this form and I need to keep only those values whose first ID value is equal to B while the second is equal to A. According to this, in the following example, only two groups should remain.&amp;nbsp;Unfortunately with proc sql the condition with if first. and last. then is not working. Does anyone have any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Group&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;06JUL2022:17:56:14&lt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&gt;B&lt;/TD&gt;&lt;TD&gt;X/27582&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;06JUL2022:18:16:03&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;X/27583&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;25JUL2022:14:31:08&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;X/27583&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;09AUG2022:14:15:41&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;X/27583&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;23AUG2022:11:34:58&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;X/27583&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;04OCT2022:00:01:15&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;X/27583&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;02JAN2023:19:10:10&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;X/27583&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 19 Jan 2023 20:30:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854700#M82411</guid>
      <dc:creator>rzuf</dc:creator>
      <dc:date>2023-01-19T20:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: First. And Last. question</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854701#M82412</link>
      <description>&lt;P&gt;The First. and Last. automatic variables only occur in data step code where there is a BY statement.&lt;/P&gt;
&lt;P&gt;So SQL would not have them because it does not have a By statement. Also, SQL does not process records according to any existing data order. Very few operations that rely on sequential processing or identification will work well if at all in SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, what is the desired output from that data?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2023 20:42:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854701#M82412</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-01-19T20:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: First. And Last. question</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854755#M82413</link>
      <description>My goal is to count the number of groups for which the first value is B and&lt;BR /&gt;the last value is A. The number should be by date, i.e. in this case:&lt;BR /&gt;Date Count Group&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;02JAN2023:17:01:33 1 X/27580&lt;BR /&gt;&lt;BR /&gt;02JAN2023:19:10:10 1 X/27583&lt;BR /&gt;&lt;BR /&gt;It doesn't need to count the total number, just assign a value of one next&lt;BR /&gt;to a newly created column (let's call it Count) with the date of the last&lt;BR /&gt;record in the group.&lt;BR /&gt;</description>
      <pubDate>Fri, 20 Jan 2023 07:53:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854755#M82413</guid>
      <dc:creator>rzuf</dc:creator>
      <dc:date>2023-01-20T07:53:04Z</dc:date>
    </item>
    <item>
      <title>Re: First. And Last. question</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854768#M82414</link>
      <description>&lt;P&gt;Double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
count = 0;
do until (last.group);
  set have;
  by group;
  count + 1;
  if count = 1 and id = "B" then b = 1;
  if count = 2 and id = "A" then a = 1;
end;
do until (last.group);
  set have;
  by group;
  if a and b then output;
end;
drop count a b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested; for tested code, supply&amp;nbsp;&lt;U&gt;usable&lt;/U&gt; example data in a&amp;nbsp;&lt;U&gt;working data step with datalines&lt;/U&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jan 2023 08:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854768#M82414</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-01-20T08:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: First. And Last. question</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854862#M82415</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/438652"&gt;@rzuf&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an elementary approach without DO loops:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=id);
set have;
by group date;
if first.group then Count=(id='B');
if last.group &amp;amp; count &amp;amp; id='A';
retain count;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that the input dataset ("HAVE") is sorted by &lt;FONT face="courier new,courier"&gt;Group Date&lt;/FONT&gt;. Variable &lt;FONT face="courier new,courier"&gt;Count&lt;/FONT&gt; is set to 1 if the first observation of a group has &lt;FONT face="courier new,courier"&gt;ID='B'&lt;/FONT&gt; and to 0 otherwise. Thanks to the RETAIN statement, this information is kept until the last observation of the group, which will be written to the output dataset ("WANT") if &lt;FONT face="courier new,courier"&gt;Count=1&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;ID='A'&lt;/FONT&gt;, dropping the now redundant variable &lt;FONT face="courier new,courier"&gt;ID&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jan 2023 16:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854862#M82415</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-01-20T16:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: First. And Last. question</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854958#M82418</link>
      <description>Thank you for your help! I tested and both solutions work but yours is very clever. I've marked it as an answer and hope it will help someone else.</description>
      <pubDate>Sat, 21 Jan 2023 10:54:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/First-And-Last-question/m-p/854958#M82418</guid>
      <dc:creator>rzuf</dc:creator>
      <dc:date>2023-01-21T10:54:00Z</dc:date>
    </item>
  </channel>
</rss>

