<?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: Transposing column data to create duplicate rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768708#M243851</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/396833"&gt;@JisangYu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;LI-SPOILER&gt;&amp;nbsp;&lt;/LI-SPOILER&gt;
&lt;P&gt;I'm a novice programmer assisting research and trying to perform data manipulation using SAS. My data looks as follows:&lt;BR /&gt;KIS&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stock&amp;nbsp; &amp;nbsp;Name Sum2000 Sum2001 Sum2002 Sum2003&lt;BR /&gt;036460&amp;nbsp; 036460&amp;nbsp; &amp;nbsp;AA&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;001471&amp;nbsp; 033780&amp;nbsp; &amp;nbsp;BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;004372&amp;nbsp; 044820&amp;nbsp; &amp;nbsp;CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to manipulate the data to look like as follows:&lt;BR /&gt;KIS&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stock&amp;nbsp; &amp;nbsp; Name&amp;nbsp; Sum&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 11&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;/P&gt;
&lt;P&gt;In short words, I want the company AA's sum for years 2000-2003 as first 4 rows, then company BB's sum for years 2000-2003 as next 4 rows, etc.&lt;BR /&gt;Since there are thousands of firms, I would really love to get some sample code to start this data manipulation.&lt;BR /&gt;I'd highly appreciate any kind of help!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Are you sure you don't want to keep the Year information?&lt;/P&gt;
&lt;P&gt;This does what you show in the example:&lt;/P&gt;
&lt;PRE&gt;data have;
   input KIS $ Stock $   Name $ Sum2000 Sum2001 Sum2002 Sum2003;
datalines;
036460 036460 AA 1 2 3 4
001471 033780 BB 5 6 7 8
004372 044820 CC 9 10 11 12
;

data want;
   set have;
   array s (*) sum2000 - sum2003;
   do i=1 to dim(s);
      sum=s[i];
      output;
   end;
   drop i sum2000 - sum2003;
run;&lt;/PRE&gt;
&lt;P&gt;The first data set is just to duplicate your example data, the second creates the desired output.&lt;/P&gt;
&lt;P&gt;The Array with a Do loop is the typical way to do the same thing to multiple variables. The Output in the loop writes the record once for each value of the array. Drop removes variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you wanted to keep the year information (strongly recommend)&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array s (2000:2003) sum2000 - sum2003;
   do year=2000 to 2003;
      sum=s[year];
      output;
   end;
   drop sum2000 - sum2003;
run;&lt;/PRE&gt;
&lt;P&gt;The difference here is the array definition sets up use of the year values 2000, 2001, 2002 and 2003 as the index value to reference the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest not using SUM as a variable name. SAS will allow that but Sum is a key word for statistics in multiple procedures and can lead to hard to follow code.&lt;/P&gt;</description>
    <pubDate>Tue, 21 Sep 2021 06:28:17 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-09-21T06:28:17Z</dc:date>
    <item>
      <title>Transposing column data to create duplicate rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768706#M243850</link>
      <description>&lt;LI-SPOILER&gt;&amp;nbsp;&lt;/LI-SPOILER&gt;&lt;P&gt;I'm a novice programmer assisting research and trying to perform data manipulation using SAS. My data looks as follows:&lt;BR /&gt;KIS&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stock&amp;nbsp; &amp;nbsp;Name Sum2000 Sum2001 Sum2002 Sum2003&lt;BR /&gt;036460&amp;nbsp; 036460&amp;nbsp; &amp;nbsp;AA&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;001471&amp;nbsp; 033780&amp;nbsp; &amp;nbsp;BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;004372&amp;nbsp; 044820&amp;nbsp; &amp;nbsp;CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to manipulate the data to look like as follows:&lt;BR /&gt;KIS&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stock&amp;nbsp; &amp;nbsp; Name&amp;nbsp; Sum&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 11&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;/P&gt;&lt;P&gt;In short words, I want the company AA's sum for years 2000-2003 as first 4 rows, then company BB's sum for years 2000-2003 as next 4 rows, etc.&lt;BR /&gt;Since there are thousands of firms, I would really love to get some sample code to start this data manipulation.&lt;BR /&gt;I'd highly appreciate any kind of help!&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 05:35:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768706#M243850</guid>
      <dc:creator>JisangYu</dc:creator>
      <dc:date>2021-09-21T05:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing column data to create duplicate rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768708#M243851</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/396833"&gt;@JisangYu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;LI-SPOILER&gt;&amp;nbsp;&lt;/LI-SPOILER&gt;
