<?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 Merge with SUBSTR in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-SUBSTR/m-p/952497#M372251</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Let's say that there are 2 fields that identify a customer-&amp;nbsp;HighCustID and&amp;nbsp;LowCustID.&lt;/P&gt;
&lt;P&gt;Let's think about it like a family structure where some people can belong to same family.&lt;/P&gt;
&lt;P&gt;So, under specific&amp;nbsp;HighCustID there can be multiple&amp;nbsp;LowCustID.&lt;/P&gt;
&lt;P&gt;Each person (LowCustID) can be Main (Ind_Main=1) or not main (Ind_Main=0)&lt;/P&gt;
&lt;P&gt;The number of&amp;nbsp;HighCustID is defined as&amp;nbsp; the number of the main person .&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;In month 202410 , 2 people :&amp;nbsp;111(Main) and 222 (not main )&amp;nbsp; belong to same&amp;nbsp;HighCustID&amp;nbsp; so the number of the&amp;nbsp;HighCustID will be 111 ( as the number of the main)&lt;/P&gt;
&lt;P&gt;In month 202409, these 2 people&amp;nbsp; changed the functions and now 111 is not main and 222 is main so&amp;nbsp;the number of the&amp;nbsp;HighCustID will be 222 ( as the number of the main).&lt;/P&gt;
&lt;P&gt;For each month there are 2 tables:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;table in level of&amp;nbsp;HighCustID with data of wealth&lt;/P&gt;
&lt;P&gt;table that shows the ownership structure&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The task is to perform Left join from table&amp;nbsp;t202410_HighLevel&amp;nbsp; &amp;nbsp;to table&amp;nbsp;t202409_HighLevel.&lt;/P&gt;
&lt;P&gt;The problem is that the number of&amp;nbsp;HighCustID can be changed during months and I still want to merge them.&lt;/P&gt;
&lt;P&gt;So,&lt;/P&gt;
&lt;P&gt;I want to merge 111(t202410_HighLevel) with 222(t202409_HighLevel)&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to merge 444(t202410_HighLevel) with 444(t202409_HighLevel)&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and about 333(t202410_HighLevel)&amp;nbsp; he doesnt exist in&amp;nbsp;t202409_HighLevel (It is a new person that join )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question-&lt;/P&gt;
&lt;P&gt;Here is the code I created and in last step I want to perform merge in the following way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;on a.HighCustID=b.HighCustID OR a.HighCustID=substr(one of the elemetns in IDs from table t202409_HighLevel_b)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can anyone help to write this code that search if&amp;nbsp;&lt;CODE class=" language-sas"&gt;a.HighCustID equal&amp;nbsp;to&amp;nbsp;one&amp;nbsp;of&amp;nbsp;the&amp;nbsp;elements&amp;nbsp;(In&amp;nbsp;concatentate&amp;nbsp;field)&amp;nbsp;from&amp;nbsp;t202409_HighLevel_b?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;My code is working perfect but there are some things I want to do:&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;In&amp;nbsp;real&amp;nbsp;life&amp;nbsp;It&amp;nbsp;can&amp;nbsp;have&amp;nbsp;more&amp;nbsp;commas&amp;nbsp;in&amp;nbsp;the&amp;nbsp;concatenate&amp;nbsp;and&amp;nbsp;then&amp;nbsp;I&amp;nbsp;want&amp;nbsp;to&amp;nbsp;create&amp;nbsp;merge&amp;nbsp;that&amp;nbsp;automatically&amp;nbsp;check&amp;nbsp;how&amp;nbsp;many&amp;nbsp;commas&amp;nbsp;have&amp;nbsp;and&amp;nbsp;try&amp;nbsp;to&amp;nbsp;merge&amp;nbsp;&amp;nbsp;with&amp;nbsp;all&amp;nbsp;possible&amp;nbsp;locations&amp;nbsp;.&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Here&amp;nbsp;is&amp;nbsp;my&amp;nbsp;full&amp;nbsp;code:&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/***********INPUT data sets*****************/
/***********INPUT data sets*****************/
/***********INPUT data sets*****************/
Data t202410_HighLevel;
Input HighCustID wealth;
cards;
111 100
333 80
444 180
;
Run;


Data t202409_HighLevel;
Input HighCustID wealth;
cards;
222 110
444 80
;
Run;


Data t202410_ownership;
Input HighCustID LowCustID Ind_Main;
cards;
111 111 1
111 222 0
333 333 1
444 444 1
;
Run;
Data t202409_ownership;
Input HighCustID LowCustID Ind_Main;
cards;
222 111 0
222 222 1
333 333 1
;
Run;


