<?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: Using a function in by statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497405#M131827</link>
    <description>&lt;P&gt;If you can't change the original table, then take a copy of it, and change the copy?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Sep 2018 14:47:22 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-09-20T14:47:22Z</dc:date>
    <item>
      <title>Using a function in by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497387#M131814</link>
      <description>&lt;P&gt;I am trying to find the last row per year in a data set. Adding an extra column is not possible in the real case. I have created a small example, which obviously does not work. Is there any smooth way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA test;&lt;BR /&gt;infile cards dlm=',' dsd;&lt;BR /&gt;INPUT DATE1 :DATE9. count: 1.;&lt;BR /&gt;FORMAT DATE1 DDMMYY10. count 1.;&lt;BR /&gt;CARDS;&lt;BR /&gt;11MAY2009, 1&lt;BR /&gt;12MAY2009, 1&lt;BR /&gt;13MAY2009, 1&lt;BR /&gt;14MAY2009, 5&lt;BR /&gt;11MAY2010, 1&lt;BR /&gt;16MAY2010, 1&lt;BR /&gt;17MAY2010, 1&lt;BR /&gt;19MAY2010, 7&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;data test1 ;&lt;BR /&gt;set test ;&lt;BR /&gt;by year(date1); /* GROUP PER YEAR */&lt;BR /&gt;if last.year;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 14:29:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497387#M131814</guid>
      <dc:creator>Jeg123</dc:creator>
      <dc:date>2018-09-20T14:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using a function in by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497391#M131817</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Adding an extra column is not possible in the real case.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Wow, that's quite a strong restriction. SAS makes no such restriction. I can't even imagine why you would not be able to add the extra column in a temporary data set and then the desired BY statement works properly.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 14:33:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497391#M131817</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-09-20T14:33:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using a function in by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497394#M131820</link>
      <description>Strange that SAS does not allow in, while it works in PROC SQL</description>
      <pubDate>Thu, 20 Sep 2018 14:35:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497394#M131820</guid>
      <dc:creator>Jeg123</dc:creator>
      <dc:date>2018-09-20T14:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Using a function in by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497396#M131821</link>
      <description>&lt;P&gt;You can't use a function in a BY statement.&amp;nbsp; But there are a couple of workarounds.&amp;nbsp; If you don't consider this cheating, you could add YEAR to a view and process the view:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test2 / view=test2;&lt;/P&gt;
&lt;P&gt;set test;&lt;/P&gt;
&lt;P&gt;year = year(date1);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set test2;&lt;/P&gt;
&lt;P&gt;by year;&lt;/P&gt;
&lt;P&gt;if last.year;&lt;/P&gt;
&lt;P&gt;drop year;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Technically, you never actually save YEAR values in a data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, you can use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set test end=done;&lt;/P&gt;
&lt;P&gt;prior_date = lag(date1);&lt;/P&gt;
&lt;P&gt;drop prior_date;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;if done then output;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;if year(date1) ne year(prior_date) then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;_n_ = _n_ - 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if _n_ &amp;gt; 0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set test point=_n_;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if done then output;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 14:39:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497396#M131821</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-09-20T14:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using a function in by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497403#M131825</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228606"&gt;@Jeg123&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Strange that SAS does not allow in, while it works in PROC SQL&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please show an example of BY processing in Proc SQL. "GROUP BY" is &lt;STRONG&gt;not the same&lt;/STRONG&gt; as By processing. Group By collects records on output, BY indicates an order and grouping on input from data.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 14:45:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497403#M131825</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-09-20T14:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: Using a function in by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497405#M131827</link>
      <description>&lt;P&gt;If you can't change the original table, then take a copy of it, and change the copy?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 14:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497405#M131827</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-20T14:47:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using a function in by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497408#M131829</link>
      <description>&lt;P&gt;Yes, use a format and the GROUPFORMAT option on the BY statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please paste your code in a text box, and make sure it runs before posting. Your data step did not work correctly for me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
infile cards dlm=',' dsd;
informat date1 date9. count 8.;
INPUT DATE1  count ;
FORMAT DATE1 DDMMYY10. count 8.;
CARDS;
11MAY2009, 1
12MAY2009, 1
13MAY2009, 1
14MAY2009, 5
11MAY2010, 1
16MAY2010, 1
17MAY2010, 1
19MAY2010, 7
;
RUN;

data test1 ;
set test ;

by date1 groupformat; /* GROUP PER YEAR */
format date1 year4.;

if last.date1;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228606"&gt;@Jeg123&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to find the last row per year in a data set. Adding an extra column is not possible in the real case. I have created a small example, which obviously does not work. Is there any smooth way?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA test;&lt;BR /&gt;infile cards dlm=',' dsd;&lt;BR /&gt;INPUT DATE1 :DATE9. count: 1.;&lt;BR /&gt;FORMAT DATE1 DDMMYY10. count 1.;&lt;BR /&gt;CARDS;&lt;BR /&gt;11MAY2009, 1&lt;BR /&gt;12MAY2009, 1&lt;BR /&gt;13MAY2009, 1&lt;BR /&gt;14MAY2009, 5&lt;BR /&gt;11MAY2010, 1&lt;BR /&gt;16MAY2010, 1&lt;BR /&gt;17MAY2010, 1&lt;BR /&gt;19MAY2010, 7&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;data test1 ;&lt;BR /&gt;set test ;&lt;BR /&gt;by year(date1); /* GROUP PER YEAR */&lt;BR /&gt;if last.year;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 14:53:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497408#M131829</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-20T14:53:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using a function in by statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497411#M131831</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228606"&gt;@Jeg123&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Strange that SAS does not allow in, while it works in PROC SQL&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Most of SAS is not SQL, it doesn't have to allow the same things as SQL. If you want most of SAS to behave the same as SQL, you are going to be disappointed and struggle with SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you haven't addressed the key question, can you create a temporary data set and then add a column to the temporary data set? Of course you can do this, why would this be not allowed?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Sep 2018 14:59:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-a-function-in-by-statement/m-p/497411#M131831</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-09-20T14:59:00Z</dc:date>
    </item>
  </channel>
</rss>

