<?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: How to merge multiple rows into a single row with same date but different variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784792#M250440</link>
    <description>Thank you!</description>
    <pubDate>Tue, 07 Dec 2021 23:50:13 GMT</pubDate>
    <dc:creator>annaleticia</dc:creator>
    <dc:date>2021-12-07T23:50:13Z</dc:date>
    <item>
      <title>How to merge multiple rows into a single row with same date but different variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784782#M250434</link>
      <description>&lt;P&gt;Data Set&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Variable&lt;/P&gt;&lt;P&gt;111&amp;nbsp; &amp;nbsp; 12/7/2021&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;111&amp;nbsp; &amp;nbsp;&amp;nbsp;12/7/2021&amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;111&amp;nbsp; &amp;nbsp;&amp;nbsp;12/7/2021&amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;&lt;P&gt;222&amp;nbsp; &amp;nbsp; 12/6/2021&amp;nbsp; &amp;nbsp; A&lt;/P&gt;&lt;P&gt;222&amp;nbsp; &amp;nbsp; &amp;nbsp;12/7/2021&amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected Result&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Variable&lt;/P&gt;&lt;P&gt;111 12/7/2021&amp;nbsp; A, B, C&lt;/P&gt;&lt;P&gt;222&amp;nbsp; 12/6/2021 A&lt;/P&gt;&lt;P&gt;222&amp;nbsp; 12/7/2021 A&lt;/P&gt;</description>
      <pubDate>Tue, 07 Dec 2021 23:12:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784782#M250434</guid>
      <dc:creator>annaleticia</dc:creator>
      <dc:date>2021-12-07T23:12:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge multiple rows into a single row with same date but different variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784788#M250436</link>
      <description>&lt;P&gt;Typically two ways - one is a data step and combine as you go down the rows and output on the last group record. This utilizes RETAIN and BY group processing.&amp;nbsp;&lt;BR /&gt;The second method is to transpose the data into a wide format using PROC TRANSPOSE and then use CATX() to combine the data.&lt;/P&gt;
&lt;P&gt;Both are illustrated below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*create sample data for demonstration;
data have;
    infile cards dlm='09'x;
    input OrgID Product $   States $;
    cards;
1   football    DC
1   football    VA
1   football    MD
2   football    CA
3   football    NV
3   football    CA
;
run;

*Sort - required for both options;
proc sort data=have;
    by orgID;
run;

**********************************************************************;
*Use RETAIN and BY group processing to combine the information;
**********************************************************************;
data want_option1;
    set have;
    by orgID;
    length combined $100.;
    retain combined;

    if first.orgID then
        combined=states;
    else
        combined=catx(', ', combined, states);

    if last.orgID then
        output;
run;

**********************************************************************;
*Transpose it to a wide format and then combine into a single field;
**********************************************************************;
proc transpose data=have out=wide prefix=state_;
    by orgID;
    var states;
run;

data want_option2;
    set wide;
    length combined $100.;
    combined=catx(', ', of state_:);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a" target="_blank" rel="noopener"&gt;https://gist.github.com/statgeek/d583cfa992bf56da51d435165b07e96a&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/386445"&gt;@annaleticia&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Data Set&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Variable&lt;/P&gt;
&lt;P&gt;111&amp;nbsp; &amp;nbsp; 12/7/2021&amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;
&lt;P&gt;111&amp;nbsp; &amp;nbsp;&amp;nbsp;12/7/2021&amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;
&lt;P&gt;111&amp;nbsp; &amp;nbsp;&amp;nbsp;12/7/2021&amp;nbsp; &amp;nbsp; &amp;nbsp;C&lt;/P&gt;
&lt;P&gt;222&amp;nbsp; &amp;nbsp; 12/6/2021&amp;nbsp; &amp;nbsp; A&lt;/P&gt;
&lt;P&gt;222&amp;nbsp; &amp;nbsp; &amp;nbsp;12/7/2021&amp;nbsp; &amp;nbsp;A&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Expected Result&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Variable&lt;/P&gt;
&lt;P&gt;111 12/7/2021&amp;nbsp; A, B, C&lt;/P&gt;
&lt;P&gt;222&amp;nbsp; 12/6/2021 A&lt;/P&gt;
&lt;P&gt;222&amp;nbsp; 12/7/2021 A&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Dec 2021 23:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784788#M250436</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-07T23:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge multiple rows into a single row with same date but different variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784790#M250438</link>
      <description>&lt;P&gt;What do you expect to do with that data set that you can't do with the original?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Dec 2021 23:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784790#M250438</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-12-07T23:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge multiple rows into a single row with same date but different variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784792#M250440</link>
      <description>Thank you!</description>
      <pubDate>Tue, 07 Dec 2021 23:50:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-multiple-rows-into-a-single-row-with-same-date-but/m-p/784792#M250440</guid>
      <dc:creator>annaleticia</dc:creator>
      <dc:date>2021-12-07T23:50:13Z</dc:date>
    </item>
  </channel>
</rss>

