<?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: proc sql : first and last in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806049#M317538</link>
    <description>&lt;PRE&gt;ods select none;
ods output sql_results=sql;
proc sql number;
select * from sashelp.class order by sex,age;
quit;
ods select all;

proc sql;
create table want as
select *,row-min(row)+1 as n from sql
 group by sex,age
  order by row;
quit;&lt;/PRE&gt;</description>
    <pubDate>Tue, 05 Apr 2022 13:25:25 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-04-05T13:25:25Z</dc:date>
    <item>
      <title>proc sql : first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806046#M317537</link>
      <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm wondering how to translate this code in proc sql : is it possible to write "first" function on proc sql ?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table2;
	set table1;
	by prod lb_lg;
	retain N;

	if first.prod then
		do;
			N = 1;
			output;
		end;
	else
		do;
			N = N + 1;
			output;
		end;
run&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Thank you for your help !&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 13:16:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806046#M317537</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2022-04-05T13:16:40Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql : first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806049#M317538</link>
      <description>&lt;PRE&gt;ods select none;
ods output sql_results=sql;
proc sql number;
select * from sashelp.class order by sex,age;
quit;
ods select all;

proc sql;
create table want as
select *,row-min(row)+1 as n from sql
 group by sex,age
  order by row;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Apr 2022 13:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806049#M317538</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-04-05T13:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql : first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806058#M317544</link>
      <description>&lt;P&gt;Basically no, because SQL thinks in sets (order irrelevant) and not in a sequence of rows.&lt;/P&gt;
&lt;P&gt;As already shown, you can do some tricks, but the data step will outperform them by orders of magnitude.&lt;/P&gt;
&lt;P&gt;And you can simplify your data step code, which also improves readability/maintainability:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table2;
set table1;
by prod lb_lg;
if first.prod
then N = 1;
else N + 1;
run&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SUM statement implies an automatic retain, and since you had OUTPUT in both branches of the IF, you can use the implicit output of the data step.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 13:45:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806058#M317544</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-05T13:45:13Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql : first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806082#M317554</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286185"&gt;@SASdevAnneMarie&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;This can be done indirectly if the idea is to use Proc Sql. I am showing an example with sashelp.class as follows.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table test as
select *
  from
(select monotonic() as N,* from sashelp.class)
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first few rows of the output will be as follows&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_0-1649170646799.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/70132iCC8667885615CD8D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sajid01_0-1649170646799.png" alt="Sajid01_0-1649170646799.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 14:57:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806082#M317554</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-04-05T14:57:42Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql : first and last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806101#M317562</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286185"&gt;@SASdevAnneMarie&lt;/a&gt;&amp;nbsp;but one must always be advised that the working of the (undocumented!) function MONOTONIC is not guaranteed. Depending on the data source, it may not create the expected result.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Apr 2022 16:08:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-first-and-last/m-p/806101#M317562</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-05T16:08:11Z</dc:date>
    </item>
  </channel>
</rss>

