<?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: Compare two char variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604741#M175353</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/44053"&gt;@LineMoon&lt;/a&gt;: This is what the COMPARE function is for:&lt;/P&gt;
&lt;PRE&gt;data want ;                                                                                                                             
  if _n_ = 1 then do ;                                                                                                                  
    if 0 then set big ref ;                                                                                                             
    dcl hash h (dataset:"ref") ;                                                                                                        
    h.definekey ("var_ref") ;                                                                                                           
    h.definedone () ;                                                                                                                   
  end ;                                                                                                                                 
  set big ;                                                                                                                             
  common_values = var ;                                                                                                                 
  do _n_ = 1 to countw (var) ;                                                                                                          
    common_values = scan (var, _n_) ;                                                                                                   
    if h.find (key:common_values) then continue ;                                                                                       
    &lt;FONT color="#0000FF"&gt;order_first_char_with_dif = abs (compare (var_ref, var)) ;&lt;/FONT&gt;                                                                          
    output ;                                                                                                                            
  end ;                                                                                                                                 
  if h.find (key:var) = 0 then do ;                                                                                                     
    common_values = "equal" ;                                                                                                           
    &lt;FONT color="#0000FF"&gt;order_first_char_with_dif = 0 ; &lt;/FONT&gt;                                                                                                    
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ;                  
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you want the records in the output listed in the order exactly as you originally suggested, just swap the execution order around:&lt;/P&gt;
&lt;PRE&gt;data want ;                                                                                                                             
  if _n_ = 1 then do ;                                                                                                                  
    if 0 then set big ref ;                                                                                                             
    dcl hash h (dataset:"ref") ;                                                                                                        
    h.definekey ("var_ref") ;                                                                                                           
    h.definedone () ;                                                                                                                   
  end ;                                                                                                                                 
  set big ;                                                                                                                             
  common_values = var ;                                                                                                                 
  common_values = "equal" ;                                                                                                             
  order_first_char_with_dif = 0 ;                                                                                                       
  if h.find (key:var) = 0 then output ;                                                                                                 
  do _n_ = countw (var) to 1 by -1 ;                                                                                                    
    common_values = scan (var, _n_) ;                                                                                                   
    if h.find (key:common_values) ne 0 then continue ;                                                                                       
    order_first_char_with_dif = abs (compare (var_ref, var)) ;                                                                          
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ;         
&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 16 Nov 2019 20:19:06 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-11-16T20:19:06Z</dc:date>
    <item>
      <title>Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/603987#M175047</link>
      <description>&lt;P&gt;Hello Experts,&lt;/P&gt;
&lt;P&gt;I have two variables in two tables.&lt;/P&gt;
&lt;P&gt;The first variable, Var, in the first table , it is a big table.&lt;/P&gt;
&lt;P&gt;The second variable, var_ref, in the second table (reference table) , it is a small table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output Rules:&lt;/P&gt;
&lt;P&gt;If var =var_ref then CommonValue='equal'&lt;/P&gt;
&lt;P&gt;if there is a common values then CommonValue = "common values betwwen var and var_ref"&lt;/P&gt;
&lt;P&gt;if not&amp;nbsp;CommonValue=''&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;gt; Big Table&lt;/STRONG&gt;&lt;/P&gt;
&lt;TABLE width="162"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="162"&gt;Var&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;First&amp;nbsp; one&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Test number one&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;gt; Ref table&lt;/STRONG&gt;&lt;/P&gt;
&lt;TABLE width="183"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="183"&gt;Var_Ref&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;one&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;First one&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Test&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;number&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;gt; Output&lt;/STRONG&gt;&lt;/P&gt;
&lt;TABLE width="425"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="162"&gt;Var&lt;/TD&gt;
&lt;TD width="80"&gt;Var_Ref&lt;/TD&gt;
&lt;TD width="183"&gt;Common&amp;nbsp; Values&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;First&amp;nbsp; one&lt;/TD&gt;
&lt;TD&gt;one&lt;/TD&gt;
&lt;TD&gt;one&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;First&amp;nbsp; one&lt;/TD&gt;
&lt;TD&gt;First one&lt;/TD&gt;
&lt;TD&gt;equal&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Test number one&lt;/TD&gt;
&lt;TD&gt;one&lt;/TD&gt;
&lt;TD&gt;one&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Test number one&lt;/TD&gt;
&lt;TD&gt;number&lt;/TD&gt;
&lt;TD&gt;number&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Test number one&lt;/TD&gt;
&lt;TD&gt;Test&lt;/TD&gt;
&lt;TD&gt;Test&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Wed, 13 Nov 2019 21:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/603987#M175047</guid>
      <dc:creator>LineMoon</dc:creator>
      <dc:date>2019-11-13T21:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/603991#M175049</link>
      <description>&lt;P&gt;None of those values are EQUAL.&amp;nbsp; What is the test that you actually want to do?&amp;nbsp; Are you trying to test if VAR_REF is contained in VAR? Does case matter?&amp;nbsp; Does it need to match a full word?&amp;nbsp; Does VAR_REF ever contain more than one word?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 22:21:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/603991#M175049</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-13T22:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/603992#M175050</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;I want to test as described in the output&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If the value of&amp;nbsp; "VAR" is contained in VAR_Ref?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Does it&amp;nbsp; contain at least one word in VAR_Ref ?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;or if does not contain any word in in VAR_Ref ?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2019 22:36:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/603992#M175050</guid>
      <dc:creator>LineMoon</dc:creator>
      <dc:date>2019-11-13T22:36:08Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/603999#M175052</link>
      <description>&lt;P&gt;i'm certain there are at least 5 better ways of doing this but this may do what you're after:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;options mlogic symbolgen;
