<?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: Collapse multiple rows into one row based on a category in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574899#M162515</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;Thank you very much for the solution!&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;&amp;nbsp;Thanks! The solution that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;provided works for the example that I provided. Your solution works perfectly as well (especially for&amp;nbsp;large-size data).&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122002"&gt;@VDD&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;Thanks for your inputs.&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jul 2019 10:09:18 GMT</pubDate>
    <dc:creator>Alireza_Boloori</dc:creator>
    <dc:date>2019-07-19T10:09:18Z</dc:date>
    <item>
      <title>Collapse multiple rows into one row based on a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574819#M162488</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to collapse all rows (for each unique ID) into one row. Here is what I have:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id x1 x2 x3 x4 x5;
datalines;
1 347 418 213 . .
1 213 . . . .
1 057 912 213 329 .
2 521 543 333 . .
2 333 521 555 . .
3 817 . . . .
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And here is what I want:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id y1 y2 y3 y4 y5 y6;
datalines;
1 347 418 213 057 912 329
2 521 543 333 555 817 .
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The only other thing that I should mention is that I want to represent duplicated values for each ID only once (e.g., 213 for ID=1 and 333 and 521 for ID=2). Any help or idea is appreciated!&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 22:56:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574819#M162488</guid>
      <dc:creator>Alireza_Boloori</dc:creator>
      <dc:date>2019-07-18T22:56:38Z</dc:date>
    </item>
    <item>
      <title>Re: Collapse multiple rows into one row based on a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574828#M162493</link>
      <description>&lt;P&gt;Let's start with the code you tried so far...&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 00:15:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574828#M162493</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-19T00:15:41Z</dc:date>
    </item>
    <item>
      <title>Re: Collapse multiple rows into one row based on a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574829#M162494</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;&amp;nbsp;If I only had one variable (x1), proc transpose would be a viable option, but since there are multiple variables, proc transpose would not return what I want.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 00:22:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574829#M162494</guid>
      <dc:creator>Alireza_Boloori</dc:creator>
      <dc:date>2019-07-19T00:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: Collapse multiple rows into one row based on a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574859#M162502</link>
      <description>&lt;P&gt;You can do:&lt;/P&gt;
&lt;P&gt;1) proc transpose from wide to short, so you have one vale in each row&lt;/P&gt;
&lt;P&gt;2) sort by ID &amp;amp; value with optioon NODUPKEY&lt;/P&gt;
&lt;P&gt;3) proc transpose from short to wide&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 03:46:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574859#M162502</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-07-19T03:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: Collapse multiple rows into one row based on a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574861#M162503</link>
      <description>&lt;P&gt;OK, so you haven't tried anything. Maybe you should start with simpler problems.&lt;/P&gt;
&lt;P&gt;Here is a data step solution to this problem that involves arrays, the do until pattern and the whichn function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array y{99};
j = 0;
do until(last.id);
    set have; by id;
    array xx x:;
    do i = 1 to dim(xx);
        if missing(xx{i}) then leave;
        else do;
            if whichn(xx{i}, of y{*}) = 0 then do;
                j = j + 1;
                y{j} = xx{i};
                end;
            end;
        end;
    end;
output;
drop i j x:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;with the hope that you will not use it before you understand how it works &lt;img id="smileywink" class="emoticon emoticon-smileywink" src="https://communities.sas.com/i/smilies/16x16_smiley-wink.png" alt="Smiley Wink" title="Smiley Wink" /&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 03:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574861#M162503</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-07-19T03:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Collapse multiple rows into one row based on a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574863#M162505</link>
      <description>&lt;P&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/how-to-reshape-data-long-to-wide-using-proc-transpose/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 03:56:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574863#M162505</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-07-19T03:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Collapse multiple rows into one row based on a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574871#M162509</link>
      <description>&lt;P&gt;I am complementing to&amp;nbsp; PGStats solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The array (YY)&amp;nbsp; that collects the distinct Xs is sized to 99 as the exact size is unknown and that you may not have more than this number. The larger size may crowd the Program Data Vector. I am showing the use of _temporary_ array which can be sized even to millions. I just used 1000 as the size. This is done by scanning the HAVE to count Distinct Values within ID and getting the maximum of counts from several IDs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/** Maximum of the Distinct Values within ID **/
data _null_;
   do i = 1 by 1 until(last.id);
      set have end = eof;
      by id notsorted;
      array yy[1000] _temporary_;
      array x x:;
      if first.id then do; j = 1; call missing(of yy[*]); end;
      do i = 1 to dim(x);
         if x[i] then do;
            v = whichn(x[i], of yy[*]);
            if v = 0 then do; yy[j] = x[i]; j+1; end;
         end;
      end;
   end;
   retain max;
   max = max(max, n(of yy[*]));
   if eof then call symputx('max', max);
run;&lt;/PRE&gt;
&lt;P&gt;Then we can use &amp;amp;max to size the array as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   do until(last.id);
      set have;
      by id notsorted;
      array x x:;
      array y y1 - y&amp;amp;max;
      if first.id then do; j = 1; call missing(of y[*]); end;
      retain y;
      do i = 1 to dim(x);
         if x[i] then do;
            v = whichN(x[i], of y[*]);
            if v = 0 then do; y[j] = x[i]; j+1; end;
         end;
      end;
   end;
keep id y:;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Jul 2019 06:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574871#M162509</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-19T06:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: Collapse multiple rows into one row based on a category</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574899#M162515</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;Thank you very much for the solution!&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;&amp;nbsp;Thanks! The solution that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;provided works for the example that I provided. Your solution works perfectly as well (especially for&amp;nbsp;large-size data).&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122002"&gt;@VDD&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel&lt;/a&gt;&amp;nbsp;Thanks for your inputs.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jul 2019 10:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Collapse-multiple-rows-into-one-row-based-on-a-category/m-p/574899#M162515</guid>
      <dc:creator>Alireza_Boloori</dc:creator>
      <dc:date>2019-07-19T10:09:18Z</dc:date>
    </item>
  </channel>
</rss>

