<?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: Match the corresponding values and concatenate by '/' in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611998#M178497</link>
    <description>&lt;P&gt;Here is another solution: Use SET with KEY= in a datastep:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input varX$ varY$;
cards;
abc C1
def C2
ghi C3
jkl C4
mno C5
;run;

data have2;
lengh varx1 $20;
input varX1;
cards;
abc/def
ghi/mno
jkl/def
abc/mno/uvx
ghi/pqr/jkl
;run;

proc sql;
  create index varx on have1(varx);
quit;

data want;
  set have2;
  do _N_=1 to countw(varX1,'/');
    varx=scan(varx1,_N_,'/');
    set have1 key=varx/unique;
    length varY1 $20;
    if _iorc_ then do;
      _error_=0;
      call catx('/',varY1,'C0');
      end;
    else call catx('/',VarY1,VarY);
    end;
  keep varX1 VarY1;
  run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 16 Dec 2019 12:27:36 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2019-12-16T12:27:36Z</dc:date>
    <item>
      <title>Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611979#M178483</link>
      <description>&lt;P&gt;Dear Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I share the sample dataset which matches my requirement for the study.&lt;/P&gt;&lt;P&gt;I have two datasets contains 2 columns varX varY.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input varX$ varY$;
cards;
abc C1
def C2
ghi C3
jkl C4
mno C5
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The above code list the alphabets individually refers to C_value.&lt;/P&gt;&lt;P&gt;The below code refers to the combination of alphabets, So&amp;nbsp;according to alphabets I need to match the C_values on varY1.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2;
input varX1$;
cards;
abc/def
ghi/mno
jkl/def
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Required Output like:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;abc/def&lt;/TD&gt;&lt;TD&gt;C1/C2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;ghi/mno&lt;/TD&gt;&lt;TD&gt;C3/C5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;jkl/def&lt;/TD&gt;&lt;TD&gt;C4/C2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kindly suggest a code to solve the task.&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 09:11:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611979#M178483</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2019-12-16T09:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611980#M178484</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215282"&gt;@Sathish_jammy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can try this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	set have1;
	call symputx(varX,varY);
run;

data want;
	set have2;
	varY1 = catx("/",symget(scan(varX1,1,"/")),symget(scan(varX1,2,"/")));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Capture d’écran 2019-12-16 à 10.20.24.png" style="width: 258px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34795iE8153A5BB7296B21/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2019-12-16 à 10.20.24.png" alt="Capture d’écran 2019-12-16 à 10.20.24.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 09:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611980#M178484</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-16T09:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611982#M178485</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
    CREATE TABLE want AS
    SELECT a.varX1, cats(b.varY,'/',c.varY)
    FROM have2 a
    LEFT JOIN have1 b
    ON b.varX=scan(a.varX1,1,'/')
    LEFT JOIN have1 c
    ON c.varX=scan(a.varX1,2,'/')
    ORDER BY a.varX1
    ;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 09:25:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611982#M178485</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-12-16T09:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611983#M178486</link>
      <description>&lt;P&gt;Thank you so much&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Both of your code works well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Additionally, I required one more condition for this task.&lt;/P&gt;&lt;P&gt;Assume that I mentioned any new combination of 3alphabets in (have1(varX1)) which not listed in (varX(have))&lt;/P&gt;&lt;P&gt;How could I get the result as 'C0' in varY1.&lt;/P&gt;&lt;P&gt;For example :&lt;/P&gt;&lt;P&gt;abc/mno/uvx&amp;nbsp; - C1/C5/C0&lt;/P&gt;&lt;P&gt;ghi/pqr/jkl&amp;nbsp; - C3/C0/C4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It required because Once I found the C0 so I could add that new value in my list manually like&lt;/P&gt;&lt;P&gt;uvx - C6&lt;/P&gt;&lt;P&gt;pqr - C7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 10:04:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611983#M178486</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2019-12-16T10:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611985#M178488</link>
      <description>&lt;P&gt;You can use the coalescec function :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
    CREATE TABLE want AS
    SELECT a.varX1, catx('/', coalescec(b.varY,'C0'), coalescec(c.varY,'C0'), coalescec(d.varY,'C0')) AS newVar
    FROM have2 a
    LEFT JOIN have1 b
    ON b.varX=scan(a.varX1,1,'/')
    LEFT JOIN have1 c
    ON c.varX=scan(a.varX1,2,'/')
    LEFT JOIN have1 d
    ON d.varX=scan(a.varX1,3,'/')
    ORDER BY a.varX1
    ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 10:05:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611985#M178488</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-12-16T10:05:02Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611986#M178489</link>
      <description>&lt;P&gt;I appreciate your response&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It results in C0 for the new value but also at the end of all observation were it not requires.&lt;/P&gt;&lt;P&gt;like:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;abc/def - C1/C2/C0&lt;/P&gt;&lt;P&gt;ghi/mno - C3/C5/C0&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Could you please help me to sort out the C0 at the unrequired Observations.&lt;/P&gt;&lt;P&gt;Much Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 10:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611986#M178489</guid>
      <dc:creator>Sathish_jammy</dc:creator>
      <dc:date>2019-12-16T10:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611988#M178490</link>
      <description>&lt;P&gt;Here is another approach with a format generated by the dataset have1 :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ctrl;
   length label $ 11;
   set have1(rename=(varX=start varY=label)) end=last;
   end=start;
   retain fmtname 'CN' type 'c';
   output;
   if last then do;
      hlo='O';
      label='C0';
      output;
   end;
