<?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: Matching 2 Character Fields in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974546#M377921</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&amp;nbsp; I don't think you've provided enough information for a concrete suggestion. In a quick test, using your fake data I created Table A with POLICY&amp;nbsp; and CUSTOMER and then created TABLE B with the same 4 values for POLICY and also created a LIMIT column. I ran a few tests and got matches using both a DATA step MERGE and an SQL join. Table A had POLICY with a length of $9 and B had POLICY with a length of $20. Of course, SAS did give a WARNING about the differing lengths of the POLICY variable in the MERGE where the data set with the length of $9 for policy was listed first.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; The code I tested is listed at the bottom of this post. You should be able to run it and prove to yourself that there were matches.&lt;/P&gt;&lt;P&gt;&amp;nbsp; There are 4 steps -- 2 steps with MERGE and 2 steps with SQL JOIN. The POLICY&amp;nbsp; values were the same and the output was 4 rows each time. So I cannot duplicate your observed behavior. There must be something different about your code or your actual data that is causing the non-matches to happen.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Do note the output from PROC CONTENTS after each step. It will make a difference in the output data for the length of POLICY, depending on which table is listed first in the MERGE or the JOIN. Others have suggested sharing more of your actual code and your actual data. I also suggest reviewing the log and reporting the exact warnings or errors you might see there (or even, post the log if it's not too long). However, if the ONLY difference in POLICY is the length, your MERGE or JOIN should work, as illustrated by my examples.&lt;/P&gt;&lt;P&gt;Cynthia&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Data A;
Length policy $9;
Infile datalines dlm=',';
Input policy $	customer $;
Datalines;
000718124,1001
000714946,1002
000910934,1003
000901672,1004
;
Run;

Data B;
Length policy $20;
Infile datalines dlm=',';
Input policy $	limit $;
Datalines;
000718124,20000
000714946,30000
000910934,40000
000901672,50000
;
Run;

Proc sort data=A; by policy; run;
Proc sort data=B; by policy; run;

Data both;
  Merge A(in=fromA)
        B(in=fromB);
  By policy;
  If fromA=1 and fromB=1 then output both;
Run;

Proc contents data=both;
Title '1) Data A length $9 listed first in merge';
Run;

Proc print data=both;
Run;
title;

Data altboth;
  Merge B(in=fromB)
        A(in=fromA);
  By policy;
  If fromA=1 and fromB=1 then output altboth;
Run;

Proc contents data=altboth;
Title '2) Data B length $20 listed first in merge';
Run;

Proc print data=altboth;
Run;

proc sql;
  create table sqboth
  as select a.policy, customer, limit
  from a,b 
  where a.policy=b.policy
  order by policy;
quit;

proc contents data=sqboth;
title '3) data a listed first in the join';
run;

proc print data=sqboth;
run;

proc sql;
  create table altsql
  as select b.policy, customer, limit
  from b,a 
  where a.policy=b.policy
  order by policy;
quit;

proc contents data=altsql;
title '4) data b listed first in the join';
run;

proc print data=altsql;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 08 Sep 2025 22:11:42 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2025-09-08T22:11:42Z</dc:date>
    <item>
      <title>Matching 2 Character Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974484#M377911</link>
      <description>&lt;P&gt;I need some help resolving an issue. I am joining two tables by a field called&amp;nbsp; 'policy'.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;policy&lt;/TD&gt;&lt;TD&gt;customer&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000718124&lt;/TD&gt;&lt;TD&gt;1001&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000714946&lt;/TD&gt;&lt;TD&gt;1002&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000910934&lt;/TD&gt;&lt;TD&gt;1003&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;000901672&lt;/TD&gt;&lt;TD&gt;1004&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In table A the policy field is Type: Character and Length: 9 and in the table B it is&amp;nbsp;Type: Character and Length: 20.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the field has 9 characters I changed the length of the field in table B in proc sql like below.&lt;/P&gt;&lt;P&gt;data test1;&lt;BR /&gt;length policy $9;&lt;BR /&gt;set B;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;So now, the field policy in test1 table has length = 9. So, when I join tables A and test1, I do not get any data back.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I changed the length of policy field in table A also, even though the character length is 9.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test2;&lt;BR /&gt;length policy $9;&lt;BR /&gt;set A;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;Again I tried join test1 and test2 but I am still not getting any data back. Both tables have the same values for policy field so I know there is data but I do not get anything back.&lt;/P&gt;&lt;P&gt;Thank you in advance&lt;/P&gt;</description>
      <pubDate>Mon, 08 Sep 2025 18:06:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974484#M377911</guid>
      <dc:creator>SASMom2</dc:creator>
      <dc:date>2025-09-08T18:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: Matching 2 Character Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974495#M377914</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;So, when I join tables A and test1, I do not get any data back.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;There could be a dozen reasonable explanations for this. Please show us the code you are using. Please show us (a portion of) the data in table A and show us (a portion of) the data in table B.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Sep 2025 18:44:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974495#M377914</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-09-08T18:44:46Z</dc:date>
    </item>
    <item>
      <title>Re: Matching 2 Character Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974546#M377921</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&amp;nbsp; I don't think you've provided enough information for a concrete suggestion. In a quick test, using your fake data I created Table A with POLICY&amp;nbsp; and CUSTOMER and then created TABLE B with the same 4 values for POLICY and also created a LIMIT column. I ran a few tests and got matches using both a DATA step MERGE and an SQL join. Table A had POLICY with a length of $9 and B had POLICY with a length of $20. Of course, SAS did give a WARNING about the differing lengths of the POLICY variable in the MERGE where the data set with the length of $9 for policy was listed first.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; The code I tested is listed at the bottom of this post. You should be able to run it and prove to yourself that there were matches.&lt;/P&gt;&lt;P&gt;&amp;nbsp; There are 4 steps -- 2 steps with MERGE and 2 steps with SQL JOIN. The POLICY&amp;nbsp; values were the same and the output was 4 rows each time. So I cannot duplicate your observed behavior. There must be something different about your code or your actual data that is causing the non-matches to happen.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;Do note the output from PROC CONTENTS after each step. It will make a difference in the output data for the length of POLICY, depending on which table is listed first in the MERGE or the JOIN. Others have suggested sharing more of your actual code and your actual data. I also suggest reviewing the log and reporting the exact warnings or errors you might see there (or even, post the log if it's not too long). However, if the ONLY difference in POLICY is the length, your MERGE or JOIN should work, as illustrated by my examples.&lt;/P&gt;&lt;P&gt;Cynthia&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Data A;
