DATA Step, Macro, Functions and more

Compare 2 numeric variables with 1 digit mismatch

Reply
Contributor
Posts: 34

Compare 2 numeric variables with 1 digit mismatch

Hi All,

   I am in situation like, i have to compare a numeric variable from flat file with a numeric variable in SAS Dataset. The trick is, if both the variable is not 100% match, then consider for 1 digit mismatch. For example,

FLAT_FILE_VAR_A

==============

123

456

789

799

SAS_DATASET_VAR_A

==================

123

456

789

111

Expecting Result

============= 

123    100%Match

456    100%Match

789     100%Match

799     80%Match  <-- this 799 is one digit mismatch with SAS table value "789"

   Could you please let me know , how i can achieve this result?? Kindly note for giving example purpose , i gave 3 bytes to variable A. In my file variable A has 9 digit numeric.

Thanks,

Prabakaran

Super User
Posts: 19,822

Re: Compare 2 numeric variables with 1 digit mismatch

1. Do an exact match first.

2. On those that aren't matched then use compged to see the distance and determine matches.

There's a good post on how to do that here by friedegg if you do some searching for fuzzy match or non-exact matching.

Super User
Posts: 10,035

Re: Compare 2 numeric variables with 1 digit mismatch

Check complev() function which can calculated a edit distance between two strings.

Super User
Posts: 5,512

Re: Compare 2 numeric variables with 1 digit mismatch

Here's an approach, but it may consume a lot of memory depending on how low a match rate you are willing to search for.

First, your variables should be character, not numeric.

Second, create a series of formats out of SAS_DATASET_VAR_A:

value $match3_ '123'='100%'

                        '456'='100%'

                        '789'='100%'

                        '111'='100%'

                         ;

value $match2_ '12*'='2 of 3'

                        '1*3'='2 of 3

                        '12*'='2 of 3

                        '*23'='2 of 3

                        '45*'='2 of 3

                        '4*6'='2 of 3

                        '*56'='2 of 3

                        '78*'='2 of 3

                        '7*9'='2 of 3

                        '*89'='2 of 3

                        '11*'='2 of 3

                        '1*1'='2 of 3

                        '*11'='2 of 3

                         ;

While this can be automated using CNTLIN= data sets, you will have to use PROC SORT NODUPKEY before creating the second format because there will be duplicate entries.  (123 and 124 will both generate 12*, for example.)  Note that the formats get larger, the more wild cards you allow.

Finally, test:

data want;

   set have;

   if put(ssn, $match3_.)='100%' then do;

      match_pattern=ssn;

      match_rate='100%   ';

      output;

      delete;

   end;

   do star_position=1 to 3;

        match_pattern=ssn;

        substr(match_pattern, star_position, 1) = '*';

        if put(match_pattern, $match2_.)='2 of 3' then do;

          match_rate='2 of 3';

          output;

          delete;

       end;

   end;

    

This is sample, untested code, but it giives you the idea.  I'm pretty sure hash tables instead of formats would work equally well, and might even consolidate everything into one step.  There would be one hash table for each level of match (100%, 80%, etc.).

Good luck.

Ask a Question
Discussion stats
  • 3 replies
  • 309 views
  • 0 likes
  • 4 in conversation