<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to compare 2 string and return a number of letter difference by position? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932373#M366783</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52063"&gt;@bgb&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Glad to see that &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976" target="_blank" rel="noopener"&gt;SASKiwi&lt;/A&gt;'s solution should work for you. Then it would be fair and help later readers if you marked his helpful reply as the accepted solution, not your own "thank you" post. Could you please change that? It's very easy: Select his post&amp;nbsp;as the solution after clicking&amp;nbsp;"Not the Solution" in the option menu (see icon below) of the current solution.&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="show_option_menu.png" style="width: 155px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97432iF2CAF44B3C615E4D/image-size/large?v=v2&amp;amp;px=999" role="button" title="show_option_menu.png" alt="show_option_menu.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Jun 2024 07:44:32 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2024-06-14T07:44:32Z</dc:date>
    <item>
      <title>How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932326#M366750</link>
      <description>&lt;P&gt;I want to write a MCARO to compare the 2 strings and return a number of letter difference by position.&lt;/P&gt;&lt;P&gt;For example, I have 2 strings.&lt;/P&gt;&lt;P&gt;Str1 ="ABCDEFG";&amp;nbsp; &amp;nbsp; &amp;nbsp; /*** Length=7 **/&lt;/P&gt;&lt;P&gt;Str2="ACDEFGHJ"&amp;nbsp; &amp;nbsp; &amp;nbsp;/*** Length=8 **/&lt;/P&gt;&lt;P&gt;By using the MACRO:&lt;/P&gt;&lt;P&gt;Diff=%DiffNum (Str1, Str2); /*** return value of Diff will be 7, Since only the first letters are the same. ***/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately, seems that none of the SAS comparing FUNCTIONs&amp;nbsp; (COMPARE, COMPLEV,&amp;nbsp;COMPGED) can do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2024 21:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932326#M366750</guid>
      <dc:creator>bgb</dc:creator>
      <dc:date>2024-06-13T21:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932334#M366754</link>
      <description>&lt;P&gt;Writing your own comparison isn't difficult:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Want;
  length str1 str2 $200;
  Str1 = "ABCDEFG";     
  Str2 = "ACDEFGHJ"; 
  do position = 1 to max(length(Str1), length(Str2));
    if substr(Str1,position,1) ne substr(Str2,position,1)
    then Diff+1;
  end;
  put _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jun 2024 22:34:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932334#M366754</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-06-13T22:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932335#M366755</link>
      <description>&lt;P&gt;Thanks&amp;nbsp; SASKiwi,&lt;/P&gt;&lt;P&gt;In fact, I had written an almost identical program as yours to do it.&lt;/P&gt;&lt;P&gt;However, what I really want is to embed it into a proc SQL procedure for merging two datasets together, and allow at most 2 difference between 2 strings. That why initially I wanted to use the SAS comparing functions directly, and then found that none of them works my situation, so I consider writing a macro to do it. It would be something like the following. Would it be possible?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Data Data1;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Str1="ABCDEFG";&amp;nbsp; Var1=88; Var2="yes"; output;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Str1="ABCDDDD";&amp;nbsp; Var1=78; Var2="no"; output;
run;

Data Data2;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Str2="ABCDEFH";&amp;nbsp; &amp;nbsp;Var3="High"; Var4=2; output;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Str2="ACDEFGHJ";&amp;nbsp; Var3="Low"; Var4=1; output;
run;

PROC SQL;
	Create table MergingData as
	select a.*, b.*
&amp;nbsp; &amp;nbsp;     from Data1&amp;nbsp; as a
&amp;nbsp; &amp;nbsp;     left join Data2 as b on %DiffNum(a.Str1, b.Str2)&amp;lt;=2;&amp;nbsp;
&amp;nbsp; &amp;nbsp; /*** Originally I had thought that I can use COMPLEV(a.Str1, b.Str2)&amp;lt;=2., but later found I can not use it. ***/
Quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jun 2024 23:00:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932335#M366755</guid>
      <dc:creator>bgb</dc:creator>
      <dc:date>2024-06-13T23:00:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932336#M366756</link>
      <description>&lt;P&gt;Why does it need to be SQL? You can do it all in a DATA step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Data1;
      Str1="ABCDEFG";  Var1=88; Var2="yes"; output;
      Str1="ABCDDDD";  Var1=78; Var2="no"; output;
run;

Data Data2;
      Str2="ABCDEFH";   Var3="High"; Var4=2; output;
      Str2="ACDEFGHJ";  Var3="Low"; Var4=1; output;