Length policy $9;
Infile datalines dlm=',';
Input policy $	customer $;
Datalines;
000718124,1001
000714946,1002
000910934,1003
000901672,1004
;
Run;

Data B;
Length policy $20;
Infile datalines dlm=',';
Input policy $	limit $;
Datalines;
000718124,20000
000714946,30000
000910934,40000
000901672,50000
;
Run;

Proc sort data=A; by policy; run;
Proc sort data=B; by policy; run;

Data both;
  Merge A(in=fromA)
        B(in=fromB);
  By policy;
  If fromA=1 and fromB=1 then output both;
Run;

Proc contents data=both;
Title '1) Data A length $9 listed first in merge';
Run;

Proc print data=both;
Run;
title;

Data altboth;
  Merge B(in=fromB)
        A(in=fromA);
  By policy;
  If fromA=1 and fromB=1 then output altboth;
Run;

Proc contents data=altboth;
Title '2) Data B length $20 listed first in merge';
Run;

Proc print data=altboth;
Run;

proc sql;
  create table sqboth
  as select a.policy, customer, limit
  from a,b 
  where a.policy=b.policy
  order by policy;
quit;

proc contents data=sqboth;
title '3) data a listed first in the join';
run;

proc print data=sqboth;
run;

proc sql;
  create table altsql
  as select b.policy, customer, limit
  from b,a 
  where a.policy=b.policy
  order by policy;
quit;

proc contents data=altsql;
title '4) data b listed first in the join';
run;

proc print data=altsql;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Sep 2025 22:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974546#M377921</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2025-09-08T22:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: Matching 2 Character Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974549#M377922</link>
      <description>&lt;P&gt;Please show the code you used to try and match the records.&amp;nbsp; You mentioned SQL but only showed DATA step code.&amp;nbsp; Did you try to do the matching using SQL?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A couple of issues to check.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;When you shorten the length of variable the way you did SAS will take the first 9 characters of the field.&amp;nbsp; If your 9 digit numbers were right aligned in the 20 character variable then the first 9 characters are all spaces.&lt;/LI&gt;
&lt;LI&gt;If the character variables where created by letting SAS autoconvert a numeric variable to character then SAS will use the BEST12 format.&amp;nbsp; Which for the examples you showed would result in POLICY numbers without the leading zeros (but with the leading spaces).&amp;nbsp; So a numeric value like&amp;nbsp;&lt;SPAN&gt;718,124 would have been converted to '&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;718124' instead of '000718124'.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Sep 2025 23:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974549#M377922</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-09-08T23:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Matching 2 Character Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974569#M377928</link>
      <description>It would be best if you could post some your original data to test the problem you are running into.&lt;BR /&gt;Actually the length of variable in these two dataset would not affect the JOIN operator in SQL . If you don't believe it , you could test it on your own.&lt;BR /&gt;And I doubt you have some unprintable characters in one dataset ,that is the reason why you can't success to join these two datasets.&lt;BR /&gt;Check this:&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Cannot-join-by-the-same-key/m-p/971964" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Cannot-join-by-the-same-key/m-p/971964&lt;/A&gt;</description>
      <pubDate>Tue, 09 Sep 2025 07:07:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974569#M377928</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-09-09T07:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: Matching 2 Character Fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974574#M377933</link>
      <description>Assign $HEX formats of suitable length (18 and 40) to the policy variables in both datasets and look what you find.&lt;BR /&gt;I suspect leading blanks (hex 20) or unprintable characters (0D0A, for instance).</description>
      <pubDate>Tue, 09 Sep 2025 08:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Matching-2-Character-Fields/m-p/974574#M377933</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-09-09T08:05:58Z</dc:date>
    </item>
  </channel>
</rss>