run;

proc format library=work cntlin=ctrl;
run;


data want;
    set have2;
    length newvar $20;
    do i=1 to countw(varX1,'/');
        call catx('/', newVar, put(scan(varX1,i,'/'),CN.));
    end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 10:34:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611988#M178490</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-12-16T10:34:16Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611989#M178491</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215282"&gt;@Sathish_jammy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have update the code using macro variables. Let me know if it works!&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Retrieve the list of words that are mentionned in have1 but not in have1 */
/* Put each value in a macrovariable with value = "C0"  */

data have2_tr (keep = VarX);
	if _n_=1 then set have1 (keep=VarX);
	set have2;
	do i=1 to countw(varX1,"/");
		VarX1_tr = scan(varX1,i,"/");
		VarX = VarX1_tr;
		output;
	end;
run;

proc sort data=have2_tr nodupkey;
	by VarX;
run;

data have2_not_in_have1;
	merge have1 (in=x keep=VarX) have2_tr (in=y);
	by VarX;
	if x=0 and y=1;
	call symputx(VarX,"C0");
run;

/* Retrieve the list of words that are mentionned in have1 			*/
/* Put each value in a macrovariable with the value = varY's value  */

data _null_;
	set have1;
	call symputx(varX,varY);
run;

/* Output the results */

data want;
	set have2;
	varY1 = symget(scan(varX1,1));
	do i = 1 to countw(varX1,"/")-1;
		varY1 = catx("/",varY1,symget(scan(varX1,i+1,"/")));
	end;
	drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NB: The dataset have2_not_in_have1 will give you the list of values not referenced in have 1 and mentioned as C0 in the final dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 10:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611989#M178491</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-16T10:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611990#M178492</link>
      <description>&lt;P&gt;Hash-object to the rescue ... maybe ...&lt;/P&gt;