run;

data Want;
  length str1 str2 $200;
  set data1;
  set data2;
  do position = 1 to max(length(Str1), length(Str2));
    if substr(Str1,position,1) ne substr(Str2,position,1)
    then Diff+1;
  end;
  put _all_;
  if Diff &amp;lt;= 2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jun 2024 23:14:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932336#M366756</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-06-13T23:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932337#M366757</link>
      <description>&lt;P&gt;Thank you so much, SASKIwi,&lt;/P&gt;&lt;P&gt;I didn't know it can be done&amp;nbsp; that way. The code seems so easy.&lt;/P&gt;&lt;P&gt;1) Is there any problem using this way for merging 2 large datasets? For example, 1 dataset with 5K observations, and the other one with 3 millions records.&lt;/P&gt;&lt;P&gt;2) I would even like to consider comparing 3&amp;nbsp; variables and add the difference together, with the maximum allowed difference of 2. So the code will be something like the following?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data Want (drop=i j k);
  length str1 str2 var1 var2 cose1 code2 $20;
  set data1;
  set data2;
  do i= 1 to max(length(Str1), length(Str2));
    if substr(Str1,i,1) ne substr(Str2,i,1)
    then Diff1+1;
  end;
  do j = 1 to max(length(Var1), length(Var2));
    if substr(var1,j,1) ne substr(Var2,j,1)
    then Diff2+1;
  end;
  do k = 1 to max(length(code1), length(code2));
    if substr(code1,k,1) ne substr(code2,k,1)
    then Diff3+1;
  end;
  put _all_;
  if (Diff1+Diff2+Diff3) &amp;lt;= 2;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2024 23:35:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932337#M366757</guid>
      <dc:creator>bgb</dc:creator>
      <dc:date>2024-06-13T23:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932338#M366758</link>
      <description>&lt;P&gt;DATA steps can deal with large datasets. You will probably need to change the merging logic as you haven't explained how that works. Are you comparing all rows in one table with all rows in the second table or is it some other joining method? The way I've done it is I assume the two input tables have the same number of rows and I'm joining row 1 with row 1, row 2 with row 2 and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code you have written seems to be on the right track.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2024 23:53:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932338#M366758</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-06-13T23:53:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932339#M366759</link>
      <description>&lt;P&gt;Yes, I am&amp;nbsp;&lt;SPAN&gt;comparing (and merging) all rows in one table with all rows in the second table.&amp;nbsp; One table is patients list (~5k), and one table is hospital data (~4M).&amp;nbsp; &amp;nbsp;So I think I will need to change the merging logic, and that's why I initially try to use PROC SQL.&amp;nbsp; Thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2024 00:00:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932339#M366759</guid>
      <dc:creator>bgb</dc:creator>
      <dc:date>2024-06-14T00:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932349#M366768</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Want;
  length str1 str2 $200;
  set data1;
  do row = 1 to obsnum;
    set data2 point = row nobs = obsnum;
	diff = 0;
    do position = 1 to max(length(Str1), length(Str2));
      if substr(Str1,position,1) ne substr(Str2,position,1)
      then Diff+1;
    end;
	if Diff &amp;lt;= 2 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Jun 2024 00:54:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932349#M366768</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2024-06-14T00:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932353#M366770</link>
      <description>&lt;P&gt;This way should work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much. It will save me a lot time trying to write a MACRO to do the comparing.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2024 01:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932353#M366770</guid>
      <dc:creator>bgb</dc:creator>
      <dc:date>2024-06-14T01:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare 2 string and return a number of letter difference by position?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932373#M366783</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52063"&gt;@bgb&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Glad to see that &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976" target="_blank" rel="noopener"&gt;SASKiwi&lt;/A&gt;'s solution should work for you. Then it would be fair and help later readers if you marked his helpful reply as the accepted solution, not your own "thank you" post. Could you please change that? It's very easy: Select his post&amp;nbsp;as the solution after clicking&amp;nbsp;"Not the Solution" in the option menu (see icon below) of the current solution.&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="show_option_menu.png" style="width: 155px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97432iF2CAF44B3C615E4D/image-size/large?v=v2&amp;amp;px=999" role="button" title="show_option_menu.png" alt="show_option_menu.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2024 07:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-2-string-and-return-a-number-of-letter-difference/m-p/932373#M366783</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-06-14T07:44:32Z</dc:date>
    </item>
  </channel>
</rss>