&lt;P&gt;I'm a novice programmer assisting research and trying to perform data manipulation using SAS. My data looks as follows:&lt;BR /&gt;KIS&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stock&amp;nbsp; &amp;nbsp;Name Sum2000 Sum2001 Sum2002 Sum2003&lt;BR /&gt;036460&amp;nbsp; 036460&amp;nbsp; &amp;nbsp;AA&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;001471&amp;nbsp; 033780&amp;nbsp; &amp;nbsp;BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;004372&amp;nbsp; 044820&amp;nbsp; &amp;nbsp;CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to manipulate the data to look like as follows:&lt;BR /&gt;KIS&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stock&amp;nbsp; &amp;nbsp; Name&amp;nbsp; Sum&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;BR /&gt;001402 036460 AA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;BR /&gt;001471 033780 BB&amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 11&lt;BR /&gt;004372 044820 CC&amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;/P&gt;
&lt;P&gt;In short words, I want the company AA's sum for years 2000-2003 as first 4 rows, then company BB's sum for years 2000-2003 as next 4 rows, etc.&lt;BR /&gt;Since there are thousands of firms, I would really love to get some sample code to start this data manipulation.&lt;BR /&gt;I'd highly appreciate any kind of help!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Are you sure you don't want to keep the Year information?&lt;/P&gt;
&lt;P&gt;This does what you show in the example:&lt;/P&gt;
&lt;PRE&gt;data have;
   input KIS $ Stock $   Name $ Sum2000 Sum2001 Sum2002 Sum2003;
datalines;
036460 036460 AA 1 2 3 4
001471 033780 BB 5 6 7 8
004372 044820 CC 9 10 11 12
;

data want;
   set have;
   array s (*) sum2000 - sum2003;
   do i=1 to dim(s);
      sum=s[i];
      output;
   end;
   drop i sum2000 - sum2003;
run;&lt;/PRE&gt;
&lt;P&gt;The first data set is just to duplicate your example data, the second creates the desired output.&lt;/P&gt;
&lt;P&gt;The Array with a Do loop is the typical way to do the same thing to multiple variables. The Output in the loop writes the record once for each value of the array. Drop removes variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you wanted to keep the year information (strongly recommend)&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array s (2000:2003) sum2000 - sum2003;
   do year=2000 to 2003;
      sum=s[year];
      output;
   end;
   drop sum2000 - sum2003;
run;&lt;/PRE&gt;
&lt;P&gt;The difference here is the array definition sets up use of the year values 2000, 2001, 2002 and 2003 as the index value to reference the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would suggest not using SUM as a variable name. SAS will allow that but Sum is a key word for statistics in multiple procedures and can lead to hard to follow code.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 06:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768708#M243851</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-09-21T06:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing column data to create duplicate rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768930#M243918</link>
      <description>&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;P&gt;I will certainly take your advice on not using Sum as variable name.&lt;/P&gt;&lt;P&gt;I highly appreciate your kind reply.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 19:37:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768930#M243918</guid>
      <dc:creator>JisangYu</dc:creator>
      <dc:date>2021-09-21T19:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing column data to create duplicate rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768933#M243920</link>
      <description>&lt;P&gt;Can I just ask you one thing:&lt;/P&gt;&lt;P&gt;What is the role of (*) in the code "array s (*) sum2000-sum2003" and the role of "$" in the code "input KIS $ Stock $ Name"?&lt;/P&gt;&lt;P&gt;Thanks for your help again!&lt;/P&gt;</description>
      <pubDate>Tue, 21 Sep 2021 19:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transposing-column-data-to-create-duplicate-rows/m-p/768933#M243920</guid>
      <dc:creator>JisangYu</dc:creator>
      <dc:date>2021-09-21T19:50:15Z</dc:date>
    </item>
  </channel>
</rss>