&lt;P&gt;Not sure that i understood the c0-problem at all, updated have-datesets would be useful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   if 0 then set have1;

   set have2;

   length varY1 $ 10;

   if _n_ = 1 then do;
      declare hash h(dataset: 'have1');
      h.defineKey('varx');
      h.defineData('vary');
      h.defineDone();
   end;

   varY1 = '';

   do i = 1 to countw(varx1, '/');
      varX = scan(varX1, i, '/');

      if h.find() = 0 then do;
         varY1 = catx('/', varY1, varY);
      end;
      else do;
         varY1 = catx('/', varY1, 'C0');
      end;
   end;

   keep VarX1 VarY1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 10:54:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611990#M178492</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-12-16T10:54:39Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611992#M178493</link>
      <description>&lt;P&gt;You can generate codes for the missing combinations as follows :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
    if _N_=0 then set have1; /* retrieve column formats */
    set have2;
    keep varX varY;
    do i=1 to countw(varX1,'/');
        varX=scan(varX1,i,'/');
        output;
    end;
run;

proc sql noprint;
    CREATE TABLE new_vals AS 
    SELECT DISTINCT * FROM temp
    WHERE varX NOT IN SELECT varX FROM have1;

    SELECT max(input(substr(varY,2),best.))+1 INTO :maxindex 
    FROM have1;
quit;

data new_vals (drop=index);
    set new_vals;
    if _N_=1 then index=symgetn('maxindex');
    varY=cats('C', index);
    index+1;
run;

proc append base=have1 data=new_vals;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 11:17:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611992#M178493</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-12-16T11:17:29Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611998#M178497</link>
      <description>&lt;P&gt;Here is another solution: Use SET with KEY= in a datastep:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input varX$ varY$;
cards;
abc C1
def C2
ghi C3
jkl C4
mno C5
;run;

data have2;
lengh varx1 $20;
input varX1;
cards;
abc/def
ghi/mno
jkl/def
abc/mno/uvx
ghi/pqr/jkl
;run;

proc sql;
  create index varx on have1(varx);
quit;

data want;
  set have2;
  do _N_=1 to countw(varX1,'/');
    varx=scan(varx1,_N_,'/');
    set have1 key=varx/unique;
    length varY1 $20;
    if _iorc_ then do;
      _error_=0;
      call catx('/',varY1,'C0');
      end;
    else call catx('/',VarY1,VarY);
    end;
  keep varX1 VarY1;
  run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 12:27:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/611998#M178497</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-12-16T12:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/612007#M178498</link>
      <description>&lt;P&gt;Sorry, Late to the party&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have1;
input varX$ varY$;
cards;
abc C1
def C2
ghi C3
jkl C4
mno C5
;

data have2;
input varX1$;
cards;
abc/def
ghi/mno
jkl/def
;

data want ;
 if _n_=1 then do;
 if 0 then set have1;
   dcl hash H (dataset:'have1',ordered:'y') ;
   h.definekey  ("vary") ;
   h.definedata ("varX","varY") ;
   h.definedone () ;
   dcl hiter hi('h');
 end;
set have2;
length want $50;
do while(hi.next()=0);
 if index(varx1, strip(varx))&amp;gt;0 then want=catx('/',want,varY);
end;
keep varx1 want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 13:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/612007#M178498</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-16T13:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Match the corresponding values and concatenate by '/'</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/612009#M178499</link>
      <description>&lt;P&gt;another Hash direct find&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have1;
input varX$ varY$;
cards;
abc C1
def C2
ghi C3
jkl C4
mno C5
;

data have2;
input varX1$;
cards;
abc/def
ghi/mno
jkl/def
;

data want ;
 if _n_=1 then do;
 if 0 then set have1;
   dcl hash H (dataset:'have1') ;
   h.definekey  ("varx") ;
   h.definedata ("varY") ;
   h.definedone();
 end;
set have2;
length want $50;
do _n_=1 to countw(varX1,'/');
 _t=scan(varx1,_n_,'/');
 if h.find(key:_t)=0 then want=catx('/',want,varY);
end;
keep varx1 want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 13:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Match-the-corresponding-values-and-concatenate-by/m-p/612009#M178499</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-12-16T13:14:22Z</dc:date>
    </item>
  </channel>
</rss>

