<?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 Transpose more than 3 variables without use proc transpose in Advanced Programming</title>
    <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/776981#M141</link>
    <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please could you assist me regarding the fictious program below (there is only one variable to transpose). It's not work. I would to have one row by id (patient) without use proc TRANSPOSE. Because, I would like transpose more than 3 variables by patient.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data A;&lt;BR /&gt;length id 8. ;&lt;BR /&gt;input id Date_exam;&lt;BR /&gt;format Date_exam ddmmyy10.;&lt;BR /&gt;informat Date_exam ddmmyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;0001 01/10/2020&lt;BR /&gt;0002 11/10/2001&lt;BR /&gt;0003 16/09/2010&lt;BR /&gt;0004 30/01/1960&lt;BR /&gt;0001 02/10/2020&lt;BR /&gt;0002 15/10/2001&lt;BR /&gt;0002 15/10/2002&lt;BR /&gt;0003 16/09/2010&lt;BR /&gt;0004 30/01/1960&lt;BR /&gt;0003 16/09/2004&lt;BR /&gt;0003 16/09/2018&lt;BR /&gt;0005 06/09/2017&lt;BR /&gt;;&lt;BR /&gt;run ;&lt;BR /&gt;&lt;BR /&gt;proc sort data=A;by id Date_exam; run ;&lt;/P&gt;&lt;P&gt;%let i=1;&lt;BR /&gt;data B; set A;&lt;BR /&gt;by id;&lt;BR /&gt;retain id Date&amp;amp;i;&lt;BR /&gt;array date[1:4];&lt;BR /&gt;do i=1 to 4;&lt;BR /&gt;Date&amp;amp;i=Date_exam;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't have one row by patient for the variable&amp;nbsp;Date_exam. Please, not use Proc TRANSPOSE. I know this row, it's ok for me but not efficience when you have more yhan 3 variables.&lt;/P&gt;&lt;P&gt;Thank you so much in advance.&lt;/P&gt;&lt;P&gt;Corinne&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Oct 2021 14:37:20 GMT</pubDate>
    <dc:creator>CorinneT</dc:creator>
    <dc:date>2021-10-28T14:37:20Z</dc:date>
    <item>
      <title>Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/776981#M141</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please could you assist me regarding the fictious program below (there is only one variable to transpose). It's not work. I would to have one row by id (patient) without use proc TRANSPOSE. Because, I would like transpose more than 3 variables by patient.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data A;&lt;BR /&gt;length id 8. ;&lt;BR /&gt;input id Date_exam;&lt;BR /&gt;format Date_exam ddmmyy10.;&lt;BR /&gt;informat Date_exam ddmmyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;0001 01/10/2020&lt;BR /&gt;0002 11/10/2001&lt;BR /&gt;0003 16/09/2010&lt;BR /&gt;0004 30/01/1960&lt;BR /&gt;0001 02/10/2020&lt;BR /&gt;0002 15/10/2001&lt;BR /&gt;0002 15/10/2002&lt;BR /&gt;0003 16/09/2010&lt;BR /&gt;0004 30/01/1960&lt;BR /&gt;0003 16/09/2004&lt;BR /&gt;0003 16/09/2018&lt;BR /&gt;0005 06/09/2017&lt;BR /&gt;;&lt;BR /&gt;run ;&lt;BR /&gt;&lt;BR /&gt;proc sort data=A;by id Date_exam; run ;&lt;/P&gt;&lt;P&gt;%let i=1;&lt;BR /&gt;data B; set A;&lt;BR /&gt;by id;&lt;BR /&gt;retain id Date&amp;amp;i;&lt;BR /&gt;array date[1:4];&lt;BR /&gt;do i=1 to 4;&lt;BR /&gt;Date&amp;amp;i=Date_exam;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't have one row by patient for the variable&amp;nbsp;Date_exam. Please, not use Proc TRANSPOSE. I know this row, it's ok for me but not efficience when you have more yhan 3 variables.&lt;/P&gt;&lt;P&gt;Thank you so much in advance.&lt;/P&gt;&lt;P&gt;Corinne&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Oct 2021 14:37:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/776981#M141</guid>
      <dc:creator>CorinneT</dc:creator>
      <dc:date>2021-10-28T14:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777016#M142</link>
      <description>&lt;P&gt;You're right it's tedious but not as much work as you think since you need to declare two more arrays and assignments/clearing.&lt;/P&gt;
