<?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 text to columns in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888224#M350948</link>
    <description>&lt;P&gt;&lt;SPAN&gt;Hi, I want to process a comma-separated list of values in a variable (e.g., Q8) and create corresponding response variables (Q8_1, Q8_2, ..., Q8_17). For each value in the list, I want to set the corresponding response variable to 1 if there is a value. If the value is missing, the corresponding response variable should remain blank. Here is an example of what the variable initially&amp;nbsp;looks like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Q8&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;11&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;15,17&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;11,12,13,14,15&lt;/P&gt;&lt;P&gt;1,2,3,4,5,6,7,8,9,10&lt;/P&gt;&lt;P&gt;11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried something like this, but it's not accounting correctly for values 10+. any suggestions? I am not married to this code, so if someone has a completely different one, I am okay with that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;data numeric2;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set numeric;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;%macro initial2(var, num);&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data numeric2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;format &amp;amp;var._1 - &amp;amp;var._&amp;amp;num 8.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set numeric2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%mend;&lt;/DIV&gt;&lt;DIV&gt;%macro pop_var(var, num);&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data numeric2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set numeric2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if index(&amp;amp;var, "&amp;amp;num")&amp;gt;0 then &amp;amp;var._&amp;amp;num = 1;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%mend pop_var;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%macro run_pop(var2, num2);&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%initial2(&amp;amp;var2, &amp;amp;num2);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%do i = 1 %to &amp;amp;num2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%pop_var(&amp;amp;var2, &amp;amp;i);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%mend;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%run_pop(Q8, 17);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Aug 2023 20:44:38 GMT</pubDate>
    <dc:creator>smh92</dc:creator>
    <dc:date>2023-08-07T20:44:38Z</dc:date>
    <item>
      <title>text to columns in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888224#M350948</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi, I want to process a comma-separated list of values in a variable (e.g., Q8) and create corresponding response variables (Q8_1, Q8_2, ..., Q8_17). For each value in the list, I want to set the corresponding response variable to 1 if there is a value. If the value is missing, the corresponding response variable should remain blank. Here is an example of what the variable initially&amp;nbsp;looks like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Q8&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;11&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;15,17&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;11,12,13,14,15&lt;/P&gt;&lt;P&gt;1,2,3,4,5,6,7,8,9,10&lt;/P&gt;&lt;P&gt;11&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried something like this, but it's not accounting correctly for values 10+. any suggestions? I am not married to this code, so if someone has a completely different one, I am okay with that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;data numeric2;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set numeric;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;%macro initial2(var, num);&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data numeric2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;format &amp;amp;var._1 - &amp;amp;var._&amp;amp;num 8.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set numeric2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%mend;&lt;/DIV&gt;&lt;DIV&gt;%macro pop_var(var, num);&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;data numeric2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set numeric2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;if index(&amp;amp;var, "&amp;amp;num")&amp;gt;0 then &amp;amp;var._&amp;amp;num = 1;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%mend pop_var;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%macro run_pop(var2, num2);&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%initial2(&amp;amp;var2, &amp;amp;num2);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%do i = 1 %to &amp;amp;num2;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%pop_var(&amp;amp;var2, &amp;amp;i);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;%end;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%mend;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%run_pop(Q8, 17);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 20:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888224#M350948</guid>
      <dc:creator>smh92</dc:creator>
      <dc:date>2023-08-07T20:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: text to columns in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888232#M350950</link>
      <description>&lt;P&gt;I think an ARRAY is a much better choice here at working with many columns in a data set, than macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    array q8 q8_1-q8_17;
    do i=1 to countw(string,',');
    	do j=1 to 17;
    	    if scan(string,i,',')=j then q8(j)=1;
	    end;
    end;
    drop i j;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;This sets the variables to 1 if that value is found, and missing if it is not found.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 21:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888232#M350950</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-08-07T21:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: text to columns in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888237#M350951</link>
      <description>&lt;P&gt;Thanks so much! Do you know why it's only creating the variables&amp;nbsp;q8_1-q8_17 but not populating 1 or blank?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 21:54:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888237#M350951</guid>
      <dc:creator>smh92</dc:creator>
      <dc:date>2023-08-07T21:54:15Z</dc:date>
    </item>
    <item>
      <title>Re: text to columns in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888245#M350955</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446528"&gt;@smh92&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks so much! Do you know why it's only creating the variables&amp;nbsp;q8_1-q8_17 but not populating 1 or blank?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You would have to provide data in the form of working data step AND the code that you actually ran to actually answer that.&lt;/P&gt;
&lt;P&gt;Possibly include the log with the code and any messages might provide a clue but data is the key.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My approach to YOUR requirement:&lt;/P&gt;
&lt;PRE&gt;data example;
   length q8 $ 55;
   input q8 $;
datalines;
11
.
.
.
15,17
.
11
11,12,13,14,15
1,2,3,4,5,6,7,8,9,10
;

data want;
   set example;
   array q(*) q8_1 - Q8_17;
   if not missing(q8) then do i=1 to countw(q8);
      index= input(strip(scan(q8,i)),5.);
      q[index]=1;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 22:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888245#M350955</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-07T22:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: text to columns in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888251#M350961</link>
      <description>&lt;P&gt;This works. Thanks again for your help!&lt;/P&gt;</description>
      <pubDate>Mon, 07 Aug 2023 23:01:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/text-to-columns-in-SAS/m-p/888251#M350961</guid>
      <dc:creator>smh92</dc:creator>
      <dc:date>2023-08-07T23:01:02Z</dc:date>
    </item>
  </channel>
</rss>

