<?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: creating multiple columns from rows in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68740#M19690</link>
    <description>Marius&lt;BR /&gt;
that's very good! Using catx() eliminates the leading delimiter caused by cat(), so eliminates the need to test for first. &lt;BR /&gt;
Applying that and the array approach greatly simplifies my DoW [pre]data temp2 ;&lt;BR /&gt;
    array arr_d(2) $50 arr_good arr_bad ;&lt;BR /&gt;
    do until( last.id ); * sometimes referred to as a DoW loop ;&lt;BR /&gt;
       set temp ;&lt;BR /&gt;
       by id ;&lt;BR /&gt;
       arr_d(source)=catx( ',', arr_d(source), val ) ;&lt;BR /&gt;
    end ;&lt;BR /&gt;
run;[/pre]no need to clear the arr_bad and good because no need to retain them, nor is there any need for explicit output because each data step iteration deals with just one ID value.</description>
    <pubDate>Fri, 04 Feb 2011 11:40:23 GMT</pubDate>
    <dc:creator>Peter_C</dc:creator>
    <dc:date>2011-02-04T11:40:23Z</dc:date>
    <item>
      <title>creating multiple columns from rows</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68735#M19685</link>
      <description>data temp ;&lt;BR /&gt;
input ID $ val source 8;&lt;BR /&gt;
datalines;&lt;BR /&gt;
V1   0 1&lt;BR /&gt;
V1  10 1&lt;BR /&gt;
V1 100 2&lt;BR /&gt;
V1 250 2&lt;BR /&gt;
V2  50 1&lt;BR /&gt;
V2  80 1&lt;BR /&gt;
V2 500 2&lt;BR /&gt;
V7  10 1&lt;BR /&gt;
V7  20 1&lt;BR /&gt;
V7  30 2&lt;BR /&gt;
V7  99 2&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
I need to create two seperate columns that comma delimit the values.  Based on the above table I need the following result.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
ID GOOD     BAD&lt;BR /&gt;
V1 '0, 10'     '100,250'&lt;BR /&gt;
V2 '50,80'     '500'&lt;BR /&gt;
V7 '10.20'     '30,99'&lt;BR /&gt;
&lt;BR /&gt;
This is the code I have so far&lt;BR /&gt;
data temp2 ;&lt;BR /&gt;
set temp ;&lt;BR /&gt;
by id ;&lt;BR /&gt;
retain arr_good ;&lt;BR /&gt;
length arr_good $ 50;&lt;BR /&gt;
&lt;BR /&gt;
arr_good=ifc( first.id,val,cats(arr_good,',',val));&lt;BR /&gt;
if last.id then output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
But this code puts all the rows for each id in one column.  I can't figure out how to check for the source to split it up into two separate columns.&lt;BR /&gt;
&lt;BR /&gt;
Thank You</description>
      <pubDate>Thu, 03 Feb 2011 17:26:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68735#M19685</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2011-02-03T17:26:17Z</dc:date>
    </item>
    <item>
      <title>Re: creating multiple columns from rows</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68736#M19686</link>
      <description>[pre]data temp2 ;&lt;BR /&gt;
 do until( last.id ); * sometimes referred to as a DoW loop ;&lt;BR /&gt;
    set temp ;&lt;BR /&gt;
    by id ;&lt;BR /&gt;
    length arr_good arr_bad $ 50;&lt;BR /&gt;
    if source = 1 then &lt;BR /&gt;
       arr_good=cats(arr_good,',',val) ;&lt;BR /&gt;
    else&lt;BR /&gt;
       arr_bad =cats(arr_bad ,',',val) ;&lt;BR /&gt;
 end ;&lt;BR /&gt;
 * now remove leading commas ;&lt;BR /&gt;
 arr_good=substr(arr_good,2) ;&lt;BR /&gt;
 arr_bad =substr(arr_bad ,2) ; &lt;BR /&gt;
 output;&lt;BR /&gt;
 run;[/pre]</description>
      <pubDate>Thu, 03 Feb 2011 17:50:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68736#M19686</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-02-03T17:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: creating multiple columns from rows</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68737#M19687</link>
      <description>Here's another option:&lt;BR /&gt;
