<?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: Find and replace multiple values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923653#M363628</link>
    <description>&lt;P&gt;agree..&amp;nbsp;unfortunately that is the format it is required in to feed another process. I have to align to the requirement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 09 Apr 2024 17:27:03 GMT</pubDate>
    <dc:creator>Anuz</dc:creator>
    <dc:date>2024-04-09T17:27:03Z</dc:date>
    <item>
      <title>Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923638#M363619</link>
      <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the best way to find and replace values in a field separated by , from values in a mapping table&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
	length R_TYP $250. ;
	infile datalines ;
	input R_TYP;
	datalines;
son,daughter
uncle
aunty
son,uncle
;
run;


DATA TEST_MAPPING;
	length K_TYP $100. S_TYP $100.;
	infile datalines delimiter=',' dsd truncover;
	input K_TYP  S_TYP;
	datalines;
son,family
daughter,family
uncle,extended family
aunty,extended family
;
run;

DATA WANT;
	length R_TYP $250. ;
	infile datalines ;
	input R_TYP;
	datalines;
family,family
extended family
extended family
family,extended family
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output in WANT table is what I need as my final output by checking values in the MAPPING table -&amp;nbsp;&lt;CODE class=" language-sas"&gt;TEST_MAPPING&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 15:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923638#M363619</guid>
      <dc:creator>Anuz</dc:creator>
      <dc:date>2024-04-09T15:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923642#M363620</link>
      <description>&lt;P&gt;Can you state some rules on what to find where? And the replacement rules?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example without any description of the rules involves leads to lots of guessing and back-and-forth of "that isn't correct" and "why is it not correct".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most of the time any variable that has multiple values in it for a single observation points to sub-optimal data structure which can cause lots of extra work to get anything done. Why are there multiple values?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 15:41:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923642#M363620</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-04-09T15:41:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923643#M363621</link>
      <description>&lt;P&gt;It will be MUCH easier if you convert you delimited list of values into multiple observations instead.&lt;/P&gt;
&lt;P&gt;So given your two input datasets (let's call the second one LOOKUP instead of TEST).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover;
  input R_TYP $250.;
datalines;
son,daughter
uncle
aunty
son,uncle
;

data lookup ;
  infile datalines dsd dsd truncover;
  input K_TYP :$100.  S_TYP :$100.;
datalines;
son,family
daughter,family
uncle,extended family
aunty,extended family
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;First fix the WANT dataset to have one observation per value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fixed;
  row+1;
  set have;
  do col=1 to max(1,countw(r_typ,','));
    length k_typ $100;
    k_typ = left(scan(r_typ,col,','));
    output;
  end;
  drop r_typ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can combine on the common variable K_TYP.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
create table matched as
  select a.*,b.s_typ
  from fixed a 
    left join lookup b
    on a.k_typ = b.k_typ
  order by row,col
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Resuls&lt;/P&gt;
&lt;PRE&gt;Obs    row    col    k_typ       S_TYP

 1      1      1     son         family
 2      1      2     daughter    family
 3      2      1     uncle       extended family
 4      3      1     aunty       extended family
 5      4      1     son         family
 6      4      2     uncle       extended family
&lt;/PRE&gt;
&lt;P&gt;And if you did want to rebuild that delimited list it can be simply done with another data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.row);
    set matched;
    by row col;
    length r_typ $250;
    r_typ=catx(',',r_typ,s_typ);
  end;
  keep row r_typ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    row    r_typ

 1      1     family,family
 2      2     extended family
 3      3     extended family
 4      4     family,extended family
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 15:43:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923643#M363621</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-09T15:43:47Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923645#M363622</link>
      <description>The HAVE table is what I receive. &lt;BR /&gt;WANT table is my desired output. &lt;BR /&gt;&lt;BR /&gt;For example a value of son should be replaced by family from the TEST_MAPPING table. &lt;BR /&gt;If the column has multiple values like son, uncle. My output should say family, extended family as replaced values&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Apr 2024 16:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923645#M363622</guid>
      <dc:creator>Anuz</dc:creator>
      <dc:date>2024-04-09T16:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923647#M363623</link>
      <description>&lt;P&gt;Thank you Tom. Let me try that/&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 16:08:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923647#M363623</guid>
      <dc:creator>Anuz</dc:creator>
      <dc:date>2024-04-09T16:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923648#M363624</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/382230"&gt;@Anuz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;The HAVE table is what I receive. &lt;BR /&gt;WANT table is my desired output. &lt;BR /&gt;&lt;BR /&gt;For example a value of son should be replaced by family from the TEST_MAPPING table. &lt;BR /&gt;If the column has multiple values like son, uncle. My output should say family, extended family as replaced values&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Just because someone gives you poorly structured data is no reason to keep such a structure.&lt;/P&gt;
