<?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: Help with Transposing Columns into rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599380#M173018</link>
    <description>&lt;P&gt;Let's assume have a SAS dataset already, call it HAVE, and you know the date of the first month. Let's assume you have it it a macro variable. Then just use a DO loop to convert.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let base_date='01SEP2019'd;
data want;
  set have;
  array _rx rx1-rx5 ;
  do index=1 to dim(_rx);
    date=intnx('month',&amp;amp;base_date,1-index,'b');
    rx=_rx[index];
    output;
  end;
  format date date9. ;
  drop index rx1-rx5;
run;
  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Oct 2019 15:48:16 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-10-25T15:48:16Z</dc:date>
    <item>
      <title>Help with Transposing Columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599356#M173005</link>
      <description>&lt;P&gt;I have the following Rx monthly data.&amp;nbsp; Rx1 being the latest month data.&amp;nbsp; In my case it Sept 2019 data.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile cards expandtabs truncover;&lt;BR /&gt;input id $2. rx1 rx2 rx3 rx4 rx5 &amp;amp; 2.;&lt;BR /&gt;cards;&lt;BR /&gt;1 5 6 8 10 4 &lt;BR /&gt;2 0 2 4 7 6&lt;BR /&gt;4 1 1 3 9 2&lt;BR /&gt;7 3 2 4 6 3&lt;BR /&gt;6 4 2 5 6 8&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the output to be as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp;Date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Rx&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;Sept-2019&amp;nbsp; &amp;nbsp;5&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;Aug-2019&amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;Jun-2019&amp;nbsp; &amp;nbsp;10&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;May-2019&amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;Sept-2019&amp;nbsp; &amp;nbsp;0&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;Aug-2019&amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;Jun-2019&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;May-2019&amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;Sept-2019&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;Aug-2019&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;Jun-2019&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;BR /&gt;4&amp;nbsp; &amp;nbsp; May-2019&amp;nbsp; &amp;nbsp;2&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; Sept-2019&amp;nbsp; &amp;nbsp;3&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; Aug-2019&amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; Jun-2019&amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; May-2019&amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; Sept-2019&amp;nbsp; &amp;nbsp;4&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; Aug-2019&amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; Jun-2019&amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; May-2019&amp;nbsp; &amp;nbsp; 8&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:06:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599356#M173005</guid>
      <dc:creator>pp2014</dc:creator>
      <dc:date>2019-10-25T15:06:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Transposing Columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599366#M173008</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11613"&gt;@pp2014&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have the following Rx monthly data.&amp;nbsp; Rx1 being the latest month data.&amp;nbsp; In my case it Sept 2019 data.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile cards expandtabs truncover;&lt;BR /&gt;input id $2. rx1 rx2 rx3 rx4 rx5 &amp;amp; 2.;&lt;BR /&gt;cards;&lt;BR /&gt;1 5 6 8 10 4 &lt;BR /&gt;2 0 2 4 7 6&lt;BR /&gt;4 1 1 3 9 2&lt;BR /&gt;7 3 2 4 6 3&lt;BR /&gt;6 4 2 5 6 8&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the output to be as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id&amp;nbsp; &amp;nbsp;Date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Rx&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;Sept-2019&amp;nbsp; &amp;nbsp;5&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;Aug-2019&amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;Jun-2019&amp;nbsp; &amp;nbsp;10&lt;BR /&gt;1&amp;nbsp; &amp;nbsp;May-2019&amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;Sept-2019&amp;nbsp; &amp;nbsp;0&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;Aug-2019&amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;Jun-2019&amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;BR /&gt;2&amp;nbsp; &amp;nbsp;May-2019&amp;nbsp; &amp;nbsp; 6&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;Sept-2019&amp;nbsp; &amp;nbsp;1&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;Aug-2019&amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;4&amp;nbsp; &amp;nbsp;Jun-2019&amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;BR /&gt;4&amp;nbsp; &amp;nbsp; May-2019&amp;nbsp; &amp;nbsp;2&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; Sept-2019&amp;nbsp; &amp;nbsp;3&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; Aug-2019&amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; Jun-2019&amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;BR /&gt;7&amp;nbsp; &amp;nbsp; May-2019&amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; Sept-2019&amp;nbsp; &amp;nbsp;4&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; Aug-2019&amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; Jul-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; Jun-2019&amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;BR /&gt;6&amp;nbsp; &amp;nbsp; May-2019&amp;nbsp; &amp;nbsp; 8&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So how do we get a month and year from the source data?&lt;/P&gt;
