<?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.id result with proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676454#M203972</link>
    <description>&lt;P&gt;hello Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to get First.ID result with proc sql.&lt;/P&gt;&lt;P&gt;Please let me know.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data readin;
input ID Name $ Score;
cards;
1     David   45
1     David   74
2     Sam     45
2     Ram     54
3     Bane    87
3     Mary    92
3     Bane    87
4     Dane    23
5     Jenny   87
5     Ken     87
6     Simran  63
8     Priya   72
;
run;

PROC SORT DATA = READIN;
BY ID;
RUN;

DATA READIN1;
SET READIN;
BY ID;
First_ID= First.ID;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Expected output by proc sql method.&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ritesh_dellvostro_0-1597319016223.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/48192i82C2D84A2CB21E16/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ritesh_dellvostro_0-1597319016223.png" alt="Ritesh_dellvostro_0-1597319016223.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Aug 2020 11:44:16 GMT</pubDate>
    <dc:creator>Riteshdell</dc:creator>
    <dc:date>2020-08-13T11:44:16Z</dc:date>
    <item>
      <title>First.id result with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676454#M203972</link>
      <description>&lt;P&gt;hello Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to get First.ID result with proc sql.&lt;/P&gt;&lt;P&gt;Please let me know.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data readin;
input ID Name $ Score;
cards;
1     David   45
1     David   74
2     Sam     45
2     Ram     54
3     Bane    87
3     Mary    92
3     Bane    87
4     Dane    23
5     Jenny   87
5     Ken     87
6     Simran  63
8     Priya   72
;
run;

PROC SORT DATA = READIN;
BY ID;
RUN;

DATA READIN1;
SET READIN;
BY ID;
First_ID= First.ID;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Expected output by proc sql method.&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ritesh_dellvostro_0-1597319016223.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/48192i82C2D84A2CB21E16/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ritesh_dellvostro_0-1597319016223.png" alt="Ritesh_dellvostro_0-1597319016223.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 11:44:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676454#M203972</guid>
      <dc:creator>Riteshdell</dc:creator>
      <dc:date>2020-08-13T11:44:16Z</dc:date>
    </item>
    <item>
      <title>Re: First.id result with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676456#M203974</link>
      <description>&lt;P&gt;Not really.&amp;nbsp; SQL does not have a concept of row orders.&amp;nbsp; Newer versions of SQL had to invent a whole complex system of "windowing" functions to implement that.&amp;nbsp; But PROC SQL does not support those more modern constructs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you have ordering variable you can use to order the observations within the group?&amp;nbsp; Is it unique?&amp;nbsp; If so then use that.&lt;/P&gt;
&lt;P&gt;For example lets make one on your example data by adding a variable called ROW so we can demonstrate.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data readin_ordered;
  set readin;
  by id;
  if first.id then row=0;
  row+1;
run;

proc sql ;
create table want as
  select *,row=min(row) as first_id
  from readin_ordered
  group by id
  order by id,row
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    ID    Name      Score    row    first_id

  1     1    David       45      1         1
  2     1    David       74      2         0
  3     2    Sam         45      1         1
  4     2    Ram         54      2         0
  5     3    Bane        87      1         1
  6     3    Mary        92      2         0
  7     3    Bane        87      3         0
  8     4    Dane        23      1         1
  9     5    Jenny       87      1         1
 10     5    Ken         87      2         0
 11     6    Simran      63      1         1
 12     8    Priya       72      1         1&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Aug 2020 11:55:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676456#M203974</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-08-13T11:55:25Z</dc:date>
    </item>
    <item>
      <title>Re: First.id result with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676457#M203975</link>
      <description>&lt;P&gt;You can do it by using the undocumented MONOTONIC() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want (drop=mn) as
  select
    a.*,
    monotonic() as mn,
    case
      when calculated mn = min(calculated mn)
      then 1
      else 0
    end as first_id
  from readin a
  group by id
  order by id, mn
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bur compare this to the extremely simple and easy to understand data step code. Maxim 14: Use the Right Tool.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 11:56:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676457#M203975</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-13T11:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: First.id result with proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676464#M203978</link>
      <description>&lt;P&gt;Thank you ,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 12:25:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-id-result-with-proc-sql/m-p/676464#M203978</guid>
      <dc:creator>Riteshdell</dc:creator>
      <dc:date>2020-08-13T12:25:17Z</dc:date>
    </item>
  </channel>
</rss>

