<?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 each character in a string with lookup dataset value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/827027#M326676</link>
    <description>&lt;P&gt;Due to security reason we can't store it into macro variable.&lt;/P&gt;</description>
    <pubDate>Thu, 04 Aug 2022 00:09:50 GMT</pubDate>
    <dc:creator>Banu2318</dc:creator>
    <dc:date>2022-08-04T00:09:50Z</dc:date>
    <item>
      <title>find and replace each character in a string with lookup dataset value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/826777#M326568</link>
      <description>&lt;P&gt;I have dataset Account_details which is having account_id other dataset "lookup" like below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;unmasked masked&lt;/P&gt;&lt;P&gt;a m&lt;/P&gt;&lt;P&gt;b n&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;need to change account_id each character with this value like below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;old Account_ID&amp;nbsp; &amp;nbsp;new Account_ID&lt;/P&gt;&lt;P&gt;Abwok&amp;nbsp; &amp;nbsp; &amp;nbsp;mnwok&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 03:59:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/826777#M326568</guid>
      <dc:creator>Banu2318</dc:creator>
      <dc:date>2022-08-03T03:59:47Z</dc:date>
    </item>
    <item>
      <title>Re: find and replace each character in a string with lookup dataset value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/826788#M326576</link>
      <description>&lt;P&gt;Sounds like an annoying homework.&lt;/P&gt;
&lt;P&gt;Please note that "A" is not "a", so do you want to compare case-insensitive?&lt;/P&gt;
&lt;P&gt;Here's an idea, i assumed that "lookup" has only single chars for in both variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length account_id $ 10;
   input account_id;
   datalines;
Abwok
;

data lookup;
   length unmasked masked $ 1;
   input unmasked masked;
   datalines;
a m
b n
;

data lookup_format;
   set lookup(rename=(unmasked = start masked = label));
   retain FmtName '$Lookup' Type 'J';
run;

proc format cntlin=lookup_format;
run;

data want;
   set have;
   length new_account_id $ 10;
   
   new_account_id = account_id;
   
   do i = 1 to lengthn(account_id);
      substr(new_account_id, i, 1) = input(lowcase(char(new_account_id, i)), $Lookup.);
   end;
   
   drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 05:54:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/826788#M326576</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-08-03T05:54:55Z</dc:date>
    </item>
    <item>
      <title>Re: find and replace each character in a string with lookup dataset value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/826798#M326582</link>
      <description>&lt;P&gt;Here is an alternative. Also, I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;, seems like annoying homework &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
   select unmasked
        , masked
   into :u separated by ''
      , :m separated by ''
   from lookup
   ;
quit;

data want;
   set have;
   new_account_id = translate(lowcase(account_id), "&amp;amp;m.", "&amp;amp;u.");
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 06:49:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/826798#M326582</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-08-03T06:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: find and replace each character in a string with lookup dataset value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/826841#M326596</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   length account_id $ 10;
   input account_id;
   datalines;
Abwok
;

data lookup;
   length unmasked masked $ 1;
   input unmasked masked;
   datalines;
a m
b n
;

proc sql noprint;
select unmasked into :unmasked separated by '' from lookup;
select upcase(unmasked) into :unmasked2 separated by '' from lookup;

select masked into :masked separated by '' from lookup;
quit;

data want;
 set have;
 want=translate(account_id,"&amp;amp;masked.","&amp;amp;unmasked.");
 want=translate(want,"&amp;amp;masked.","&amp;amp;unmasked2.");
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 12:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/826841#M326596</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-03T12:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: find and replace each character in a string with lookup dataset value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/827027#M326676</link>
      <description>&lt;P&gt;Due to security reason we can't store it into macro variable.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 00:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/827027#M326676</guid>
      <dc:creator>Banu2318</dc:creator>
      <dc:date>2022-08-04T00:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: find and replace each character in a string with lookup dataset value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/827028#M326677</link>
      <description>its taking longer time to apply format for multiple columns.</description>
      <pubDate>Thu, 04 Aug 2022 00:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/827028#M326677</guid>
      <dc:creator>Banu2318</dc:creator>
      <dc:date>2022-08-04T00:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: find and replace each character in a string with lookup dataset value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/827031#M326679</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/389904"&gt;@Banu2318&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Due to security reason we can't store it into macro variable.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is no reason you couldn't use a macro variable.&amp;nbsp; I suspect the issue you have is you don't want the code/decode pairs printed to the SAS log, but you can easily prevent that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But there shouldn't be any need to store the code/decode strings into macro variables.&amp;nbsp; It will be easier to keep them in dataset variables anyway.&amp;nbsp; Your current structure is inefficient of use, so first transform it so you have FROM and TO variables you use with the TRANSLATE function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lookup_fixed;
  length from to $256 ;
  do until (eof);
    set lookup;
    substr(from,_n_,1)=unmasked;
    substr(to,_n_,1)=masked;
  end;
  drop unmasked masked;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now to transform your ACCOUNT_DETAILS dataset you can use something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data account_details_masked ;
  if _n_=1 then set lookup_fixed;
&amp;nbsp;&amp;nbsp;set&amp;nbsp;account_details;
&amp;nbsp;&amp;nbsp;account_id&amp;nbsp;=&amp;nbsp;translate(account_id,to,from);
  drop from to;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could easily extend that to multiple variables by using an ARRAY.&lt;/P&gt;
&lt;P&gt;For example if in addition to ACCOUNT_ID you also wanted to mask NAME and ADDRESS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data account_details_masked ;
  if _n_=1 then set lookup_fixed;
&amp;nbsp;&amp;nbsp;set&amp;nbsp;account_details;
  array mask account_id name address ;
  do index=1 to dim(mask);
  &amp;nbsp;&amp;nbsp;mask[index] =&amp;nbsp;translate(mask[index],to,from);
  end;
  drop from to index;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 02:05:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/find-and-replace-each-character-in-a-string-with-lookup-dataset/m-p/827031#M326679</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-04T02:05:23Z</dc:date>
    </item>
  </channel>
</rss>