&lt;P&gt;One way to read the data to have multiple rows with the same ID and only X as the variable.&lt;/P&gt;
&lt;PRE&gt;data have;
   infile cards expandtabs truncover;
   input id $2. @ ;
    do i= 1 to 5 ;
      input x @;
      output;
   end;
   input;
   drop i;
cards;
1 5 6 8 10 4 
2 0 2 4 7 6
4 1 1 3 9 2
7 3 2 4 6 3
6 4 2 5 6 8
;&lt;/PRE&gt;
&lt;P&gt;This approach would create one row with a missing X for each expected line.&lt;/P&gt;
&lt;P&gt;If you don't want missing x values then change the OUTPUT to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if not missing(x) then output;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:20:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599366#M173008</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-25T15:20:30Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Transposing Columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599371#M173012</link>
      <description>&lt;P&gt;Regarding date, I will have dataset with current_date field in it which gets updated every month when data gets updated.&amp;nbsp; So current month&amp;nbsp; is Sept 2019.&amp;nbsp; I want to see Date field also while transposing..&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599371#M173012</guid>
      <dc:creator>pp2014</dc:creator>
      <dc:date>2019-10-25T15:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Transposing Columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599378#M173017</link>
      <description>&lt;P&gt;I'm sure there's a better way to do this, but this appears to work. I'm assuming your date field will be dynamic, so you will need to figure out how to do that, but here's a start:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input id $2. rx1 rx2 rx3 rx4 rx5 &amp;amp; 2.;
cards;
1 5 6 8 10 4
2 0 2 4 7 6
4 1 1 3 9 2
7 3 2 4 6 3
6 4 2 5 6 8
;

proc sort data=have;
by id;
run;

proc format;
value $rxf
	"rx1" = "Sept-2019"
	"rx2" = "Aug-2019"
	"rx3" = "Jul-2019"
	"rx4" = "Jun-2019"
	"rx5" = "May-2019"
;

proc transpose data=have
				out=want (rename=(col1=Rx))
			   name=date1;
by id;
var rx1 rx2 rx3 rx4 rx5;
run;

data want_format (drop=date1 rename=(date2=date1));
set want;
length date2 $9.;
date2 = put(date1, $rxf.);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I ran a proc compare and the results were the same.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:45:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599378#M173017</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2019-10-25T15:45:04Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Transposing Columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599380#M173018</link>
      <description>&lt;P&gt;Let's assume have a SAS dataset already, call it HAVE, and you know the date of the first month. Let's assume you have it it a macro variable. Then just use a DO loop to convert.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let base_date='01SEP2019'd;
data want;
  set have;
  array _rx rx1-rx5 ;
  do index=1 to dim(_rx);
    date=intnx('month',&amp;amp;base_date,1-index,'b');
    rx=_rx[index];
    output;
  end;
  format date date9. ;
  drop index rx1-rx5;
run;
  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 15:48:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599380#M173018</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-25T15:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Transposing Columns into rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599446#M173045</link>
      <description>&lt;P&gt;And a way to do while reading the data:&lt;/P&gt;
&lt;PRE&gt;%let base_date='01SEP2019'd;
data have;
   infile cards expandtabs truncover;
   input id $2. @ ;
   date=intnx('month',&amp;amp;base_date,0,'b');
    do i= 1 to 5 ;
      input x @;
      output;
      date=intnx('month',date,-1,'b');
   end;
   input;
   drop i;
   format date date9. ;
cards;
1 5 6 8 10 4 
2 0 2 4 7 6
4 1 1 3 9 2
7 3 2 4 6 3
6 4 2 5 6 8
;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Oct 2019 20:32:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Transposing-Columns-into-rows/m-p/599446#M173045</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-25T20:32:16Z</dc:date>
    </item>
  </channel>
</rss>

