<?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: Dedup  Code Convert to SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652679#M195983</link>
    <description>&lt;P&gt;Where is&amp;nbsp;wip.master_academic_hierarchy1 coming from? Oracle or SAS?&lt;/P&gt;</description>
    <pubDate>Tue, 02 Jun 2020 21:22:20 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2020-06-02T21:22:20Z</dc:date>
    <item>
      <title>Dedup  Code Convert to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652625#M195954</link>
      <description>&lt;P&gt;I'm converting SAS code to proc sql temporarily write to an Oracle schema rather than the work library.&amp;nbsp; I need to replicate the exact logic of the below.&amp;nbsp; I can't use distinct without additional logic because I need to prioritize rows that meet a criteria.&amp;nbsp; For example a row would look like&lt;/P&gt;
&lt;P&gt;col1&amp;nbsp; &amp;nbsp;clol2&amp;nbsp; &amp;nbsp; &amp;nbsp;priority col3&lt;/P&gt;
&lt;P&gt;apple orange 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cherry&lt;/P&gt;
&lt;P&gt;apple orange 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; strawberry&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there an Oracle Equivalent of:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data master_academic_hierarchy; set wip.master_academic_hierarchy1;
  if sorcmjr_adm_ind='Y' or sorcmjr_stu_ind='Y' then priority=1; else priority=2;
run;

/*dedup based on program*/
proc sort data=master_academic_hierarchy; by sobcurr_program priority descending sorcmjr_term_code_eff;
proc sort data=master_academic_hierarchy nodupkey; by sobcurr_program;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jun 2020 17:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652625#M195954</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2020-06-02T17:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Dedup  Code Convert to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652677#M195982</link>
      <description>&lt;P&gt;Presuming you are using pass through SQL.&amp;nbsp; You can use a CASE statement in your ORDER BY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;...
  ORDER BY 
    sobcurr_program 
    , case 
        when sorcmjr_adm_ind='Y' or sorcmjr_stu_ind='Y' then 1 else 2
      end descending
    , sorcmjr_term_code_eff&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might also need a select, hard to know what you fully want from the limited information&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;...
  SELECT 
...
    , case 
        when sorcmjr_adm_ind='Y' or sorcmjr_stu_ind='Y' then 1 else 2
      end as priority
...&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jun 2020 21:15:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652677#M195982</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-06-02T21:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: Dedup  Code Convert to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652679#M195983</link>
      <description>&lt;P&gt;Where is&amp;nbsp;wip.master_academic_hierarchy1 coming from? Oracle or SAS?&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jun 2020 21:22:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652679#M195983</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2020-06-02T21:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: Dedup  Code Convert to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652700#M195995</link>
      <description>&lt;P&gt;Assuming your source table is in Oracle using the row_number() analytics functions should get you very close to what the SAS code does (code not tested).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table master_academic_hierarchy as
SELECT * &lt;BR /&gt;FROM
  (
    SELECT 
      *,
      row_number() OVER(
          PARTITION BY 
            sobcurr_program
          ORDER BY 
            case when sorcmjr_adm_ind='Y' or sorcmjr_stu_ind='Y' then 1 else 2 end DESC, 
            sorcmjr_term_code_eff NULLS FIRST
      ) row_num
    FROM 
        master_academic_hierarchy1  
  )
WHERE row_num = 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jun 2020 23:21:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652700#M195995</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-02T23:21:35Z</dc:date>
    </item>
    <item>
      <title>Re: Dedup  Code Convert to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652852#M196075</link>
      <description>&lt;P&gt;It is coming from Oracle.&amp;nbsp; I'm trying to avoid using the work library.&amp;nbsp; Currently testing the posted code.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 12:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652852#M196075</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2020-06-03T12:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: Dedup  Code Convert to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652857#M196076</link>
      <description>&lt;P&gt;Unfortunately, this wouldn't prioritize selected rows first.&amp;nbsp; This would only select filtered rows first.&amp;nbsp; Hence it is not depuping logic.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 13:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652857#M196076</guid>
      <dc:creator>DavidPhillips2</dc:creator>
      <dc:date>2020-06-03T13:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: Dedup  Code Convert to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652890#M196084</link>
      <description>&lt;P&gt;Once you get the rownum can you not filter the rownum on the minimum for each group?&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 14:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652890#M196084</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-03T14:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: Dedup  Code Convert to SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652907#M196090</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/5059"&gt;@DavidPhillips2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Unfortunately, this wouldn't prioritize selected rows first.&amp;nbsp; This would only select filtered rows first.&amp;nbsp; Hence it is not depuping logic.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I believe you're missing something. This SQL should return the same result than your combination of two Proc Sorts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first Proc Sort orders the rows by multiple variables.&lt;/P&gt;
&lt;P&gt;proc sort data=master_academic_hierarchy; by &lt;STRONG&gt;sobcurr_program&lt;/STRONG&gt; &lt;FONT color="#000080"&gt;priority descending sorcmjr_term_code_eff&lt;/FONT&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SQL creates a new column Row_Num that re-starts with 1 for every new value of&amp;nbsp;&lt;STRONG&gt;sobcurr_program.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Within a group (a partition) the row count is along the sort order of&amp;nbsp;&lt;FONT color="#000080"&gt;priority descending sorcmjr_term_code_eff&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;- the first row after the sort will have row_num=1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your 2nd Proc Sort selects the first record for every new value of&amp;nbsp;&lt;STRONG&gt;sobcurr_program&lt;/STRONG&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;- and because the SAS table doesn't get re-sorted it's the first row from your first more granular sort.&lt;/P&gt;
&lt;P&gt;The SQL select the rows&amp;nbsp;&lt;STRONG&gt;WHERE row_num = 1&lt;/STRONG&gt; - that's pretty much the same than what the 2nd Proc Sort with NODUPKEY does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result of the two Proc Sorts and the SQL will only differ if sorting by&lt;FONT color="#000080"&gt;&amp;nbsp;&lt;EM&gt;sobcurr_program priority descending sorcmjr_term_code_eff &lt;/EM&gt;&lt;/FONT&gt;can still have multiple rows within the same value for&amp;nbsp;&lt;FONT color="#000080"&gt;&lt;EM&gt;sorcmjr_term_code_eff&lt;/EM&gt;&lt;/FONT&gt;&amp;nbsp;. Due to the sequential nature of SAS data processing the result for the same source table will always be the same but with SQL the row order within a tuple is not deterministic so it can vary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jun 2020 15:29:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dedup-Code-Convert-to-SQL/m-p/652907#M196090</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-03T15:29:17Z</dc:date>
    </item>
  </channel>
</rss>

