<?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: TRANWRD to replace numbers with characters in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510789#M137468</link>
    <description>What I meant was, the TRANWRD function is not treating value of second argument as distinct. Shouldn't the value enclosed in quotes in second argument be treated as a distinct value?</description>
    <pubDate>Tue, 06 Nov 2018 15:51:32 GMT</pubDate>
    <dc:creator>lbarwick</dc:creator>
    <dc:date>2018-11-06T15:51:32Z</dc:date>
    <item>
      <title>TRANWRD to replace numbers with characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510777#M137465</link>
      <description>&lt;P&gt;I have a column of data with character-formatted numbers, e.g. the value for a record could be "1, 2, 3, 10, 12". These numbers represent a character label, e.g. 1=One, 2=Two, 3=Three, 10=Ten, 12=Twelve. I am trying to use TRANWRD to replace the numbers with the character labels:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;char_data=tranwrd(char_data,'1','One');&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;char_data=tranwrd(char_data,'2','Two');&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;char_data=tranwrd(char_data,'3','Three');&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;char_data=tranwrd(char_data,'10','Ten');&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;char_data=tranwrd(char_data,'12','Twelve');&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;What I end up with is "One, Two, Three, One0, OneTwo" because I guess SAS is not treating the original 'numbers' as distinct values but rather using the first number and replacing that first (e.g. in the Ten and Twelve examples).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any way around this?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
length char_data $20.;
input char_data $;
datalines;
1,2,3,10,12
;
run;

data want;
length char_data $50.; format char_data $50.; informat char_data $50.;
set have;

char_data=tranwrd(char_data,'1','One');
char_data=tranwrd(char_data,'2','Two');
char_data=tranwrd(char_data,'3','Three');
char_data=tranwrd(char_data,'10','Ten');
char_data=tranwrd(char_data,'12','Twelve');

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Nov 2018 15:18:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510777#M137465</guid>
      <dc:creator>lbarwick</dc:creator>
      <dc:date>2018-11-06T15:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: TRANWRD to replace numbers with characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510786#M137467</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/20041"&gt;@lbarwick&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You store several numeric values as a (comma-separated) &lt;EM&gt;single&lt;/EM&gt; character value. Why are you surprised that "&lt;SPAN&gt;SAS is not treating the original 'numbers' as distinct values"?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;My recommendation: Store the "original 'numbers'" in a &lt;EM&gt;numeric&lt;/EM&gt; variable. It's easier to concatenate the results afterwards if necessary.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Example:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
infile datalines dsd;
input val @@;
datalines;
1,2,3,10,12
;

data want(drop=val);
length char_data $50;
do until(last);
  set temp end=last;
  char_data=catx(', ',char_data,propcase(put(val,words.)));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;         char_data

One, Two, Three, Ten, Twelve&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Nov 2018 15:47:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510786#M137467</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-11-06T15:47:23Z</dc:date>
    </item>
    <item>
      <title>Re: TRANWRD to replace numbers with characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510789#M137468</link>
      <description>What I meant was, the TRANWRD function is not treating value of second argument as distinct. Shouldn't the value enclosed in quotes in second argument be treated as a distinct value?</description>
      <pubDate>Tue, 06 Nov 2018 15:51:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510789#M137468</guid>
      <dc:creator>lbarwick</dc:creator>
      <dc:date>2018-11-06T15:51:32Z</dc:date>
    </item>
    <item>
      <title>Re: TRANWRD to replace numbers with characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510796#M137469</link>
      <description>&lt;P&gt;The TRANWRD function treats '1' as a (&lt;EM&gt;sub)string&lt;/EM&gt; of length 1, i.e., as&amp;nbsp;a single character. And this character is found and replaced in the string "..., &lt;STRONG&gt;1&lt;/STRONG&gt;0, ...". You could replace the longer substrings ('10', '12') first to avoid this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The function name "TRAN&lt;STRONG&gt;WRD&lt;/STRONG&gt;"&amp;nbsp;can be misleading because other functions referring to "words" (such as COUNT&lt;STRONG&gt;W&lt;/STRONG&gt; or FIND&lt;STRONG&gt;W&lt;/STRONG&gt;) use a specific definition of a "word" (involving delimiters). TRANWRD, however, does not use this concept. It is older than COUNTW and FINDW, so the confusion has "historical" reasons.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Nov 2018 16:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510796#M137469</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-11-06T16:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: TRANWRD to replace numbers with characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510799#M137471</link>
      <description>&lt;P&gt;classically put . Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/20041"&gt;@lbarwick&lt;/a&gt;&amp;nbsp; Not a solution but the demo(reverse trnwrd) clarifies&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;'s explanation&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 char_data $20.;
input char_data $;
datalines;
1,2,3,10,12
;
run;

data want;
length char_data $50.; format char_data $50.; informat char_data $50.;
set have;
char_data=tranwrd(char_data,'12','Twelve');
char_data=tranwrd(char_data,'10','Ten');
char_data=tranwrd(char_data,'1','One');
char_data=tranwrd(char_data,'2','Two');
char_data=tranwrd(char_data,'3','Three');
/*char_data=tranwrd(char_data,'10','Ten');*/
/*char_data=tranwrd(char_data,'12','Twelve');*/

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS Output&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;TABLE class="systitleandfootercontainer" border="0" summary="Page Layout" width="100%" frame="void" rules="none" cellspacing="1" cellpadding="1"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="c systemtitle"&gt;The SAS System&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;BR /&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;char_data&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;One,Two,Three,Ten,Twelve&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Tue, 06 Nov 2018 16:15:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510799#M137471</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-06T16:15:42Z</dc:date>
    </item>
    <item>
      <title>Re: TRANWRD to replace numbers with characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510800#M137472</link>
      <description>Reordering the tranwrd-calls could help solving the issue: start with replacing 12, then 11 etc&lt;BR /&gt;But the better solution is not having value list in a variable, such design create headaches sooner or even sooner.</description>
      <pubDate>Tue, 06 Nov 2018 16:17:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510800#M137472</guid>
      <dc:creator>error_prone</dc:creator>
      <dc:date>2018-11-06T16:17:04Z</dc:date>
    </item>
    <item>
      <title>Re: TRANWRD to replace numbers with characters</title>
      <link>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510813#M137476</link>
      <description>Thanks. Didn't think to put the 1-9 at the end. That seems to have worked. I realize the data are not ideal but in my use case it is the only option I have.</description>
      <pubDate>Tue, 06 Nov 2018 16:43:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/TRANWRD-to-replace-numbers-with-characters/m-p/510813#M137476</guid>
      <dc:creator>lbarwick</dc:creator>
      <dc:date>2018-11-06T16:43:52Z</dc:date>
    </item>
  </channel>
</rss>

