<?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: Unusual String Comparison Problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169904#M32625</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Okay, I think I got it. The condition that must be included into the solution is that the length(COL1)&amp;gt;=length(COL2). This is easy to add in.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What is harder to adjust for is that if the two strings match (e.g. if COL1='AA999A' and COL2='AA999A'), then the COMPARE function gives us a zero as an answer, and so the MAX function in &lt;A __default_attr="645292" __jive_macro_name="user" class="jive_macro jive_macro_user" href="https://communities.sas.com/"&gt;&lt;/A&gt;'s solution doesn't handle this case.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, we create a function of the COL1 and COL2 values that turns the zero into a large number.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;proc sql;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;create table want as&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;select * from a,b group by col1 having&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;((compare(col1,col2,'il')&amp;gt;0)*compare(col1,col2,'il') + (compare(col1,col2,'il')=0)*999) =&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;max(((compare(col1,col2,'il')&amp;gt;0)*compare(col1,col2,'il') + (compare(col1,col2,'il')=0)*999))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;and length(col1)&amp;gt;=length(col2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;So zeros get turned into 999 while the other results from COMPARE remain unchanged.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Here is the correct data set WANT:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Obs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COL1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COL2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999A&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999AAB&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999A&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999ABB&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999AB&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Anyone got a simpler solution?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 13 Nov 2014 16:40:43 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2014-11-13T16:40:43Z</dc:date>
    <item>
      <title>Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169894#M32615</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Suppose I have a character string in SAS variable named COL1, and other character strings in a SAS variable named COL2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I wanted to, for example, match the first five characters of COL1 with the first five characters of COL2, this is easy to do in a datastep or SQL. But my problem today is that I want to match the string in COL1 with the string in COL2 that has the most characters matching (and the match must be consecutive, and begin in position 1 of the string)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, for example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;COL1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COL2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999AAB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999ABB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999A&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999AB&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The string in COL2 that has the longest match to AA999AAB in COL1 is AA999A (&lt;EM&gt;i.e.&lt;/EM&gt; AA999 in COL2 is not the match I want)&lt;/P&gt;&lt;P&gt;The string in COL2 that has the longest match to AA999ABB in COL1 is AA999AB (&lt;EM&gt;i.e.&lt;/EM&gt; AA999 and AA999A is not the match I want)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and so on&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How can I do this? I have written a very convoluted looping program to get this done, but I'm hoping there is a smarter and more efficient way, using any of: data step, SQL, regular expressions, IML, whatever.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 14:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169894#M32615</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T14:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169895#M32616</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well, there is the soundex function which codes a word up.&amp;nbsp; Not sure how close though.&amp;nbsp; Am also thinking of arrays but puzzling over a simple technique.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 14:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169895#M32616</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-11-13T14:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169896#M32617</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't have an immediate answer, but SAS has some "distance between words" functions that might help. I discuss them in this article:&lt;/P&gt;&lt;P&gt;&lt;A href="http://blogs.sas.com/content/iml/2011/10/05/distances-between-words/"&gt;http://blogs.sas.com/content/iml/2011/10/05/distances-between-words/&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In the article I used the SPEDIS function, which is apparently not what you want. You want a function that "rewards" exact matches at the beginning of a word.&amp;nbsp; Nevertheless, here is some code that shows how to use the SPEDIS function. Perhaps it will give you a place to start. If none of the functions in the article perform the match you want, then you will have to write the function "cost function" yourself.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data A;&lt;BR /&gt;input COL1 $12.;&lt;BR /&gt;datalines;&lt;BR /&gt;AA999AAB&lt;BR /&gt;AA999ABB&lt;BR /&gt;;&lt;BR /&gt;data B;&lt;BR /&gt;input COL2 $12.;&lt;BR /&gt;datalines;&lt;BR /&gt;AA999&lt;BR /&gt;AA999A&lt;BR /&gt;AA999B&lt;BR /&gt;AA999AB&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc iml;&lt;/P&gt;&lt;P&gt;use A; read all var {col1} into x; close A;&lt;BR /&gt;use B; read all var {col2} into y; close B;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;cost = j(nrow(x), nrow(y),.);&lt;BR /&gt;do i = 1 to nrow(x);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; word = x&lt;I&gt;;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; cost[i,] = spedis(y`, word);&lt;BR /&gt;end;&lt;BR /&gt;print cost[rowname=x colname=y];&lt;/I&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169896#M32617</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2014-11-13T15:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169897#M32618</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How about this one :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;

data a;
input COL1&amp;nbsp; : $20.;
cards;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
AA999AAB&amp;nbsp; 
AA999ABB 
;
run; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 

data b;
input COL2&amp;nbsp; : $20.;
cards;&amp;nbsp; 
AA999
AA999A
AA999B
AA999AB
;
run; 
proc sql;
create table want as
 select * from a,b group by col1 having compare(col1,col2,'il')=max(compare(col1,col2,'il'));