/***********Data sets that I created in order to get the wanted data set*****/
/***********Data sets that I created in order to get the wanted data set*****/
/***********Data sets that I created in order to get the wanted data set*****/
data want_ownership_202410;
length CAT $20.;
do until (last.HighCustID);
set t202410_ownership;
by HighCustID notsorted;
CAT=catx(',',CAT,LowCustID);
end;
drop LowCustID;
run;

data want_ownership_202409;
length CAT $20.;
do until (last.HighCustID);
set t202409_ownership;
by HighCustID notsorted;
CAT=catx(',',CAT,LowCustID);
end;
drop LowCustID;
run;




proc sql;
create table t202409_HighLevel_b as
select a.*,b.CAT
from t202409_HighLevel as a
left join want_ownership_202409  as b
on a.HighCustID=b.HighCustID
;
quit;



 
proc sql;
create table Want as
select a.HighCustID as HighCustID202409,a.wealth as wealth202410,b.wealth as wealth202409
from t202410_HighLevel  as a
left join t202409_HighLevel_b  as b
on a.HighCustID=b.HighCustID
OR
CAT(a.HighCustID)=scan(b.CAT,1,",")
OR
CAT(a.HighCustID)=scan(b.CAT,2,",")
OR
CAT(a.HighCustID)=scan(b.CAT,3,",")
OR
CAT(a.HighCustID)=scan(b.CAT,4,",")
;
quit;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 04 Dec 2024 05:55:36 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2024-12-04T05:55:36Z</dc:date>
    <item>
      <title>Merge with SUBSTR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-SUBSTR/m-p/952497#M372251</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;Let's say that there are 2 fields that identify a customer-&amp;nbsp;HighCustID and&amp;nbsp;LowCustID.&lt;/P&gt;
&lt;P&gt;Let's think about it like a family structure where some people can belong to same family.&lt;/P&gt;
&lt;P&gt;So, under specific&amp;nbsp;HighCustID there can be multiple&amp;nbsp;LowCustID.&lt;/P&gt;
&lt;P&gt;Each person (LowCustID) can be Main (Ind_Main=1) or not main (Ind_Main=0)&lt;/P&gt;
&lt;P&gt;The number of&amp;nbsp;HighCustID is defined as&amp;nbsp; the number of the main person .&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;In month 202410 , 2 people :&amp;nbsp;111(Main) and 222 (not main )&amp;nbsp; belong to same&amp;nbsp;HighCustID&amp;nbsp; so the number of the&amp;nbsp;HighCustID will be 111 ( as the number of the main)&lt;/P&gt;
&lt;P&gt;In month 202409, these 2 people&amp;nbsp; changed the functions and now 111 is not main and 222 is main so&amp;nbsp;the number of the&amp;nbsp;HighCustID will be 222 ( as the number of the main).&lt;/P&gt;
&lt;P&gt;For each month there are 2 tables:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;table in level of&amp;nbsp;HighCustID with data of wealth&lt;/P&gt;
&lt;P&gt;table that shows the ownership structure&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The task is to perform Left join from table&amp;nbsp;t202410_HighLevel&amp;nbsp; &amp;nbsp;to table&amp;nbsp;t202409_HighLevel.&lt;/P&gt;
&lt;P&gt;The problem is that the number of&amp;nbsp;HighCustID can be changed during months and I still want to merge them.&lt;/P&gt;
&lt;P&gt;So,&lt;/P&gt;
&lt;P&gt;I want to merge 111(t202410_HighLevel) with 222(t202409_HighLevel)&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to merge 444(t202410_HighLevel) with 444(t202409_HighLevel)&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and about 333(t202410_HighLevel)&amp;nbsp; he doesnt exist in&amp;nbsp;t202409_HighLevel (It is a new person that join )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question-&lt;/P&gt;
&lt;P&gt;Here is the code I created and in last step I want to perform merge in the following way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;on a.HighCustID=b.HighCustID OR a.HighCustID=substr(one of the elemetns in IDs from table t202409_HighLevel_b)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Can anyone help to write this code that search if&amp;nbsp;&lt;CODE class=" language-sas"&gt;a.HighCustID equal&amp;nbsp;to&amp;nbsp;one&amp;nbsp;of&amp;nbsp;the&amp;nbsp;elements&amp;nbsp;(In&amp;nbsp;concatentate&amp;nbsp;field)&amp;nbsp;from&amp;nbsp;t202409_HighLevel_b?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;My code is working perfect but there are some things I want to do:&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;In&amp;nbsp;real&amp;nbsp;life&amp;nbsp;It&amp;nbsp;can&amp;nbsp;have&amp;nbsp;more&amp;nbsp;commas&amp;nbsp;in&amp;nbsp;the&amp;nbsp;concatenate&amp;nbsp;and&amp;nbsp;then&amp;nbsp;I&amp;nbsp;want&amp;nbsp;to&amp;nbsp;create&amp;nbsp;merge&amp;nbsp;that&amp;nbsp;automatically&amp;nbsp;check&amp;nbsp;how&amp;nbsp;many&amp;nbsp;commas&amp;nbsp;have&amp;nbsp;and&amp;nbsp;try&amp;nbsp;to&amp;nbsp;merge&amp;nbsp;&amp;nbsp;with&amp;nbsp;all&amp;nbsp;possible&amp;nbsp;locations&amp;nbsp;.&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Here&amp;nbsp;is&amp;nbsp;my&amp;nbsp;full&amp;nbsp;code:&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/***********INPUT data sets*****************/
/***********INPUT data sets*****************/
/***********INPUT data sets*****************/
Data t202410_HighLevel;
Input HighCustID wealth;
cards;
111 100
333 80
444 180
;
Run;