&lt;P&gt;You don't need the macro variables though, that's what the array indices are for.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's some transposing data tutorials, not sure why you don't want to use PROC TRANSPOSE since it's more efficient in the long run. Another thing to consider - look back at how this data was created and see if there's a way to change it.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Long to Wide:&lt;BR /&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;A href="https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;And sometimes a double transpose is needed for extra wide data sets:&lt;BR /&gt;&lt;A href="https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd" target="_blank"&gt;https://gist.github.com/statgeek/2321b6f62ab78d5bf2b0a5a8626bd7cd&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=A;
	by id Date_exam;
run;

data B;
	set A;
	by id;
	retain id Date1-Date4;
	format date1-date4 date9.;
	array date[1:4];

	if first.id then
		do;
			count=0;
			call missing(of date(*));
		end;
	count+1;
	Date[count]=Date_exam;

	if last.id then
		output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/377925"&gt;@CorinneT&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please could you assist me regarding the fictious program below (there is only one variable to transpose). It's not work. I would to have one row by id (patient) without use proc TRANSPOSE. Because, I would like transpose more than 3 variables by patient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data A;&lt;BR /&gt;length id 8. ;&lt;BR /&gt;input id Date_exam;&lt;BR /&gt;format Date_exam ddmmyy10.;&lt;BR /&gt;informat Date_exam ddmmyy10.;&lt;BR /&gt;datalines;&lt;BR /&gt;0001 01/10/2020&lt;BR /&gt;0002 11/10/2001&lt;BR /&gt;0003 16/09/2010&lt;BR /&gt;0004 30/01/1960&lt;BR /&gt;0001 02/10/2020&lt;BR /&gt;0002 15/10/2001&lt;BR /&gt;0002 15/10/2002&lt;BR /&gt;0003 16/09/2010&lt;BR /&gt;0004 30/01/1960&lt;BR /&gt;0003 16/09/2004&lt;BR /&gt;0003 16/09/2018&lt;BR /&gt;0005 06/09/2017&lt;BR /&gt;;&lt;BR /&gt;run ;&lt;BR /&gt;&lt;BR /&gt;proc sort data=A;by id Date_exam; run ;&lt;/P&gt;