data ref_table;
    infile datalines dlm=",";
    format var $50.;
    input var;
    datalines;
Var_Ref,
one,
First one,
Test,
number,
    ;
run;

data big_table;
    infile datalines dlm=",";
    format var $50.;
    input var;
    datalines;
Var,
First one,
Test number one,
;
run;

proc sql;
    create table distinct_varlist as
        select distinct(strip(var)) as ref_var
        from ref_table;
quit;

proc sql; 
    create table Big_table_distinct as 
        select distinct var 
        from big_table; 
quit;

proc sql noprint; select count(ref_var) into: distinct_varcount from distinct_varlist;

%macro iterative_check;

    %do i = 1 %to &amp;amp;distinct_varcount.;

        data _null_;
            p = &amp;amp;i.;
            set distinct_varlist point=p;
            call symputx('varlength',length(ref_var),'L');
            call symputx('ref_var_m',ref_var,'L');
            stop;
        run;

        data setall_&amp;amp;i.(drop=CV:);
            set Big_table_distinct;
                BigTable_Var = strip(Var);
                RefTable_Var = strip("&amp;amp;ref_var_m.");

             if BigTable_Var = RefTable_Var         then CV1 = "Equal";
             if indexw(BigTable_Var,RefTable_Var)   then CV2 = strip(substr(BigTable_Var,indexw(BigTable_Var,RefTable_Var),index(BigTable_Var,' ')));
             if indexw(RefTable_Var,BigTable_Var)   then CV3 = strip(substr(RefTable_Var,indexw(RefTable_Var,BigTable_Var),index(RefTable_Var,' ')+1));
             format Common_Values $50.;
                    Common_Values = coalescec(CV1,CV2,CV3);
        run;

    %end;
%mend iterative_check;
%iterative_check;

data combine;
    set setall:;
run;

proc sort nodupkey data=combine; by BigTable_Var RefTable_Var Common_Values;

data output;
    set combine;
        where Common_Values ne '';
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Nov 2019 01:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/603999#M175052</guid>
      <dc:creator>jarg</dc:creator>
      <dc:date>2019-11-14T01:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604013#M175059</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/44053"&gt;@LineMoon&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;A modicum of hash object usage should do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data big ;                                                                                                                              
  input @1 var $16. ;                                                                                                                   
  cards ;                                                                                                                               
first one                                                                                                                               
test number one                                                                                                                         
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data ref ;                                                                                                                              
  input @1 var_ref $16. ;                                                                                                               
  cards ;                                                                                                                               
one                                                                                                                                     
first one                                                                                                                               
test                                                                                                                                    
number                                                                                                                                  
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want ;                                                                                                                             
  if _n_ = 1 then do ;                                                                                                                  
    if 0 then set big ref ;                                                                                                             
    dcl hash h (dataset:"ref") ;                                                                                                        
    h.definekey ("var_ref") ;                                                                                                           
    h.definedone () ;                                                                                                                   
  end ;                                                                                                                                 
  set big ;                                                                                                                             
  common_values = var ;                                                                                                                 
  if h.find (key:var) = 0 then do ;                                                                                                     
    common_values = "equal" ;                                                                                                           
    output ;                                                                                                                            
  end ;                                                                                                                                 
  do _n_ = 1 to countw (var) ;                                                                                                          
    common_values = scan (var, _n_) ;                                                                                                   
    if h.find (key:common_values) = 0 then output ;                                                                                    
  end ;                                                                                                                                 
run ;                                            
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2019 04:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604013#M175059</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-14T04:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604279#M175172</link>
      <description>&lt;P&gt;Thank you very much for your answer, That's very kind from you&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2019 20:46:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604279#M175172</guid>
      <dc:creator>LineMoon</dc:creator>
      <dc:date>2019-11-14T20:46:42Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604332#M175202</link>
      <description>&lt;P&gt;Thank very much. that's very kind from you.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2019 22:28:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604332#M175202</guid>
      <dc:creator>LineMoon</dc:creator>
      <dc:date>2019-11-14T22:28:10Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604346#M175209</link>
      <description>this is an elegant solution, it's probably about time i wrap my head around hash objects. kudos!</description>
      <pubDate>Thu, 14 Nov 2019 23:48:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604346#M175209</guid>
      <dc:creator>jarg</dc:creator>
      <dc:date>2019-11-14T23:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604369#M175220</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/297082"&gt;@jarg&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;The hash object can make many things more elegant, simpler, and faster. That's why &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13569"&gt;@DonH&lt;/a&gt;&amp;nbsp;and I have written a whole book about it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.sas.com/store/books/categories/examples/data-management-solutions-using-sas-hash-table-operations-a-business-intelligence-case-study/prodBK_69153_en.html" target="_self"&gt;https://www.sas.com/store/books/categories/examples/data-management-solutions-using-sas-hash-table-operations-a-business-intelligence-case-study/prodBK_69153_en.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Though in this case, the concept of the program is quite simple:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;store VAR_REF from REF in a lookup table&lt;/LI&gt;