&lt;P&gt;If you provide accurate analysis or reporting then what happens in between receipt of data and the report shouldn't matter.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 16:15:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923648#M363624</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-04-09T16:15:58Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923653#M363628</link>
      <description>&lt;P&gt;agree..&amp;nbsp;unfortunately that is the format it is required in to feed another process. I have to align to the requirement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 17:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923653#M363628</guid>
      <dc:creator>Anuz</dc:creator>
      <dc:date>2024-04-09T17:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923654#M363629</link>
      <description>&lt;P&gt;If the data is smallish just do the code I posted before as it is much clearer what is going on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the data is large you might get better performance by doing the transformation in one step.&amp;nbsp; Essentially something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length temp $250 ;
  do col=1 to countw(r_typ,',');
     temp=catx(',',temp,put(left(scan(r_type,col,',')),$group.));
  end;
  r_typ=temp;
  drop temp col;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where $GROUP is a format you generated from your LOOKUP table that would map 'son' to 'family' and 'uncle' to 'extended family'.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 17:35:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923654#M363629</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-04-09T17:35:33Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923658#M363631</link>
      <description>&lt;P&gt;A bit more of a verbose solution but easy to follow and modify.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    infile datalines truncover;
    input R_TYP $250.;
    datalines;
son,daughter
uncle
aunty
son,uncle
;

data lookup;
    infile datalines dsd dsd truncover;
    input K_TYP :$100. S_TYP :$100.;
    datalines;
son,family
daughter,family
uncle,extended family
aunty,extended family
;
    
    
    
*find number of values per row for array dimensions;
data _null_;
    set have end=eof;
    retain max_num 0;
    n_words=countc(r_typ, ',')+1;
    max_num=max(n_words, max_num);

    if eof then
        call symputx('num_words', n_words);
run;

%put &amp;amp;num_words;

*create format for mapping;
data lookup_fmt;
    set lookup;
    start=k_typ;
    label=s_typ;
    type='C';
    fmtname='family_fmt';
run;

proc format cntlin=lookup_fmt;
run;

*map codes to new values;
data mapped_summarized;
    set have;
    array _original(&amp;amp;num_words.) $100.;
    array _mapped(&amp;amp;num_words.) $100.;

    do i=1 to countc(r_typ, ",")+1;
        _original(i)=trim(scan(r_typ, i, ','));
        _mapped(i)=put(_original(i), $family_fmt.);
    end;
    
    *concatenate output as desired;
    r_typ_new=catx(", ", of _mapped(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/382230"&gt;@Anuz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the best way to find and replace values in a field separated by , from values in a mapping table&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
	length R_TYP $250. ;
	infile datalines ;
	input R_TYP;
	datalines;
son,daughter
uncle
aunty
son,uncle
;
run;


DATA TEST_MAPPING;
	length K_TYP $100. S_TYP $100.;
	infile datalines delimiter=',' dsd truncover;
	input K_TYP  S_TYP;
	datalines;
son,family
daughter,family
uncle,extended family
aunty,extended family
;
run;

DATA WANT;
	length R_TYP $250. ;
	infile datalines ;
	input R_TYP;
	datalines;
family,family
extended family
extended family
family,extended family
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output in WANT table is what I need as my final output by checking values in the MAPPING table -&amp;nbsp;&lt;CODE class=" language-sas"&gt;TEST_MAPPING&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 17:53:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923658#M363631</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2024-04-09T17:53:32Z</dc:date>
    </item>
    <item>
      <title>Re: Find and replace multiple values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923695#M363645</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
	length R_TYP $250. ;
	infile datalines ;
	input R_TYP;
	datalines;
son,daughter
uncle
aunty
son,uncle
;
run;


DATA TEST_MAPPING;
	length K_TYP $100. S_TYP $100.;
	infile datalines delimiter=',' dsd truncover;
	input K_TYP  S_TYP;
	datalines;
son,family
daughter,family
uncle,extended family
aunty,extended family
;
run;

data want;
 if _n_=1 then do;
   if 0 then set TEST_MAPPING;
   declare hash h(dataset:'TEST_MAPPING');
   h.definekey('K_TYP');
   h.definedata('S_TYP');
   h.definedone();
 end;
set have;
length want temp $ 200;
do i=1 to countw(R_TYP,',');
 temp=scan(R_TYP,i,',');
 if h.find(key:temp)=0 then want=catx(',',want,S_TYP);
  else want=catx(',',want,temp);
end;
keep want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Apr 2024 02:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-and-replace-multiple-values/m-p/923695#M363645</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-04-10T02:34:51Z</dc:date>
    </item>
  </channel>
</rss>