&lt;P&gt;%let i=1;&lt;BR /&gt;data B; set A;&lt;BR /&gt;by id;&lt;BR /&gt;retain id Date&amp;amp;i;&lt;BR /&gt;array date[1:4];&lt;BR /&gt;do i=1 to 4;&lt;BR /&gt;Date&amp;amp;i=Date_exam;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't have one row by patient for the variable&amp;nbsp;Date_exam. Please, not use Proc TRANSPOSE. I know this row, it's ok for me but not efficience when you have more yhan 3 variables.&lt;/P&gt;
&lt;P&gt;Thank you so much in advance.&lt;/P&gt;
&lt;P&gt;Corinne&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Oct 2021 15:45:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777016#M142</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-28T15:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777019#M143</link>
      <description>Note that in this method you need to know ahead of time, what the max number of dates/variable2/variable3 that you need to account for. With the PROC TRANSPOSE approach you do not need to know this ahead of time. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 28 Oct 2021 15:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777019#M143</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-28T15:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777038#M144</link>
      <description>Thanks, this program runs. But it's quit complicated. I will follow your tip regarding the Proc TRANSPOSE. But my concern with Proc Transpose when you have to transpose more than 3 variables in one row for each observation (patient Id) it isn't easy with Proc TRANSPOSE.</description>
      <pubDate>Thu, 28 Oct 2021 16:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777038#M144</guid>
      <dc:creator>CorinneT</dc:creator>
      <dc:date>2021-10-28T16:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777040#M145</link>
      <description>In PROC TRANSPOSE you do it once for each variable and then merge the results. But it's fully dynamic and you don't need to worry if the number of dates changes. If you have 6 maximum dates next time you need to update your code or design it so that the data step does that ahead of time, which adds another layer of complexity to this code. &lt;BR /&gt;&lt;BR /&gt;In the data step, you have to handle all of the retaining manually so it is more complex. &lt;BR /&gt;&lt;BR /&gt;The steps are:&lt;BR /&gt;1. Sort to ensure correct order&lt;BR /&gt;2. Declare an array with a maximum dimensions required so you can use index references - need this information before the step &lt;BR /&gt;3. At the start of each ID, initialize all your dates to missing and set your counter to 0&lt;BR /&gt;4. Increment counter at each date&lt;BR /&gt;5. For each date, assign it to the correct array variable - use RETAIN to hold values across rows.&lt;BR /&gt;6. If last record then output to data set&lt;BR /&gt;7. Format dates so they appear as SAS dates&lt;BR /&gt;8. Repeat steps 2 &amp;amp; 5 for each variable that requires transposing&lt;BR /&gt;&lt;BR /&gt;Transpose solution:&lt;BR /&gt;1. Sort&lt;BR /&gt;2. Transpose Var1&lt;BR /&gt;3. Transpose Var2&lt;BR /&gt;4. Transpose Var3&lt;BR /&gt;5. Merge results&lt;BR /&gt;6. Format data as required&lt;BR /&gt;&lt;BR /&gt;Which one is less error prone and easier to code?&lt;BR /&gt;</description>
      <pubDate>Thu, 28 Oct 2021 16:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777040#M145</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-28T16:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777093#M146</link>
      <description>Thanks so much regarding the steps of ARRAY solution. It's more clear.&lt;BR /&gt;Yes, the Proc TRANSPOSE prone less error. What's I'm not like with Proc TRANSPOSE is that you have to transpose the variables one after the other. Dans after mrege each tables.&lt;BR /&gt;however, you are right Proc TRANSPOSE seems to be method least error prone to code.</description>
      <pubDate>Thu, 28 Oct 2021 18:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777093#M146</guid>
      <dc:creator>CorinneT</dc:creator>
      <dc:date>2021-10-28T18:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777165#M147</link>
      <description>&lt;P&gt;Just use PROC SUMMARY.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input id Date_exam;
  informat Date_exam ddmmyy.;
  format Date_exam ddmmyy10.;
  days_ago = today()-Date_exam;
datalines;
0001 01/10/2020
0002 11/10/2001
0003 16/09/2010
0004 30/01/1960
0001 02/10/2020
0002 15/10/2001
0002 15/10/2002
0003 16/09/2010
0004 30/01/1960
0003 16/09/2004
0003 16/09/2018
0005 06/09/2017
;

proc summary data=have nway ;
  class id ;
  output out=want idgroup(out[5] (Date_exam days_ago)= ) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;                             Date_exam_   Date_exam_   Date_exam_   Date_exam_   Date_exam_   days_   days_   days_   days_   days_
Obs   id   _TYPE_   _FREQ_       1            2            3            4            5        ago_1   ago_2   ago_3   ago_4   ago_5

 1     1      1        2     01/10/2020   02/10/2020            .            .            .     393     392       .       .     .
 2     2      1        3     11/10/2001   15/10/2001   15/10/2002            .            .    7323    7319    6954       .     .
 3     3      1        4     16/09/2010   16/09/2010   16/09/2004   16/09/2018            .    4061    4061    6252    1139     .
 4     4      1        2     30/01/1960   30/01/1960            .            .            .   22553   22553       .       .     .
 5     5      1        1     06/09/2017            .            .            .            .    1514       .       .       .     .
&lt;/PRE&gt;
&lt;P&gt;You still need to set an upper bound on the number of copies you want out. So either pre-count or just live with your guess for the right number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Oct 2021 04:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777165#M147</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-29T04:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777340#M148</link>
      <description>Is PROC SUMMARY limited to only character variables though?</description>
      <pubDate>Fri, 29 Oct 2021 16:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777340#M148</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-29T16:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777352#M149</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Is PROC SUMMARY limited to only character variables though?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=sashelp.class nway ;
  class age ;
  output out=want idgroup(out[5] (Name Sex Height Weight)= ) ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Oct 2021 16:26:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777352#M149</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-10-29T16:26:08Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose more than 3 variables without use proc transpose</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777358#M150</link>
      <description>Nice! I always forget about that method for some reason &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Fri, 29 Oct 2021 16:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/Transpose-more-than-3-variables-without-use-proc-transpose/m-p/777358#M150</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-29T16:42:23Z</dc:date>
    </item>
  </channel>
</rss>

