<?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: select max from character variables between 2 tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370323#M275714</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/149564"&gt;@teddyee&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Below code creates your desired outcome.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_a;
  infile datalines truncover dlm=' ' dsd;
  input customer $ date:anydtdte. filter $;
  format date date9.;
  datalines;
s8889b 1/2/2017 N
S9990n 2/2/2017 A
T6789p  Y
Z1234 4/2/2017 
;
run;

data table_b;
  infile datalines truncover dlm=' ' dsd;
  input customer $ date:anydtdte. filter $;
  format date date9.;
  datalines;
s8889b 1/1/2017 Y
S9990n 2/2/2017 N
T6789p 3/2/2017 N
Z1234 
;
run;

proc sql;
  create table want as
    select
      coalesce(a.customer,b.customer) as customer,
      max(a.date,b.date) as date format=date9.,
      byte(max(rank(a.filter),rank(b.filter))) as filter
    from 
      table_a as a
        full outer join
      table_b as b
      on 
        a.customer=b.customer
    ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please post in the future fully working data steps creating your sample data so we don't have to do this for you.&lt;/P&gt;</description>
    <pubDate>Sun, 25 Jun 2017 01:05:32 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2017-06-25T01:05:32Z</dc:date>
    <item>
      <title>select max from character variables between 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370286#M275711</link>
      <description>&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;table a&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;customer &amp;nbsp; date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;filter&lt;BR /&gt;s8889b &amp;nbsp; 1/2/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; N&lt;BR /&gt;S9990n &amp;nbsp; 2/2/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; A&lt;BR /&gt;T6789p &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Y&lt;BR /&gt;Z1234 &amp;nbsp; &amp;nbsp; 4/2/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;table b&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;customer &amp;nbsp; &amp;nbsp;date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;filter&lt;BR /&gt;&lt;SPAN&gt;s8889b&lt;/SPAN&gt; &amp;nbsp; &amp;nbsp;1/1/2017 &amp;nbsp; &amp;nbsp;Y&lt;BR /&gt;&lt;SPAN&gt;S9990n&lt;/SPAN&gt; &amp;nbsp; &amp;nbsp;2/2/2017 &amp;nbsp; N&lt;BR /&gt;&lt;SPAN&gt;T6789p&lt;/SPAN&gt; &amp;nbsp; &amp;nbsp;3/2/2017 &amp;nbsp; N&lt;BR /&gt;Z1234 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;&lt;U&gt;&lt;BR /&gt;&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;U&gt;desired outcome&amp;nbsp;&lt;/U&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;customer &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;filter&lt;/P&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&lt;SPAN&gt;s8889b &amp;nbsp; &amp;nbsp; &amp;nbsp; 1/2/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Y&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;S9990n &amp;nbsp; &amp;nbsp; &amp;nbsp; 2/2/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; N&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;T6789p &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/2/2017 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;Y&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Z1234 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4/2/2017&lt;/SPAN&gt; &amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;i am not able to get&amp;nbsp;the filter column in the desired&amp;nbsp;outcome, where Y&amp;gt;N&amp;gt;A&amp;gt;null (by alphabetical order), with my code below....&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;proc sql;&lt;BR /&gt;create table final as&lt;BR /&gt;select a.customer,&lt;BR /&gt;max(a.filter, b.filter) as filter_new -------------------------------&amp;gt; how should i change this line to achieve my desired&amp;nbsp;outcome?&lt;BR /&gt;case when coalesce(a.date,b.date) is not null&lt;BR /&gt;then max(a.date,b.date)&lt;BR /&gt;end as date_new format date9.&lt;BR /&gt;from work.cards a&lt;BR /&gt;left join work.cp b on a.customer=b.customer;&lt;BR /&gt;quit;&lt;/DIV&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 24 Jun 2017 17:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370286#M275711</guid>
      <dc:creator>teddyee</dc:creator>
      <dc:date>2017-06-24T17:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: select max from character variables between 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370288#M275712</link>
      <description>&lt;P&gt;Have you tried an UPDATE statement?&lt;/P&gt;</description>
      <pubDate>Sat, 24 Jun 2017 17:52:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370288#M275712</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-06-24T17:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: select max from character variables between 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370292#M275713</link>
      <description>&lt;P&gt;Please explain what it is you want to happen. First just use words and then use concrete examples of specific cases.&lt;/P&gt;
&lt;P&gt;For example in your posted data there is a CUSTOMER '&lt;SPAN&gt;s8889b' that in A has DATE='02JAN2017'd and FILTER='N" and in B has DATE='01JAN2017'd and FILTER='Y'. &amp;nbsp;But in the output you have taken the DATE from A and FILTER from B. &amp;nbsp;Why?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Did you want to have the latest&amp;nbsp;date&amp;nbsp;and the maximum filter?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As to your code make sure that you are clear on whether you want to use the MAX() aggregate function of SQL (which works across observations) or the SAS function MAX(,) which takes the maximum of a list of two or more individual values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If you just want to stack up the data and take the max per customer then use a sub-query to stack (union) the data.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint ;
  create table want as
  select customer
       , max(date) as date format=yymmdd10.
       , max(filter) as filter
  from (
    select * from A
    union
    select * from B
        )
   group by 1
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Jun 2017 18:18:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370292#M275713</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-24T18:18:49Z</dc:date>
    </item>
    <item>
      <title>Re: select max from character variables between 2 tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370323#M275714</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/149564"&gt;@teddyee&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Below code creates your desired outcome.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_a;
  infile datalines truncover dlm=' ' dsd;
  input customer $ date:anydtdte. filter $;
  format date date9.;
  datalines;
s8889b 1/2/2017 N
S9990n 2/2/2017 A
T6789p  Y
Z1234 4/2/2017 
;
run;

data table_b;
  infile datalines truncover dlm=' ' dsd;
  input customer $ date:anydtdte. filter $;
  format date date9.;
  datalines;
s8889b 1/1/2017 Y
S9990n 2/2/2017 N
T6789p 3/2/2017 N
Z1234 
;
run;

proc sql;
  create table want as
    select
      coalesce(a.customer,b.customer) as customer,
      max(a.date,b.date) as date format=date9.,
      byte(max(rank(a.filter),rank(b.filter))) as filter
    from 
      table_a as a
        full outer join
      table_b as b
      on 
        a.customer=b.customer
    ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please post in the future fully working data steps creating your sample data so we don't have to do this for you.&lt;/P&gt;</description>
      <pubDate>Sun, 25 Jun 2017 01:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-max-from-character-variables-between-2-tables/m-p/370323#M275714</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-06-25T01:05:32Z</dc:date>
    </item>
  </channel>
</rss>

