<?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 Remapping a string variable into an unknown number of new variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394247#M277912</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a string variable called "illness" which contains a varying number of conditions separated by commas:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EX what I have:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Illness: "Heart Attack, Dementia, Cancer"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create a new variable for each condition with each condition&amp;nbsp;remapped to a new classification.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EX what I want:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Ilness1: "I20-I25 Ischaemic heart diseases" (i.e. Heart Attack)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Illness2: "F00-F09 Organic disorders"&amp;nbsp;&amp;nbsp;(i.e. Dementia)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Illness3: "C00-D48 Neoplasms"&amp;nbsp;&amp;nbsp;(i.e. Cancer)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I was thinking I could do a simple if then statement like the one below:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;EM&gt;data x;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;set x;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;If illness contains "Heart Attack" then illness1="I20-I25 Ischaemic heart diseases";&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;EM&gt;If illness contains "Dementia" then&amp;nbsp;illness2="F00-F09 Organic disorders";&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;EM&gt;If illness contains "Cancer" then&amp;nbsp;illness3="C00-D48 Neoplasms";&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;.....&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;run;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, this would be very time consuming seeing that the variable contains a varying number of conditions... Any suggestions for a more efficient&amp;nbsp;way to achieve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your insight!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Sep 2017 15:30:35 GMT</pubDate>
    <dc:creator>vgrywacheski</dc:creator>
    <dc:date>2017-09-08T15:30:35Z</dc:date>
    <item>
      <title>Remapping a string variable into an unknown number of new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394247#M277912</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a string variable called "illness" which contains a varying number of conditions separated by commas:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EX what I have:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Illness: "Heart Attack, Dementia, Cancer"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create a new variable for each condition with each condition&amp;nbsp;remapped to a new classification.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EX what I want:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Ilness1: "I20-I25 Ischaemic heart diseases" (i.e. Heart Attack)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Illness2: "F00-F09 Organic disorders"&amp;nbsp;&amp;nbsp;(i.e. Dementia)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Illness3: "C00-D48 Neoplasms"&amp;nbsp;&amp;nbsp;(i.e. Cancer)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I was thinking I could do a simple if then statement like the one below:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;EM&gt;data x;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;set x;&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;If illness contains "Heart Attack" then illness1="I20-I25 Ischaemic heart diseases";&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;EM&gt;If illness contains "Dementia" then&amp;nbsp;illness2="F00-F09 Organic disorders";&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;EM&gt;If illness contains "Cancer" then&amp;nbsp;illness3="C00-D48 Neoplasms";&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;.....&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;run;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, this would be very time consuming seeing that the variable contains a varying number of conditions... Any suggestions for a more efficient&amp;nbsp;way to achieve this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your insight!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2017 15:30:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394247#M277912</guid>
      <dc:creator>vgrywacheski</dc:creator>
      <dc:date>2017-09-08T15:30:35Z</dc:date>
    </item>
    <item>
      <title>Re: Remapping a string variable into an unknown number of new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394248#M277913</link>
      <description>&lt;P&gt;You can use the SCAN() function to separate words if they're comma delimited as in your example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also output them out to a 'long format' data set and then do a merge or lookup to get the full codes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use COUNTC to count the number of commas and see how many words you need to parse out.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2017 15:37:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394248#M277913</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-09-08T15:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: Remapping a string variable into an unknown number of new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394376#M277914</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/74415"&gt;@vgrywacheski&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;And to give you some possible sample code for what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;describes.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  id=1;
  have_str='Heart  Attack, Dementia, Cancer, something else'; 
run;

proc format;
  value $StandIll
    HEART ATTACK    ='I20-I25 Ischaemic heart diseases" (i.e. Heart Attack)'
    DEMENTIA        ='F00-F09 Organic disorders"  (i.e. Dementia)'
    CANCER          ='C00-D48 Neoplasms"  (i.e. Cancer)'
    ;
quit;

data want(drop=_:);
  set have;
  _cnt=countc(have_str,',');
  do _i=0 to _cnt;
    term=scan(have_str,_i+1,',');
    term=upcase(compbl(strip(term)));
    want_str=put(term,$StandIll.);
    found_flg = (want_str ne term);
    output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;N.B: If you're after actually working sample code then the best way to motivate forum members to provide you with such code is to provide sample data via a working SAS data step creating such data. It's also highly appreciated if you describe what you've already tried and got stuck with. And of course as you've done it: Describe the desired result.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Sep 2017 23:46:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394376#M277914</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-09-08T23:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: Remapping a string variable into an unknown number of new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394406#M277915</link>
      <description>&lt;P&gt;You could use an indexed&amp;nbsp;dataset&amp;nbsp;with key= access&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE style="width: 816px; height: 1px;"&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&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;data have;
patient = 1;
Illnesses = "Heart Attack, Dementia, Cancer";
output;
patient = 2;
Illnesses = "Heart Attack, Tumor, Booboo";
output;
run;

data d2(index = (illness/unique));
infile datalines dsd;
length label illness $32;
input label &amp;amp;  Illness &amp;amp;;
illness = upcase(illness);
datalines;
I20-I25 Ischaemic heart diseases, Heart Attack
F00-F09 Organic disorders, Dementia
C00-D48 Neoplasms, Cancer
C00-D48 Neoplasms, Tumor
;

data want;
set have;
length illness $32;
array a $32 illness1 - illness10;
do i = 1 to countw(Illnesses, ",");
    illness = upcase(scan(Illnesses, i, ",", "R"));
    set d2 key=illness;
    if _iorc_ = 0 then a{i} = label;
    else a{i} = "*** Unknown ***";
    end;
keep patient illness1-illness10;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Sep 2017 04:58:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394406#M277915</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-09-09T04:58:52Z</dc:date>
    </item>
    <item>
      <title>Re: Remapping a string variable into an unknown number of new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394581#M277916</link>
      <description>&lt;P&gt;%let illness1=%str('HEART ATTACK','CARDIAC');&lt;/P&gt;&lt;P&gt;%let illness2=%str('DEMENTIA','AMNESIA');&lt;/P&gt;&lt;P&gt;%let illness3=%str('CANCER','CIRRHOSIS');&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data &amp;nbsp;WANT;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set HAVE;&lt;/P&gt;&lt;P&gt;&amp;nbsp; length ILLNESS1 ILLNESS2 ILLNESS3 $30;&lt;/P&gt;&lt;P&gt;&amp;nbsp; ILLNESS1=ifc(upcase(ILLNESS) in (&amp;amp;illness1),&lt;STRONG&gt;"I20-I25 Ischaemic heart diseases","");&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; ILLNESS2=ifc(upcase(ILLNESS) in (&amp;amp;illness2),&lt;STRONG&gt;"F00-F09 Organic disorders"&lt;/STRONG&gt;&lt;STRONG&gt;,"");&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; ILLNESS3=ifc(upcase(ILLNESS) in (&amp;amp;illness3),&lt;STRONG&gt;"C00-D48 Neoplasms"&lt;/STRONG&gt;&lt;STRONG&gt;,"");&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 07:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394581#M277916</guid>
      <dc:creator>ShiroAmada</dc:creator>
      <dc:date>2017-09-11T07:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: Remapping a string variable into an unknown number of new variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394669#M277917</link>
      <description>&lt;P&gt;Thank you so much Patrick! This is exactly what I needed &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2017 13:28:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remapping-a-string-variable-into-an-unknown-number-of-new/m-p/394669#M277917</guid>
      <dc:creator>vgrywacheski</dc:creator>
      <dc:date>2017-09-11T13:28:18Z</dc:date>
    </item>
  </channel>
</rss>

