<?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: Combine Rows into New column and Keep Original Layout in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/791610#M253597</link>
    <description>&lt;P&gt;Create the ID combined in a separate table &amp;nbsp;and merge it back with the main table.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;A DoW loop is the only other way to do this but it’s also still technically two passes of the data so same amount of work as the merge and the merge is infinitely easier to understand.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 22 Jan 2022 00:43:02 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-01-22T00:43:02Z</dc:date>
    <item>
      <title>Combine Rows into New column and Keep Original Layout</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/791603#M253592</link>
      <description>&lt;P&gt;I know there is data steps/arrays that will combine multiple rows into one row, I want to do that and keep the original table layout, and for the total column keep max value. For example;&lt;/P&gt;
&lt;P&gt;Original Table:&lt;/P&gt;
&lt;TABLE width="231"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="30"&gt;Key&lt;/TD&gt;
&lt;TD width="93"&gt;ID&lt;/TD&gt;
&lt;TD width="45"&gt;NAME&lt;/TD&gt;
&lt;TD width="63"&gt;TOTAL&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;262P00050013&lt;/TD&gt;
&lt;TD&gt;Bob&lt;/TD&gt;
&lt;TD&gt;15012.32&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;262P00050014&lt;/TD&gt;
&lt;TD&gt;Joe&lt;/TD&gt;
&lt;TD&gt;24484.12&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;262P00050015&lt;/TD&gt;
&lt;TD&gt;Sally&lt;/TD&gt;
&lt;TD&gt;18988.43&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;New Table:&lt;/P&gt;
&lt;TABLE width="699"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="30"&gt;Key&lt;/TD&gt;
&lt;TD width="93"&gt;ID&lt;/TD&gt;
&lt;TD width="45"&gt;NAME&lt;/TD&gt;
&lt;TD width="63"&gt;TOTAL&lt;/TD&gt;
&lt;TD width="283"&gt;ID_COMBINED&lt;/TD&gt;
&lt;TD width="122"&gt;NAME_COMBINED&lt;/TD&gt;
&lt;TD width="63"&gt;Max&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;262P00050013&lt;/TD&gt;
&lt;TD&gt;Bob&lt;/TD&gt;
&lt;TD&gt;15012.32&lt;/TD&gt;
&lt;TD&gt;262P00050013, 262P00050014, 262P00050015&lt;/TD&gt;
&lt;TD&gt;Bob, Joe, Sally&lt;/TD&gt;
&lt;TD&gt;24484.12&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;262P00050014&lt;/TD&gt;
&lt;TD&gt;Joe&lt;/TD&gt;
&lt;TD&gt;24484.12&lt;/TD&gt;
&lt;TD&gt;262P00050013, 262P00050014, 262P00050015&lt;/TD&gt;
&lt;TD&gt;Bob, Joe, Sally&lt;/TD&gt;
&lt;TD&gt;24484.12&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;262P00050015&lt;/TD&gt;
&lt;TD&gt;Sally&lt;/TD&gt;
&lt;TD&gt;18988.43&lt;/TD&gt;
&lt;TD&gt;262P00050013, 262P00050014, 262P00050015&lt;/TD&gt;
&lt;TD&gt;Bob, Joe, Sally&lt;/TD&gt;
&lt;TD&gt;24484.12&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 22:48:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/791603#M253592</guid>
      <dc:creator>jimbobob</dc:creator>
      <dc:date>2022-01-21T22:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: Combine Rows into New column and Keep Original Layout</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/791610#M253597</link>
      <description>&lt;P&gt;Create the ID combined in a separate table &amp;nbsp;and merge it back with the main table.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;A DoW loop is the only other way to do this but it’s also still technically two passes of the data so same amount of work as the merge and the merge is infinitely easier to understand.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Jan 2022 00:43:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/791610#M253597</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-22T00:43:02Z</dc:date>
    </item>
    <item>
      <title>Re: Combine Rows into New column and Keep Original Layout</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/791620#M253603</link>
      <description>&lt;P&gt;Yes, you have to read all obs for each key twice, once to build the MAX and COMBINED variables, and again to re-read (and output) each obs with the new variables added:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Key	$1. ID :$12.	NAME :$6.	TOTAL ;
datalines;
A	262P00050013	Bob	15012.32
A	262P00050014	Joe	24484.12
A	262P00050015	Sally	18988.43
run;

data want;
  if 0 then set have;  /*Insures that original vars in HAVE will be the leftmost vars in WANT*/

  /* Now read all obs for a key create the new variables for it */
  length id_combined $80 name_combined $60 ;
  do until (last.key);
    set have;
    by key;
    id_combined=catx(',',id_combined,id);
    name_combined=catx(',',name_combined,name);
    max=max(total,max);
  end;

  /* With new vars in hand, re-read old vars for the key and output each obs */
  do until (last.key);
    set have;
    by key;
    output;
  end;
run;

    &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;said, you have to read each key twice (the data are sorted by key, right?).&amp;nbsp; &amp;nbsp;But this approach does not create and write out a separate table/dataset with the combined variables.&amp;nbsp; So if your dataset is large, you'll avoid writing out an intermediate dataset only to read it back in from disk for merging purposes.&amp;nbsp; &amp;nbsp;The complexity of the above does have its benefits.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will need to know the lengths needed for id_combined and name_combined.&amp;nbsp; For example, if the maximum number of observations possible for a key is (say) 10, then the length needed for ID_COMBINED would be 10*12 (each id is $12) plus 9*1&amp;nbsp; (9 commas) ==&amp;gt; $129 minimum length.&amp;nbsp; Same concept for name_combined.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Jan 2022 02:47:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/791620#M253603</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-01-22T02:47:33Z</dc:date>
    </item>
    <item>
      <title>Re: Combine Rows into New column and Keep Original Layout</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/792217#M253824</link>
      <description>&lt;P&gt;Thanks Mkeintz, this works great.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 16:13:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/792217#M253824</guid>
      <dc:creator>jimbobob</dc:creator>
      <dc:date>2022-01-25T16:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Combine Rows into New column and Keep Original Layout</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/792218#M253825</link>
      <description>&lt;P&gt;Thanks Reeza.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 16:14:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Combine-Rows-into-New-column-and-Keep-Original-Layout/m-p/792218#M253825</guid>
      <dc:creator>jimbobob</dc:creator>
      <dc:date>2022-01-25T16:14:33Z</dc:date>
    </item>
  </channel>
</rss>