quit;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169897#M32618</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2014-11-13T15:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169898#M32619</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Rick,&lt;/P&gt;&lt;P&gt;OP asked the first N character matched , not the minimize spell distance . COMPARE() might be a good choice.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:07:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169898#M32619</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2014-11-13T15:07:01Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169899#M32620</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;DING DING DING DING!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Looks like &lt;A __default_attr="645292" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt; has the answer! That's amazing in its simplicity. Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169899#M32620</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T15:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169900#M32621</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;One thing need to remind : Is the length of COL1 is always greater than the length of COL2 ? otherwise ,you need to add abs() around it .&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169900#M32621</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2014-11-13T15:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169901#M32622</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ok, I was just thinking that COL1 length could be the same as COL2 length, but your statement "you need to add abs() around it" is somewhat vague ... add abs() around what?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Specifically, if COL1 also contains AA999A, I would want it to match AA999A in COL2, and because COMPARE returns a 0 when the strings are identical, the SQL needs to be modified for this case, but specifically how?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169901#M32622</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T15:23:22Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169902#M32623</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK.Check this example . the output is what you need ?&lt;/P&gt;&lt;P&gt;I have to go to sleep now. Bye.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data a;&lt;/P&gt;&lt;P&gt;input COL1&amp;nbsp; : $20.;&lt;/P&gt;&lt;P&gt;cards;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;AA999AAB &lt;/P&gt;&lt;P&gt;AA999ABB&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data b;&lt;/P&gt;&lt;P&gt;input COL2&amp;nbsp; : $20.;&lt;/P&gt;&lt;P&gt;cards; &lt;/P&gt;&lt;P&gt;AA999&lt;/P&gt;&lt;P&gt;AA999A&lt;/P&gt;&lt;P&gt;AA999B&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;AA999ABASD&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table want as&lt;/P&gt;&lt;P&gt;select * from a,b group by col1 having compare(col1,col2,'il')=max(compare(col1,col2,'il'));&lt;/P&gt;&lt;P&gt;quit;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: xia keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169902#M32623</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2014-11-13T15:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169903#M32624</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No that's the wrong result, AA999ABB in COL1 should match AA999A in COL2 in this case. COL2 will be shorter than or the same length as COL1, I don't think in my situation COL2 can be longer than COL1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But specifically, if the code is&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data a;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;input COL1&amp;nbsp; : $20.;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;cards;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999AAB&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999ABB &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999A&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;run; &lt;/SPAN&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;data b;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;input COL2&amp;nbsp; : $20.;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;cards;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999A&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999B&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;AA999AB&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note: I have added AA999A to COL1, then the result I want is AA999AAB and AA999ABB are matched properly by your first code example, but AA999A in COL1 is matched to AA999 in COL2 using your first code example, and I want it matched to AA999A in COL2.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 15:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169903#M32624</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T15:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169904#M32625</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Okay, I think I got it. The condition that must be included into the solution is that the length(COL1)&amp;gt;=length(COL2). This is easy to add in.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What is harder to adjust for is that if the two strings match (e.g. if COL1='AA999A' and COL2='AA999A'), then the COMPARE function gives us a zero as an answer, and so the MAX function in &lt;A __default_attr="645292" __jive_macro_name="user" class="jive_macro jive_macro_user" href="https://communities.sas.com/"&gt;&lt;/A&gt;'s solution doesn't handle this case.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, we create a function of the COL1 and COL2 values that turns the zero into a large number.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;proc sql;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;create table want as&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;select * from a,b group by col1 having&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;((compare(col1,col2,'il')&amp;gt;0)*compare(col1,col2,'il') + (compare(col1,col2,'il')=0)*999) =&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;max(((compare(col1,col2,'il')&amp;gt;0)*compare(col1,col2,'il') + (compare(col1,col2,'il')=0)*999))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;and length(col1)&amp;gt;=length(col2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;So zeros get turned into 999 while the other results from COMPARE remain unchanged.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Here is the correct data set WANT:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Obs&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COL1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; COL2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999A&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999A&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999AAB&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999A&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-family: courier new,courier;"&gt; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999ABB&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999AB&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial,helvetica,sans-serif;"&gt;Anyone got a simpler solution?&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 16:40:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169904#M32625</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T16:40:43Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169905#M32626</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Paige,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I liked &lt;A __default_attr="645292" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt;'s solution, but it didn't work on the extra test cases I added. The compare function returns a 0 if there is an exact match, otherwise it returns the first character that is different.&amp;nbsp; As such, here is my proposed solution:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input (COL1&amp;nbsp; COL2) ($);&lt;/P&gt;&lt;P&gt;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;AA999AAB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999&lt;/P&gt;&lt;P&gt;AA999ABB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999A&lt;/P&gt;&lt;P&gt;BB999AAB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999B&lt;/P&gt;&lt;P&gt;.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999AA&lt;/P&gt;&lt;P&gt;.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999AB&lt;/P&gt;&lt;P&gt;.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AA999ABB&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data col1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have (keep=col1 where=(not missing(col1)));&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data col2;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have (keep=col2);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table want as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select *,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case compare(col1,col2,'il')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when 0 then length(col1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else compare(col1,col2,'il')-1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end as length&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from col1,col2 &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; group by col1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having (calculated length&amp;gt;0) and&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; calculated length=max(calculated length)&lt;/P&gt;&lt;P&gt;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169905#M32626</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2014-11-13T17:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169906#M32627</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your requirement still seems a little vague to me, will COL2 have to inclusive to COL1 if matched? Will they always compare from the first char? If both Yes, then try the following:&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-family: 'Courier New';"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; a;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; COL1&amp;nbsp; : &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: teal; background: white;"&gt;$20.&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;cards&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;AA999AAB&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;AA999ABB &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;AA999A&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-family: 'Courier New';"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-family: 'Courier New';"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; b;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; COL2&amp;nbsp; : &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: teal; background: white;"&gt;$20.&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;cards&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;AA999&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;AA999A&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;AA999B&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;AA999AB&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-family: 'Courier New';"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="color: navy; background: white; font-family: 'Courier New';"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN style="color: navy; background: white; font-family: 'Courier New';"&gt;&lt;STRONG&gt;sql&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;create&lt;/SPAN&gt; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;table&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; want &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;as&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;select&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; col1, (&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;select distinct&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; col2 &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;from&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; b &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;where&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; col1 eqt trim(col2) &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;having&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; length(col2) = max(length(col2))) &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;as&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; col2&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;from&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; a;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: navy; background: white; font-family: 'Courier New';"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;Good luck,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;Haikuo &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169906#M32627</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2014-11-13T17:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169907#M32628</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Lack of SQL skills would force me to take a totally different approach.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Step 1:&amp;nbsp; Create a format that translates from the value of COL2 to the observation number holding that value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Step 2:&amp;nbsp; Loop through COL1 (all characters down to 1 character) until you find a match. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, hard-coding the format just for readability step 1 would look like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc format;&lt;BR /&gt;value $recno&lt;/P&gt;&lt;P&gt;'AA999'='1'&lt;/P&gt;&lt;P&gt;'AA999A'='2'&lt;/P&gt;&lt;P&gt;'AA999B'='3'&lt;/P&gt;&lt;P&gt;'AA999AB'='4'&lt;/P&gt;&lt;P&gt;other='Not Found';&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In practice, I'm sure you would want to switch to a CNTLIN= data set.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then step 2 uses the format:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set col1_source;&lt;/P&gt;&lt;P&gt;if COL1 &amp;gt; ' ' then do _n_=length(COL1) to 1 by -1 until (result ne 'Not Found');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; result = put(substr(COL1, 1, _n_), $recno.);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;if result ne 'Not Found' then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; recno = input(result, 8.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set col2_dataset point=recno;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's sophisticated, but not horribly complex.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:23:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169907#M32628</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2014-11-13T17:23:01Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169908#M32629</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE __jive_macro_name="quote" class="jive_text_macro jive_macro_quote"&gt;
&lt;P&gt;Your requirement still seems a little vague to me, will COL2 have to inclusive to COL1 if matched?&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here are the requirements. The desired output is when COL1 and COL2 have the same characters in positions 1 through n, where n is the maximum value of all comparisons of each COL1 with all COL2; and length(COL1)&amp;gt;=length(COL2)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:23:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169908#M32629</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T17:23:42Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169909#M32630</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, &lt;A __default_attr="5253" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt;. I originally had a loop solution, which does work, but in any event, the reason for my question was to see if simpler methods are available (and also hoping that I would learn something, which has happened!)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169909#M32630</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T17:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169910#M32631</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Paige,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Did you try the code I had suggested? It gives the same results as your code but, additionally, excludes cases where there is no match.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Art&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:34:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169910#M32631</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2014-11-13T17:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169911#M32632</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Seem to work, &lt;A __default_attr="149594" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt;, thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169911#M32632</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T17:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169912#M32633</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Paige,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Forgive me if I overlooked your comments, I am aware of that the length of COL2 is less or equal to COL1,&amp;nbsp; but does COL2 have to be inclusive to COL1? For instance, consider the following examples:&lt;/P&gt;&lt;P&gt;for COL1=AA999AAB, &lt;/P&gt;&lt;P&gt;COL2 of AA999 is a better match or AA999AB? notice the latter has the bigger n? if it is the latter, then what is your rule of dealing ties? eg.&amp;nbsp; AA999AB or&amp;nbsp; AA999AC? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Haikuo &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:42:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169912#M32633</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2014-11-13T17:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Unusual String Comparison Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169913#M32634</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE __jive_macro_name="quote" class="jive_text_macro jive_macro_quote" modifiedtitle="true"&gt;
&lt;P&gt;Hai.kuo wrote:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;for COL1=AA999AAB,&lt;/P&gt;
&lt;P&gt;COL2 of AA999 is a better match or AA999AB?&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;For the case above, COL2 of AA999 is the match I want, as the requirement seems to have been incompletely stated. When you match COL1 and COL2, there cannot be any mismatches, positions 1 thru length(COL2) must match, and I want the longest COL2 which has this property. If you match COL1=AA999AAB with COL2=AA999AB, there is a mismatch in postion 7, so it is disqualified.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 17:51:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unusual-String-Comparison-Problem/m-p/169913#M32634</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2014-11-13T17:51:16Z</dc:date>
    </item>
  </channel>
</rss>

