<?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: Iterate over a macro list of text expressions? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489718#M127938</link>
    <description>This worked great - thank you!</description>
    <pubDate>Fri, 24 Aug 2018 17:48:53 GMT</pubDate>
    <dc:creator>ap1994</dc:creator>
    <dc:date>2018-08-24T17:48:53Z</dc:date>
    <item>
      <title>Iterate over a macro list of text expressions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489713#M127934</link>
      <description>&lt;P&gt;Version: SAS 9.4 TS Level 1M3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to take a categorical variable wherein the value of each observation can be 1-8, and each number represents a category, and assign the text string category each numerical value represents into a new variable. The reason I am trying to do this is to assign the category that corresponds to each number as the text expression each number represents in a new variable. I am trying to use a Do loop and the scan() function to so that for each possible value of i&amp;nbsp;in the categorical variable, the new variable will return the string from the responses list at the ith position. My code is below; the issue I am seeing with this code is that the resulting output only provides the 1st character in each string. Is there a way to use a Do loop and macro list that will assign the category to a new variable?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let responses = CR|PR|RELAPSE_lt12|RELAPSE_gt12|CLEAR| 100_DAY_MORTALITY|POST_TX_COMPLICATION|DEATH|UNKNOWN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Q71 is the variable with values 1-8 that represents one of the categories in &amp;amp;responses.*/&lt;BR /&gt;data HSCT_response_cat;&lt;BR /&gt;set HSCT_response;&lt;BR /&gt;response_cat = "";&lt;BR /&gt;do i = 1 to 8;&lt;BR /&gt;if Q71 = i then response_cat = scan("&amp;amp;responses.", i, '|');&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated!&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 17:32:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489713#M127934</guid>
      <dc:creator>ap1994</dc:creator>
      <dc:date>2018-08-24T17:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate over a macro list of text expressions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489715#M127936</link>
      <description>&lt;P&gt;Could you provide some sample data and expected output for us to verify your code/logic?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also need to assign a length to the response_cat variable ahead of time. That alone may resolve your issues.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data HSCT_response_cat;
set HSCT_response;
&lt;FONT size="4" color="#339966"&gt;&lt;STRONG&gt;length response_cat $20.;&lt;/STRONG&gt;&lt;/FONT&gt;
response_cat = "";
do i = 1 to 8;
if Q71 = i then response_cat = scan("&amp;amp;responses.", i, '|');
end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Aug 2018 17:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489715#M127936</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-24T17:39:20Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate over a macro list of text expressions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489718#M127938</link>
      <description>This worked great - thank you!</description>
      <pubDate>Fri, 24 Aug 2018 17:48:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489718#M127938</guid>
      <dc:creator>ap1994</dc:creator>
      <dc:date>2018-08-24T17:48:53Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate over a macro list of text expressions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489744#M127950</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228225"&gt;@ap1994&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Version: SAS 9.4 TS Level 1M3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to take a categorical variable wherein the value of each observation can be 1-8, and each number represents a category, and assign the text string category each numerical value represents into a new variable. The reason I am trying to do this is to assign the category that corresponds to each number as the text expression each number represents in a new variable. I am trying to use a Do loop and the scan() function to so that for each possible value of i&amp;nbsp;in the categorical variable, the new variable will return the string from the responses list at the ith position. My code is below; the issue I am seeing with this code is that the resulting output only provides the 1st character in each string. Is there a way to use a Do loop and macro list that will assign the category to a new variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let responses = CR|PR|RELAPSE_lt12|RELAPSE_gt12|CLEAR| 100_DAY_MORTALITY|POST_TX_COMPLICATION|DEATH|UNKNOWN;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*Q71 is the variable with values 1-8 that represents one of the categories in &amp;amp;responses.*/&lt;BR /&gt;data HSCT_response_cat;&lt;BR /&gt;set HSCT_response;&lt;BR /&gt;response_cat = "";&lt;BR /&gt;do i = 1 to 8;&lt;BR /&gt;if Q71 = i then response_cat = scan("&amp;amp;responses.", i, '|');&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is much appreciated!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What you seem to be doing is the role of a custom format. You can display pretty much any given text for a given value. Without adding any additional variables. Plus if the data adds codes you only have to change the format definition to apply the new value.&lt;/P&gt;
&lt;P&gt;Also you can create groups of code values that will be honored by analysis graphing procedures generally.&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value code
1="CR"
2="PR"
3="RELAPSE_lt12"
4="RELAPSE_gt12"
5="CLEAR"
6="100_DAY_MORTALITY"
7="POST_TX_COMPLICATION"
8="DEATH"
9="UNKNOWN"
;
run;

data example;
   do i=1 to 9;
      output;
   end;
run;

proc print data=example;
  var i;
  format i code.;
run;
proc format library=work;
value codegrp
1,2 = "CR/PR"
3,4 = "Relapse"
5="CLEAR"
6,8="MORTALITY"
7="POST_TX_COMPLICATION"
9="UNKNOWN"
;
run;
proc freq data=example;
   tables i;
   format i codegrp.;
run;

   &lt;/PRE&gt;
&lt;P&gt;If you have a format and really need a text variable then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;newvar = put(variablename, formatname.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The proc format code is often much easier to assign ranges and has a number of other options that are well worth learning.&lt;/P&gt;
&lt;P&gt;In some cases if you have a good document explaining your variables and values you may also create formats from a data set.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 19:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iterate-over-a-macro-list-of-text-expressions/m-p/489744#M127950</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-08-24T19:54:29Z</dc:date>
    </item>
  </channel>
</rss>

