<?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: Translating list of codes to descriptive text in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710349#M218652</link>
    <description>&lt;P&gt;A small adaptation of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; 's code will insert empty descriptions into the list:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc format;
value $codelist
1 = "Meaning for Code 1"
2 = "Meaning for Code 2"
3 = "Meaning for Code 3"
4 = "Meaning for Code 4"
5 = "Meaning for Code 5"
6 = "Meaning for Code 6"
7 = "Meaning for Code 7"
8 = "Meaning for Code 8"
" " = "_"
;
run;

Data have;
   input list $;
datalines;
1,2,3
.
4,1,,6
;

data want;
   set have;
   length description $ 140;
   /* the 140 above is just an example for the length
      of the variable. You should set it to the sum of
      the number of characters in the 9 codes plus 8 for the 
      commas
   */
   do i= 1 to countw(list,',',"m");
      description = catx(',',description,Put(scan(list,i,',',"m"),$codelist.) );
   end;
   description = translate(description, " ", "_");
   drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1610168683757.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53354i390FF6A74F1D0C53/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1610168683757.png" alt="PGStats_0-1610168683757.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 09 Jan 2021 05:05:27 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2021-01-09T05:05:27Z</dc:date>
    <item>
      <title>Translating list of codes to descriptive text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710325#M218635</link>
      <description>&lt;P&gt;I have a character field called &lt;STRONG&gt;EXPLNTN_TYP_NUM_LIST&lt;/STRONG&gt;&amp;nbsp; that contains a list of ‘explanation’ code(s) separated by commas – i.e. “1,2, &amp;lt;x&amp;gt;, ..”. – that need to be translated into descriptive text(s) separated by commas – i.e. “explanation1, explanation2, &amp;lt;explanationx&amp;gt;, ..”.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To make it a little more interesting, &amp;nbsp;EXPLNTN_TYP_NUM_LIST can have up to a maximum of 9 possible explanation codes &lt;U&gt;or&lt;/U&gt; the field could be blank/missing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, the question is:&amp;nbsp; how do I translate this list of codes in EXPLNTN_TYP_NUM_LIST to another field – say EXPLNTN_TYP_NUM_LIST_DESC – when I don’t know if the field will contain either a blank, 1 code or multiple codes separated by commas (up to 9 codes).&amp;nbsp;&amp;nbsp; Keep in mind that if the EXPLNTN_TYP_NUM_LIST field is blank/missing, I want to return a blank/missing value as a descriptive text.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note:&amp;nbsp; I have built a character format table using PROC FORMAT called $EXPLNTN to build out the code translations.&amp;nbsp; There are 16 possible code values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure how to do this using Base SAS.&amp;nbsp; Any help would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jan 2021 23:22:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710325#M218635</guid>
      <dc:creator>DougCole</dc:creator>
      <dc:date>2021-01-08T23:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: Translating list of codes to descriptive text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710328#M218638</link>
      <description>&lt;P&gt;Use a loop and CATX.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Find the number of items, use COUNTC/COUNTW&lt;/P&gt;
&lt;P&gt;2. Loop over items to extract each part&lt;/P&gt;
&lt;P&gt;3. Concatenate to main string&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Make sure length is long enough to work;
length stringDesc $200.;

*counts number of words, you may need to increment by 1 or account for 0 with an IF statement;
nWords = countw(string);

do i=1 to nWords;
    x =scan(string, i);
   stringDesc = catx(", ", stringDesc, put(code, formatName.));
end;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363221"&gt;@DougCole&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a character field called &lt;STRONG&gt;EXPLNTN_TYP_NUM_LIST&lt;/STRONG&gt;&amp;nbsp; that contains a list of ‘explanation’ code(s) separated by commas – i.e. “1,2, &amp;lt;x&amp;gt;, ..”. – that need to be translated into descriptive text(s) separated by commas – i.e. “explanation1, explanation2, &amp;lt;explanationx&amp;gt;, ..”.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make it a little more interesting, &amp;nbsp;EXPLNTN_TYP_NUM_LIST can have up to a maximum of 9 possible explanation codes &lt;U&gt;or&lt;/U&gt; the field could be blank/missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, the question is:&amp;nbsp; how do I translate this list of codes in EXPLNTN_TYP_NUM_LIST to another field – say EXPLNTN_TYP_NUM_LIST_DESC – when I don’t know if the field will contain either a blank, 1 code or multiple codes separated by commas (up to 9 codes).&amp;nbsp;&amp;nbsp; Keep in mind that if the EXPLNTN_TYP_NUM_LIST field is blank/missing, I want to return a blank/missing value as a descriptive text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note:&amp;nbsp; I have built a character format table using PROC FORMAT called $EXPLNTN to build out the code translations.&amp;nbsp; There are 16 possible code values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure how to do this using Base SAS.&amp;nbsp; Any help would be greatly appreciated.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jan 2021 00:12:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710328#M218638</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-01-09T00:12:57Z</dc:date>
    </item>
    <item>
      <title>Re: Translating list of codes to descriptive text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710329#M218639</link>
      <description>&lt;P&gt;Just use SCAN() and CATX() functions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
   length new_var $400 ;
   do index=1 to countw(EXPLNTN_TYP_NUM_LIST,',');
       new_var=catx(',',new_var,put(strip(scan(EXPLNTN_TYP_NUM_LIST,index,',')),&lt;SPAN&gt;$EXPLNTN.&lt;/SPAN&gt;));
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Jan 2021 00:13:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710329#M218639</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-01-09T00:13:33Z</dc:date>
    </item>
    <item>
      <title>Re: Translating list of codes to descriptive text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710331#M218641</link>
      <description>&lt;P&gt;One way, not the only way.&lt;/P&gt;