&lt;LI&gt;for each record from BIG:&lt;/LI&gt;
&lt;LI&gt;search VAR in the table, and if found, output as "equal"&lt;/LI&gt;
&lt;LI&gt;then search each word in VAR in the table, and if found, output it and the corresponding VAR_REF&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The concept would remain the same if a different type of lookup table were used. However, the hash table makes the process of organizing the lookup table and searching it a breeze - and searches in O(1) time to boot. I agree that it behooves any SAS programmer to make the hash object, to a lesser or larger degree, an instrument in one's toolbox. After all, it's been an integral part of Base SAS for 16 years and counting! &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Nov 2019 01:34:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604369#M175220</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-15T01:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604391#M175227</link>
      <description>i see i'm speaking to a resident expert then! shouldn't have expected anything less from the Hashman in hindsight.&lt;BR /&gt;&lt;BR /&gt;thanks for the explanation, i was sure there'd be a more compact solution but wasn't expecting it all to be done in a singe step. i've been meaning to look into hashes as i'm working with some larger tables these days than i'm used to and need some superior merging methods.&lt;BR /&gt;&lt;BR /&gt;i'd say you've just sold another copy of your book</description>
      <pubDate>Fri, 15 Nov 2019 05:14:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604391#M175227</guid>
      <dc:creator>jarg</dc:creator>
      <dc:date>2019-11-15T05:14:44Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604397#M175230</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/297082"&gt;@jarg&lt;/a&gt;:That's good. The more, the merrier &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; ;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Nov 2019 06:28:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604397#M175230</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-15T06:28:22Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604688#M175328</link>
      <description>&lt;P&gt;Please, is -it possible to show, the order of the first char there is a difference betwwen var and var_ref&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Var=test one&lt;/P&gt;
&lt;P&gt;Var_ref=test two&lt;/P&gt;
&lt;P&gt;order_first_char_with_dif=5&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2019 12:02:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604688#M175328</guid>
      <dc:creator>LineMoon</dc:creator>
      <dc:date>2019-11-16T12:02:57Z</dc:date>
    </item>
    <item>
      <title>Re: Compare two char variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604741#M175353</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/44053"&gt;@LineMoon&lt;/a&gt;: This is what the COMPARE function is for:&lt;/P&gt;
&lt;PRE&gt;data want ;                                                                                                                             
  if _n_ = 1 then do ;                                                                                                                  
    if 0 then set big ref ;                                                                                                             
    dcl hash h (dataset:"ref") ;                                                                                                        
    h.definekey ("var_ref") ;                                                                                                           
    h.definedone () ;                                                                                                                   
  end ;                                                                                                                                 
  set big ;                                                                                                                             
  common_values = var ;                                                                                                                 
  do _n_ = 1 to countw (var) ;                                                                                                          
    common_values = scan (var, _n_) ;                                                                                                   
    if h.find (key:common_values) then continue ;                                                                                       
    &lt;FONT color="#0000FF"&gt;order_first_char_with_dif = abs (compare (var_ref, var)) ;&lt;/FONT&gt;                                                                          
    output ;                                                                                                                            
  end ;                                                                                                                                 
  if h.find (key:var) = 0 then do ;                                                                                                     
    common_values = "equal" ;                                                                                                           
    &lt;FONT color="#0000FF"&gt;order_first_char_with_dif = 0 ; &lt;/FONT&gt;                                                                                                    
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ;                  
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you want the records in the output listed in the order exactly as you originally suggested, just swap the execution order around:&lt;/P&gt;
&lt;PRE&gt;data want ;                                                                                                                             
  if _n_ = 1 then do ;                                                                                                                  
    if 0 then set big ref ;                                                                                                             
    dcl hash h (dataset:"ref") ;                                                                                                        
    h.definekey ("var_ref") ;                                                                                                           
    h.definedone () ;                                                                                                                   
  end ;                                                                                                                                 
  set big ;                                                                                                                             
  common_values = var ;                                                                                                                 
  common_values = "equal" ;                                                                                                             
  order_first_char_with_dif = 0 ;                                                                                                       
  if h.find (key:var) = 0 then output ;                                                                                                 
  do _n_ = countw (var) to 1 by -1 ;                                                                                                    
    common_values = scan (var, _n_) ;                                                                                                   
    if h.find (key:common_values) ne 0 then continue ;                                                                                       
    order_first_char_with_dif = abs (compare (var_ref, var)) ;                                                                          
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ;         
&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2019 20:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-two-char-variables/m-p/604741#M175353</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-16T20:19:06Z</dc:date>
    </item>
  </channel>
</rss>

