<?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: How to select the first observation in each group, ordered by specific variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/702524#M215179</link>
    <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=f_agreg_mens_pos;
by pos_id c_estado descending c_cartao_sup;
run;

data want;
rank = 1;
set f_agreg_mens_pos;
by pos_id;
if first.pos_id;
keep
  rank    
  pos_id
  c_estado
  d_estado
  c_cartao_sup
  c_ndc_sup
  c_orgao_resp
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I read that code correctly, you want the top observation according to the sort order.&lt;/P&gt;</description>
    <pubDate>Thu, 30 Jun 2022 17:15:53 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-06-30T17:15:53Z</dc:date>
    <item>
      <title>How to select the first observation in each group, ordered by specific variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/701447#M214810</link>
      <description>&lt;PRE&gt;proc sql;
create table a as 
select rank,      
              pos_id,       
              c_estado,     
              d_estado,     
              c_cartao_sup, 
              c_ndc_sup,    
              c_orgao_resp 
from (select row_number() over (partition by pos_id order by c_estado asc, c_cartao_sup desc) as rank, 
                            pos_id,      
              c_estado,     
              d_estado,     
              c_cartao_sup, 
              c_ndc_sup,    
              c_orgao_resp 
            from  f_agreg_mens_pos)x
where rank=1;
run;&lt;/PRE&gt;
&lt;P&gt;Editor's Note: The title was changed to more generally reflect the goal.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 17:14:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/701447#M214810</guid>
      <dc:creator>Romeo1</dc:creator>
      <dc:date>2022-06-30T17:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert the code with row_number() over (partition by  ) in SAS Studio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/701451#M214813</link>
      <description>&lt;P&gt;See if you can use the 'translation' in the example below by converting to subqueries&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/43191746/sql-to-proc-sql-partition-by-alternative-min-case" target="_self"&gt;https://stackoverflow.com/questions/43191746/sql-to-proc-sql-partition-by-alternative-min-case&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Nov 2020 08:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/701451#M214813</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-25T08:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert the code with row_number() over (partition by  ) in SAS Studio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/702519#M215177</link>
      <description>&lt;P&gt;Hi, I can not&amp;nbsp;&lt;SPAN&gt;translate by converting to subqueries, it's confused. There is no way to do row_number() over (partition by ) in SAS Studio?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Nov 2020 15:10:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/702519#M215177</guid>
      <dc:creator>Romeo1</dc:creator>
      <dc:date>2020-11-30T15:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the first observation in each group, ordered by specific variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/702524#M215179</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=f_agreg_mens_pos;
by pos_id c_estado descending c_cartao_sup;
run;

data want;
rank = 1;
set f_agreg_mens_pos;
by pos_id;
if first.pos_id;
keep
  rank    
  pos_id
  c_estado
  d_estado
  c_cartao_sup
  c_ndc_sup
  c_orgao_resp
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I read that code correctly, you want the top observation according to the sort order.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 17:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-first-observation-in-each-group-ordered-by/m-p/702524#M215179</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-30T17:15:53Z</dc:date>
    </item>
  </channel>
</rss>

