<?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 collapse rows into columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386801#M92694</link>
    <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304" target="_blank"&gt;@draycut&lt;/A&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;. Yes I do agree that for programming the long version is much better. I needed to screen data by eyes before going further and I didn't want to do it clumsilily by excel, so I thought I might learn something this way, as I did from RW9s code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Wed, 09 Aug 2017 20:42:18 GMT</pubDate>
    <dc:creator>Shayan2012</dc:creator>
    <dc:date>2017-08-09T20:42:18Z</dc:date>
    <item>
      <title>How to collapse rows into columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386668#M92649</link>
      <description>&lt;P&gt;Assume that I have a dataset like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data execs;
    input id $ firmid $ year;
    datalines;                      
         A001 APL  1990
         A002 MSFT 1993
         A002 AMZN 1994
         A002 APL  1996
         A003 CSCO 1994
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This hypothetical table contains the first time a person has joined the firm. For example, Mr A002 has joined MSFT in 1993, and then joined AMZN in 1994 and then joined APL in 1996. Mr A003 only joined CSCO in 1994.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to collapse this data into person-level shape, so that each row is for a person, with multiple columns for every job experience that the person has had.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   input id $ firmid $ year $ firmid2 $ year2 $ firmid3 $ year3;
   datalines;                      
    A001 APL  1990 . . . .
    A002 MSFT 1993 AMZN 1994 APL 1996
    A003 CSCO 1994 . . . .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a nice way to do this procedure? I grappled with Proc Transpose but it does not seem to give me the desired output, or I cannot figure it out. Any helps?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2017 15:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386668#M92649</guid>
      <dc:creator>Shayan2012</dc:creator>
      <dc:date>2017-08-09T15:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse rows into columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386671#M92651</link>
      <description>&lt;P&gt;Why do you want your data like this? In my opinion your &lt;SPAN class="token number"&gt;&lt;STRONG&gt;&lt;FONT color="#008080"&gt;ex&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;ecs dataset looks way better than your want dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2017 15:12:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386671#M92651</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-08-09T15:12:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse rows into columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386672#M92652</link>
      <description>&lt;P&gt;First off I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;, from a programming point of view your first dataset is far simpler to work with.&lt;/P&gt;
&lt;P&gt;Now what you want to do is called transposing or reshaping data. &amp;nbsp;Your going from a normalised (data goes down, common in programming, and in databasing) to transposed data (common in Excel). &amp;nbsp;To do this there is multiple resources out there which can show you the proc transpose for simple data transposing, or by using arrays for more complex mapping. &amp;nbsp;For instance from this forum:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&amp;amp;allow_punctuation=false&amp;amp;q=reshaping" target="_blank"&gt;https://communities.sas.com/t5/forums/searchpage/tab/message?advanced=false&amp;amp;allow_punctuation=false&amp;amp;q=reshaping&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case&amp;nbsp;it looks like a more complicate transpose, so you would need arrays:&lt;/P&gt;
&lt;PRE&gt;/* assumes max 3 firms *.
data want;
  set execs;
  by id;
  array firm_id{3} $20;
  array year_{3} $20;
  retain firm_id: year_:;
  if first.id then do;
    ind=1;
    call missing(of firm_id:,of year_:);
  end;
  firm_id{ind}=firmid;
  year_{ind}=year;
  ind=ind+1;
  if last.id then output;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2017 15:18:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386672#M92652</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-08-09T15:18:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse rows into columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386676#M92653</link>
      <description>&lt;P&gt;I have a macro that simplifies the task of making a wide file wider. You can download it at:&amp;nbsp;&lt;A href="http://www.sascommunity.org/wiki/A_Better_Way_to_Flip_(Transpose)_a_SAS_Dataset" target="_blank"&gt;http://www.sascommunity.org/wiki/A_Better_Way_to_Flip_(Transpose)_a_SAS_Dataset&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your current problem you would only have to call the macro with the following code:&lt;/P&gt;
&lt;PRE&gt;%transpose(data=execs, out=want, by=id, var=firmid year)&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2017 15:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386676#M92653</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-08-09T15:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse rows into columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386799#M92693</link>
      <description>Wow! This Macro is awesome. Thanks a lot for sharing this Arthur!</description>
      <pubDate>Wed, 09 Aug 2017 20:40:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386799#M92693</guid>
      <dc:creator>Shayan2012</dc:creator>
      <dc:date>2017-08-09T20:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse rows into columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386801#M92694</link>
      <description>&lt;P&gt;Thanks a lot&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304" target="_blank"&gt;@draycut&lt;/A&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;. Yes I do agree that for programming the long version is much better. I needed to screen data by eyes before going further and I didn't want to do it clumsilily by excel, so I thought I might learn something this way, as I did from RW9s code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Aug 2017 20:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/386801#M92694</guid>
      <dc:creator>Shayan2012</dc:creator>
      <dc:date>2017-08-09T20:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse rows into columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/387008#M92743</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data execs;
    input id $ firmid $ year;
    datalines;                      
         A001 APL  1990
         A002 MSFT 1993
         A002 AMZN 1994
         A002 APL  1996
         A003 CSCO 1994
;
run;
proc sql noprint;
select max(n) into : n
 from (select count(*) as n from execs group by id);
 quit;
proc summary data=execs;
by id;
output out=want idgroup(out[&amp;amp;n] (firmid year)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or use MERGE skill.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Aug 2017 14:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-rows-into-columns/m-p/387008#M92743</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-08-10T14:05:04Z</dc:date>
    </item>
  </channel>
</rss>