data temp ;&lt;BR /&gt;
input ID $ val $ source 8.;&lt;BR /&gt;
datalines;&lt;BR /&gt;
V1 0 1&lt;BR /&gt;
V1 10 1&lt;BR /&gt;
V1 100 2&lt;BR /&gt;
V1 250 2&lt;BR /&gt;
V2 50 1&lt;BR /&gt;
V2 80 1&lt;BR /&gt;
V2 500 2&lt;BR /&gt;
V7 10 1&lt;BR /&gt;
V7 20 1&lt;BR /&gt;
V7 30 2&lt;BR /&gt;
V7 99 2&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=temp;&lt;BR /&gt;
by id source;&lt;BR /&gt;
run;&lt;BR /&gt;
Data temp2;&lt;BR /&gt;
retain Arr_good Arr_Bad;&lt;BR /&gt;
length arr_good $50. arr_bad $50.;&lt;BR /&gt;
set temp;&lt;BR /&gt;
by id source;&lt;BR /&gt;
&lt;BR /&gt;
if first.source and source=1 then Arr_good=val;&lt;BR /&gt;
else if source=1 then arr_good=cats(arr_good,',',val);&lt;BR /&gt;
&lt;BR /&gt;
if first.source and source=2 then Arr_bad=val;&lt;BR /&gt;
else if source=2 then Arr_bad=cats(arr_bad,',',val);&lt;BR /&gt;
&lt;BR /&gt;
if last.id then output;&lt;BR /&gt;
drop val source;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 03 Feb 2011 18:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68737#M19687</guid>
      <dc:creator>MaxW</dc:creator>
      <dc:date>2011-02-03T18:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: creating multiple columns from rows</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68738#M19688</link>
      <description>Thank you both for your replies. &lt;BR /&gt;
&lt;BR /&gt;
I ended up going with the way MaxW did it because it was closer to where I was at.&lt;BR /&gt;
&lt;BR /&gt;
Thanks again</description>
      <pubDate>Thu, 03 Feb 2011 18:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68738#M19688</guid>
      <dc:creator>jerry898969</dc:creator>
      <dc:date>2011-02-03T18:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: creating multiple columns from rows</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68739#M19689</link>
      <description>hello,&lt;BR /&gt;
&lt;BR /&gt;
&lt;P&gt;you can also use arrays and the catx function for inserting the comma separator:&lt;/P&gt;&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp ;&lt;BR /&gt;
input ID $ val source;&lt;BR /&gt;
datalines;&lt;BR /&gt;
V1 0 1&lt;BR /&gt;
V1 10 1&lt;BR /&gt;
V1 100 2&lt;BR /&gt;
V1 250 2&lt;BR /&gt;
V2 50 1&lt;BR /&gt;
V2 80 1&lt;BR /&gt;
V2 500 2&lt;BR /&gt;
V7 10 1&lt;BR /&gt;
V7 20 1&lt;BR /&gt;
V7 30 2&lt;BR /&gt;
V7 99 2&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=temp;&lt;BR /&gt;
by id ;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data temp2 ;&lt;BR /&gt;
set temp ;&lt;BR /&gt;
by id ;&lt;BR /&gt;
&lt;BR /&gt;
array build{2} $ 50 arr_good arr_bad;&lt;BR /&gt;
retain build('');&lt;BR /&gt;
&lt;BR /&gt;
if first.id then call missing(arr_good,arr_bad);&lt;BR /&gt;
&lt;BR /&gt;
build{source}=catx(',',build{source},val);&lt;BR /&gt;
&lt;BR /&gt;
if last.id then output;&lt;BR /&gt;
&lt;BR /&gt;
drop val source;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;P&gt;Marius&lt;/P&gt;</description>
      <pubDate>Fri, 04 Feb 2011 09:52:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68739#M19689</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-02-04T09:52:17Z</dc:date>
    </item>
    <item>
      <title>Re: creating multiple columns from rows</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68740#M19690</link>
      <description>Marius&lt;BR /&gt;
that's very good! Using catx() eliminates the leading delimiter caused by cat(), so eliminates the need to test for first. &lt;BR /&gt;
Applying that and the array approach greatly simplifies my DoW [pre]data temp2 ;&lt;BR /&gt;
    array arr_d(2) $50 arr_good arr_bad ;&lt;BR /&gt;
    do until( last.id ); * sometimes referred to as a DoW loop ;&lt;BR /&gt;
       set temp ;&lt;BR /&gt;
       by id ;&lt;BR /&gt;
       arr_d(source)=catx( ',', arr_d(source), val ) ;&lt;BR /&gt;
    end ;&lt;BR /&gt;
run;[/pre]no need to clear the arr_bad and good because no need to retain them, nor is there any need for explicit output because each data step iteration deals with just one ID value.</description>
      <pubDate>Fri, 04 Feb 2011 11:40:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/creating-multiple-columns-from-rows/m-p/68740#M19690</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2011-02-04T11:40:23Z</dc:date>
    </item>
  </channel>
</rss>

