<?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: How do I replace a characters in a string with a value from another variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845351#M334194</link>
    <description>&lt;P&gt;Thank you for the double-trim. Will come in handy for replacing in a string&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 20 Nov 2022 19:19:12 GMT</pubDate>
    <dc:creator>Pili1100</dc:creator>
    <dc:date>2022-11-20T19:19:12Z</dc:date>
    <item>
      <title>How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845160#M334102</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to replace a generic part of a string and replace with value from one variable in another variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Pili1100_0-1668792504941.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77455i5A3F8BF1F489E818/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Pili1100_0-1668792504941.png" alt="Pili1100_0-1668792504941.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;



/*input*/
DATA input;
LENGTH LIB_TABLES $100;
INPUT LIB_TABLES;
DATALINES;
LIB_A.TABLE_1
LIB_B.TABLE_2
LIB_C.TABLE_3
;

RUN;

/*What I have*/
DATA have;
SET input;
LIB = SCAN( LIB_TABLES, 1, '.');
TABLES = REVERSE(SCAN(REVERSE(LIB_TABLES),1,'.'));
X_have= 'info_tables(LIBNAME=XXX, TABLE=XXX, Table info)';

RUN;



/*Desirable output*/
DATA want; 

LIB_TABLES = 'LIB_A.TABLE_1';
LIB		   = 'LIB_A';
TABLES	   = 'TABLE_1';
X_WANT	   = 'info_tables(LIBNAME=LIB_A,TABLE=TABLE_1, Table info)';
OUTPUT;

	
LIB_TABLES = 'LIB_B.TABLE_2';
LIB		   = 'LIB_B';
TABLES	   = 'TABLE_2';
X_WANT	   = 'info_tables(LIBNAME=LIB_B,TABLE=TABLE_2, Table info)';
OUTPUT;


LIB_TABLES = 'LIB_C.TABLE_3';
LIB		   = 'LIB_C';
TABLES	   = 'TABLE_3';
X_WANT	   = 'info_tables(LIBNAME=LIB_C,TABLE=TABLE_3, Table info)';
OUTPUT;
RUN;	




&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How should I do it?&lt;/P&gt;
&lt;P&gt;Many thanks,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2022 17:29:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845160#M334102</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2022-11-18T17:29:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845197#M334114</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; You are very close. Using one of the concatenate functions to join your text and your variable values will work. I'd recommend the CATT function, which trims blanks:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1668802112714.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77468i3850FE852D5CA275/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1668802112714.png" alt="Cynthia_sas_0-1668802112714.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I also simplified your SCAN, since using a -1 tells SCAN to start scanning from the other end of the variable string, which is what your REVERSES were doing.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2022 20:10:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845197#M334114</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2022-11-18T20:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845198#M334115</link>
      <description>&lt;P&gt;As you stated that you wanted to _replace_ a certain char group by a variable my&amp;nbsp;suggestion would be using the tranwrd function . ... note the trim(). This was suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; in&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Tranwrd-with-variables/td-p/391673" target="_self"&gt;this post&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*input*/ 
DATA input; 
LENGTH LIB_TABLES $100; 
INPUT LIB_TABLES; 
DATALINES; 
LIB_A.TABLE_1 
LIB_B.TABLE_2 
LIB_C.TABLE_3 
; 
RUN; 
 
/*What I have*/ 
DATA have; 
SET input; 
LIB = SCAN( LIB_TABLES, 1, '.'); 
TABLES = REVERSE(SCAN(REVERSE(LIB_TABLES),1,'.')); 
X_have= 'info_tables(LIBNAME=XXX, TABLE=YYY, Table info)'; 
x_want=tranwrd(tranwrd(x_have, "YYY", trim(TABLES)), "XXX", trim(LIB)); 
RUN; 
proc print data=have;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="fja_0-1668803351806.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77469i2981F655126438A8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="fja_0-1668803351806.png" alt="fja_0-1668803351806.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2022 20:56:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845198#M334115</guid>
      <dc:creator>fja</dc:creator>
      <dc:date>2022-11-18T20:56:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845205#M334118</link>
      <description>&lt;P&gt;To re-create your example does NOT require replacing any text.&lt;/P&gt;
&lt;P&gt;Just GENERATE the text.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA input;
  LENGTH LIB_TABLES $100;
  INPUT LIB_TABLES;
DATALINES;
LIB_A.TABLE_1
LIB_B.TABLE_2
LIB_C.TABLE_3
;

data want;
  set input;
  length lib $8 tables $32 x_want $100 ;
  lib=scan(lib_tables,1,'.');
  tables=scan(lib_tables,-1,'.');
  x_want=cats('info_tables(libname=',lib,',table=',tables,',Table info)');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs     LIB_TABLES       lib     tables                           x_want

 1     LIB_A.TABLE_1    LIB_A    TABLE_1    info_tables(libname=LIB_A,table=TABLE_1,Table info)
 2     LIB_B.TABLE_2    LIB_B    TABLE_2    info_tables(libname=LIB_B,table=TABLE_2,Table info)
 3     LIB_C.TABLE_3    LIB_C    TABLE_3    info_tables(libname=LIB_C,table=TABLE_3,Table info)
&lt;/PRE&gt;
&lt;P&gt;Do you have an example where you actually need to replace text?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2022 20:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845205#M334118</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-11-18T20:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845213#M334123</link>
      <description>&lt;P&gt;Pretty much the same as Tom already suggested with the one extension to also deal with one level table names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
  LENGTH LIB_TABLES $100;
  INPUT LIB_TABLES;
  DATALINES;
LIB_A.TABLE_1
LIB_B.TABLE_2
LIB_C.TABLE_3
TABLE4
;

data want;
  set have;
  length lib $8 tables $32 x_want $100;
  lib=scan(cats('WORK.',lib_tables),-2,'.');
  tables=scan(lib_tables,-1,'.');
  x_want=cats('info_tables(LIBNAME=',lib,', table=',tables,', Table info)');
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1668807737714.png" style="width: 558px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/77472i4D1A13D39BD93BAF/image-dimensions/558x141?v=v2" width="558" height="141" role="button" title="Patrick_0-1668807737714.png" alt="Patrick_0-1668807737714.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Nov 2022 21:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845213#M334123</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-11-18T21:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845350#M334193</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I got so many good solutions from the community. Thank you for helping me. This one works as charm!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":folded_hands:"&gt;🙏&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Nov 2022 19:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845350#M334193</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2022-11-20T19:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845351#M334194</link>
      <description>&lt;P&gt;Thank you for the double-trim. Will come in handy for replacing in a string&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Nov 2022 19:19:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845351#M334194</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2022-11-20T19:19:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845352#M334195</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Thank you for helping me. Another good solution.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;BR /&gt;Regarding your question,&amp;nbsp;&lt;EM&gt;Do you have an example where you actually need to replace text? &lt;/EM&gt;and the&amp;nbsp;&lt;/SPAN&gt;reason I need help with this because I was looking for a solution for a makro where I use lib and tables and I wanted to avoid to copy/pase the XXX.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It can be time consuming when it's 20 tables.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Nov 2022 19:25:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845352#M334195</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2022-11-20T19:25:38Z</dc:date>
    </item>
    <item>
      <title>Re: How do I replace a characters in a string with a value from another variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845353#M334196</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for expanding the solution, having WORK as default. Real nice&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":clapping_hands:"&gt;👏&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Nov 2022 19:28:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-replace-a-characters-in-a-string-with-a-value-from/m-p/845353#M334196</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2022-11-20T19:28:06Z</dc:date>
    </item>
  </channel>
</rss>

