<?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: dynamic columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886299#M350231</link>
    <description>I do not</description>
    <pubDate>Tue, 25 Jul 2023 20:28:34 GMT</pubDate>
    <dc:creator>Ody</dc:creator>
    <dc:date>2023-07-25T20:28:34Z</dc:date>
    <item>
      <title>dynamic columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886292#M350228</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to parse a column called LINE that is delimited by spaces(' ').&lt;/P&gt;
&lt;P&gt;Example records:&lt;/P&gt;
&lt;P&gt;somedata1 somedata2&lt;/P&gt;
&lt;P&gt;somedata1 somedata2 somedata3&lt;/P&gt;
&lt;P&gt;somedata1&lt;/P&gt;
&lt;P&gt;somedate1 somedate2 somedate3 somedate4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number of delimiters is unknown. To handle this I'm taking the max count of spaces:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
  select max(countc(LINE,' ')) + 1 into: nums
  from testdata;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to create additional columns with a prefix of col_ for the max number of delimiters found in the LINE column. So far I have the following code but it's not quite working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data test;
set mydata;

  %macro cols;
    %do i = 1 %to &amp;amp;nums;
      col_&amp;amp;i = scan(LINE, &amp;amp;nums, ' ');
    %end;

  %mend cols;

  %cols;
run;&lt;/PRE&gt;
&lt;P&gt;I would expect the Test dataset to contain 4 additional columns called Col_1, Col_2, Col_3, and Col_4; each containing the delimited values from LINE.&lt;/P&gt;
&lt;P&gt;Put another way:&lt;/P&gt;
&lt;P&gt;Col_1 will contain the values equal to somedata1&lt;/P&gt;
&lt;P&gt;Col_2 will contain the values equal to somedata2&lt;/P&gt;
&lt;P&gt;Col_3 will contain the values equal to somedata3&lt;/P&gt;
&lt;P&gt;Col_4 will contain the values equal to somedata4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The results are getting close to the requirements but I'm missing something; its not quite right. I havent coded in SAS in a few years beyond simple CRUD stuff and am quite rusty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate any help with this, thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jul 2023 19:49:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886292#M350228</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2023-07-25T19:49:56Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886293#M350229</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long;
set have;
n = _n_;
do i = 1 to countw(line);
  col = scan(line,i);
  output;
end;
keep n col;
run;

proc transpose data=long out=want (drop=_name_);
by n;
var col;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jul 2023 20:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886293#M350229</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-25T20:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886295#M350230</link>
      <description>Do you have other columns you need to account for?</description>
      <pubDate>Tue, 25 Jul 2023 20:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886295#M350230</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-07-25T20:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886299#M350231</link>
      <description>I do not</description>
      <pubDate>Tue, 25 Jul 2023 20:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886299#M350231</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2023-07-25T20:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886319#M350236</link>
      <description>Your SCAN function refers to &amp;amp;nums when it should be using &amp;amp;i.</description>
      <pubDate>Tue, 25 Jul 2023 22:27:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886319#M350236</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-07-25T22:27:16Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886320#M350237</link>
      <description>&lt;P&gt;No need to write a macro.&amp;nbsp; Just use an array.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the macro variable to set the dimension of the array. Instead of counting spaces count the actual number of terms.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select max(countw(LINE,' ')) into :nums trimmed
  from testdata;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now use the macro variable to define the number of variables to create.&amp;nbsp; Make sure to tell SAS how LONG to make the variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  set testdata;
  array col_ [ &amp;amp;nums ] $50 ;
  do i=1 to countw(line,' ');
    col_[i] = scan(line,i,' ');
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jul 2023 23:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886320#M350237</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-25T23:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886331#M350244</link>
      <description>&lt;P&gt;This is exactly what i was looking for!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I guess I had the right pieces/idea, even have a few tries using arrays but couldn't get it to work. I was struggling (for more time than i care to admit) on how to execute it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you and to everyone else that offered some insight!&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 02:22:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-columns/m-p/886331#M350244</guid>
      <dc:creator>Ody</dc:creator>
      <dc:date>2023-07-26T02:22:53Z</dc:date>
    </item>
  </channel>
</rss>