Data t202409_HighLevel;
Input HighCustID wealth;
cards;
222 110
444 80
;
Run;


Data t202410_ownership;
Input HighCustID LowCustID Ind_Main;
cards;
111 111 1
111 222 0
333 333 1
444 444 1
;
Run;
Data t202409_ownership;
Input HighCustID LowCustID Ind_Main;
cards;
222 111 0
222 222 1
333 333 1
;
Run;


/***********Data sets that I created in order to get the wanted data set*****/
/***********Data sets that I created in order to get the wanted data set*****/
/***********Data sets that I created in order to get the wanted data set*****/
data want_ownership_202410;
length CAT $20.;
do until (last.HighCustID);
set t202410_ownership;
by HighCustID notsorted;
CAT=catx(',',CAT,LowCustID);
end;
drop LowCustID;
run;

data want_ownership_202409;
length CAT $20.;
do until (last.HighCustID);
set t202409_ownership;
by HighCustID notsorted;
CAT=catx(',',CAT,LowCustID);
end;
drop LowCustID;
run;




proc sql;
create table t202409_HighLevel_b as
select a.*,b.CAT
from t202409_HighLevel as a
left join want_ownership_202409  as b
on a.HighCustID=b.HighCustID
;
quit;



 
proc sql;
create table Want as
select a.HighCustID as HighCustID202409,a.wealth as wealth202410,b.wealth as wealth202409
from t202410_HighLevel  as a
left join t202409_HighLevel_b  as b
on a.HighCustID=b.HighCustID
OR
CAT(a.HighCustID)=scan(b.CAT,1,",")
OR
CAT(a.HighCustID)=scan(b.CAT,2,",")
OR
CAT(a.HighCustID)=scan(b.CAT,3,",")
OR
CAT(a.HighCustID)=scan(b.CAT,4,",")
;
quit;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 05:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-with-SUBSTR/m-p/952497#M372251</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-12-04T05:55:36Z</dc:date>
    </item>
    <item>
      <title>Re: Merge with SUBSTR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-SUBSTR/m-p/952513#M372255</link>
      <description>&lt;P&gt;To stick with the coding approach you've taken I believe to understand that you're just asking how to deal with a varying number of comma separated terms in your concatenated string. If so then why not use findw() instead?&lt;/P&gt;
&lt;PRE&gt;findw(strip(b.CAT),CAT(a.HighCustID)) &amp;gt;0&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Dec 2024 10:07:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-with-SUBSTR/m-p/952513#M372255</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-12-04T10:07:30Z</dc:date>
    </item>
    <item>
      <title>Re: Merge with SUBSTR</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-with-SUBSTR/m-p/952540#M372268</link>
      <description>&lt;P&gt;INDEXW() will work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use CATS() function strip leading/trailing spaces from HIGHCUSTID (or convert it to a string if it is numeric).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ON indexw(b.CAT,cats(a.HighCustID),',')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to ignore the case of any letters in the ids the you might want to use FINDW() instead as it has a modifiers arguments you can add to ignore case.&amp;nbsp; It also has modifiers to remove the spaces.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ON findw(b.CAT,a.HighCustID,',','it')&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Dec 2024 15:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-with-SUBSTR/m-p/952540#M372268</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-12-04T15:36:43Z</dc:date>
    </item>
  </channel>
</rss>