&lt;PRE&gt;Proc format;
value $codelist
1 = "Meaning for Code 1"
2 = "Meaning for Code 2"
3 = "Meaning for Code 3"
4 = "Meaning for Code 4"
5 = "Meaning for Code 5"
6 = "Meaning for Code 6"
7 = "Meaning for Code 7"
8 = "Meaning for Code 8"
;
run;

Data have;
   input list $;
datalines;
1,2,3
.
4,1,3,6
;

data want;
   set have;
   length description $ 140;
   /* the 140 above is just an example for the length
      of the variable. You should set it to the sum of
      the number of characters in the 9 codes plus 8 for the 
      commas
   */
   do i= 1 to countw(list,',');
      description = catx(',',description,Put(scan(list,i,','),$codelist.) );
   end;
   drop i;
run;

&lt;/PRE&gt;
&lt;P&gt;Custom formats are one way to do look ups like this.&lt;/P&gt;
&lt;P&gt;The Countw function counts "words" separated by only a comma in this case, other and multiple delimiters could be used. Then for each "word" or code value use the CATX function to concatenate the list with the Put value of the current code. CATX will use the first character in the parameters to separate the values, so a comma. SCAN extracts the given numbered list item, and then the Put applies the formatted value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since the Description starts out empty in each iteration of the data step using it to concatenate is an empty value and the CATX function will suppress all blanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the list is missing or all blanks then Countw returns 0 and since the Do loop counter i starts at 1 it exceeds the upper bound and does not execute.&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jan 2021 00:18:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710331#M218641</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-09T00:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: Translating list of codes to descriptive text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710349#M218652</link>
      <description>&lt;P&gt;A small adaptation of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; 's code will insert empty descriptions into the list:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc format;
value $codelist
1 = "Meaning for Code 1"
2 = "Meaning for Code 2"
3 = "Meaning for Code 3"
4 = "Meaning for Code 4"
5 = "Meaning for Code 5"
6 = "Meaning for Code 6"
7 = "Meaning for Code 7"
8 = "Meaning for Code 8"
" " = "_"
;
run;

Data have;
   input list $;
datalines;
1,2,3
.
4,1,,6
;

data want;
   set have;
   length description $ 140;
   /* the 140 above is just an example for the length
      of the variable. You should set it to the sum of
      the number of characters in the 9 codes plus 8 for the 
      commas
   */
   do i= 1 to countw(list,',',"m");
      description = catx(',',description,Put(scan(list,i,',',"m"),$codelist.) );
   end;
   description = translate(description, " ", "_");
   drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1610168683757.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53354i390FF6A74F1D0C53/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1610168683757.png" alt="PGStats_0-1610168683757.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Jan 2021 05:05:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710349#M218652</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-01-09T05:05:27Z</dc:date>
    </item>
    <item>
      <title>Re: Translating list of codes to descriptive text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710691#M218829</link>
      <description>Thanks everyone. This worked like a charm! Much appreciated.&lt;BR /&gt;</description>
      <pubDate>Mon, 11 Jan 2021 22:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710691#M218829</guid>
      <dc:creator>DougCole</dc:creator>
      <dc:date>2021-01-11T22:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: Translating list of codes to descriptive text</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710802#M218878</link>
      <description>&lt;P&gt;Thanks for your help.&amp;nbsp; This is exactly what I needed!&lt;/P&gt;&lt;P&gt;Cheers.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 14:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Translating-list-of-codes-to-descriptive-text/m-p/710802#M218878</guid>
      <dc:creator>DougCole</dc:creator>
      <dc:date>2021-01-12T14:13:25Z</dc:date>
    </item>
  </channel>
</rss>

