<?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: Merging data in 2 levels in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813330#M320976</link>
    <description>&lt;A href="https://communities.sas.com/t5/SAS-Programming/Good-Forum-Practices-Ask-a-question/m-p/813327" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Good-Forum-Practices-Ask-a-question/m-p/813327&lt;/A&gt;</description>
    <pubDate>Sat, 14 May 2022 06:38:39 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-05-14T06:38:39Z</dc:date>
    <item>
      <title>Merging data in 2 levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813328#M320974</link>
      <description>&lt;P&gt;Hi Everyone. I m currently using SAS 9.4 version . I have to merge two tables A and B based on SUBJID and LABID. The SUBJID is of 10 characters and LABID is of 12 characters . My requirement is first I have to check if I can match SUBJID and first 12 digits of LABID.&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;SUBJID&lt;/TD&gt;&lt;TD&gt;LABID&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#00FF00"&gt;EQ98765599&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#00FF00"&gt;987653456986&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;MN65473829&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;634528754100&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#00FF00"&gt;OP32419877&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#00FF00"&gt;362567375709&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;SUBJID&lt;/TD&gt;&lt;TD&gt;LABID&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#00FF00"&gt;EQ98765599&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#00FF00"&gt;987653456986&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;MN65473829&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#FF0000"&gt;634528754187&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;FONT color="#00FF00"&gt;OP32419877&lt;/FONT&gt;&lt;/TD&gt;&lt;TD&gt;&lt;FONT color="#00FF00"&gt;362567375709&lt;/FONT&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The subjects highlighted in green are exact match where as the subject highlighted in red are not exact match because only first 10 characters are matching in LABID and 11th ,12th characters are different in LABID , so they wont come in output. Is it possible to merge the data like if 12 digits of LABID are matching then output the data , if 12 digits of LABID are not matching then check if 10 digits of LABID are matching then output the data.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2022 07:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813328#M320974</guid>
      <dc:creator>Prudhvi_007</dc:creator>
      <dc:date>2022-05-14T07:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data in 2 levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813330#M320976</link>
      <description>&lt;A href="https://communities.sas.com/t5/SAS-Programming/Good-Forum-Practices-Ask-a-question/m-p/813327" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Good-Forum-Practices-Ask-a-question/m-p/813327&lt;/A&gt;</description>
      <pubDate>Sat, 14 May 2022 06:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813330#M320976</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-05-14T06:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data in 2 levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813334#M320980</link>
      <description>&lt;P&gt;The message the&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;referred to appears to be deleted, but my guess is that it contains a suggestion that you present test data as data step code, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
length SUBJID LABID $20;
input subjid labid;
cards;
EQ98765599 987653456986
MN65473829 634528754100
OP32419877 1362567375709
;run;

Data have2;
length SUBJID LABID $20;
input subjid labid;
cards;
EQ98765599 987653456986
MN65473829 634528754187
OP32419877 1362567375709
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think the easiest way to make the match is to use SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as select 
    have1.*,
    have2.labid as labid2, 
    case
      when have1.labid=have2.labid then 1
      else 0
    end as match
  from have1 join have2 on have1.subjid=have2.subjid and substr(have1.labid,1,10)=substr(have2.labid,1,10)
  ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will merge all the data on the criteria you mentioned. The variable MATCH will contain a 1 if the two LABIDs are identical, and otherwise a 0. If there are multiple occurences of each SUBJID and LABID in your data, you may want to add additional criteria, as you will otherwise get a cartesian product (all combinations of possible matches from the two tables).&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2022 10:15:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813334#M320980</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2022-05-14T10:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data in 2 levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813337#M320983</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390515"&gt;@Prudhvi_007&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;it is indeed possible to have an output of matching and non-matching observations. One can use either the SAS dataset merge or Proc SQL whichever one is comfortable with.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2022 12:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813337#M320983</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-05-14T12:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data in 2 levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813349#M320993</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These questions are not answered by your sample data, but the answers would significantly impact the appropriate responses:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Do you expect any many-to-many matches of SUBJID/12-digit-LABID?&amp;nbsp; If yes, then do you want to keep implied cartesian crossing of all SUBJID/12-digit matches?&lt;/LI&gt;
&lt;LI&gt;If you do not expect the above conditino, then are your 12-digit LABID's constructed in such a way that any of them share the first 10 digits?&amp;nbsp; If so, and assuming a given row is matched on SUBJID, but no 12-digit-LABID match, what do you want to do if there is a set of many-to-many matches on SUBJID/10-digit-LABID?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 14 May 2022 15:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813349#M320993</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-05-14T15:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: Merging data in 2 levels</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813355#M320998</link>
      <description>&lt;P&gt;The clearest way is to make a new variable that has just the first 10 characters of the LABID.&amp;nbsp; Let's call it LABSUBJ.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fix1;
  set one;
  labsubj = substr(labid,1,10);
run;

data fix2;
  set two;
  labsubj = substr(labid,1,10);
run;

data want;
  merge fix1 fix2(rename=(labid=labid2));
  by subjid labsubj;
  exact = labid = labid2;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs      SUBJID         LABID         labsubj         labid2       exact

 1     EQ98765599    987653456986    9876534569    987653456986      1
 2     MN65473829    634528754100    6345287541    634528754187      0
 3     OP32419877    362567375709    3625673757    362567375709      1
&lt;/PRE&gt;
&lt;P&gt;If there are multiple observations of for the same values of SUBJID and LABSUBJ in both datasets then you probably need to do a many to many join which will require SQL code instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table both as 
  select a.*
       , coalesce(substr(a.labid,1,10),substr(b.labid,1,10)) as LABSUBJ
       , b.labid as labid2
       , a.labid=b.labid as exact
  from one a 
  full join two b
  on a.subjid = b.subjid
  and substr(a.labid,1,10) eqt b.labid
  order by 1,2,3,4
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 May 2022 19:24:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-data-in-2-levels/m-p/813355#M320998</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-14T19:24:23Z</dc:date>
    </item>
  </channel>
</rss>

