<?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 multiple columns dataset into one row by subject ID in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865953#M38270</link>
    <description>&lt;P&gt;The first question to ask is what do you expect to do with that data set that you cannot do with the current structure?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The varying number of resulting variables means that any code using such a data structure is hard to maintain if you have more data later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the only purpose is for a person to read that data then perhaps a report is what you actually want such as:&lt;/P&gt;
&lt;PRE&gt;data have;
  input ID   Startdate :mmddyy10.  Stopdate :mmddyy10.  Result;
  format startdate stopdate mmddyy10.;
datalines;
1           02/03/2011           02/05/2011            1
1           03/22/2016           .                     3
1           05/13/2016          05/13/2016             4
2           11/11/2011           11/13/2011            2
2           03/23/2012           03/23/2012            1
2           09/10/2016           .                     4
2            02/02/2019          02/02/2019            2
;

proc sort data=have;
   by id startdate.;
run;

data need;
   set have;
   by id;
   retain resultorder;
   if first.id then resultorder=1;
   else resultorder+1;
run;
proc report data=need;
   columns id resultorder,( startdate stopdate result);
   define id /group;
   define resultorder/across "Result order";
run;&lt;/PRE&gt;
&lt;P&gt;Please note the first data step is the preferred way to show example data. If you provide a data step then you have controlled the variable properties which are important.&lt;/P&gt;
&lt;P&gt;Whatever you want you likely need to add an order variable, which I called ResultOrder, to identify the order.&lt;/P&gt;</description>
    <pubDate>Thu, 23 Mar 2023 14:46:38 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-03-23T14:46:38Z</dc:date>
    <item>
      <title>Transposing multiple columns dataset into one row by subject ID</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865946#M38268</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that looks like this (sorted by ID and startdate):&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Startdate&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stopdate&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Result&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/03/2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;02/05/2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/22/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;05/13/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 05/13/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11/11/2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;11/13/2011&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/23/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/23/2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;09/10/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/02/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/02/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;I want the data transposed like this, so each Id has only one row:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; startdate1&amp;nbsp; &amp;nbsp; stopdate1&amp;nbsp; &amp;nbsp;result1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;startdate2&amp;nbsp; &amp;nbsp; stopdate2&amp;nbsp; result2&amp;nbsp; &amp;nbsp;startdate3 stopdate3&amp;nbsp; result3&amp;nbsp; startdate4&amp;nbsp; etc.&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/03/2011&amp;nbsp; &amp;nbsp; 02/05/2011&amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;03/22/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 05/13/2016&amp;nbsp; 05/13/2016&amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 11/11/2011&amp;nbsp; &amp;nbsp;11/13/2011&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;03/23/2012&amp;nbsp; &amp;nbsp; 03/23/2012&amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;09/10/2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp;&amp;nbsp;02/02/2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried using data step for transposing but I end up having several transposed datasets that confuse me. I need help in creating a code that can get me the above dataset smoothly as I have to repeat this code in several different datasets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 14:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865946#M38268</guid>
      <dc:creator>Rahim_221</dc:creator>
      <dc:date>2023-03-23T14:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing multiple columns dataset into one row by subject ID</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865953#M38270</link>
      <description>&lt;P&gt;The first question to ask is what do you expect to do with that data set that you cannot do with the current structure?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The varying number of resulting variables means that any code using such a data structure is hard to maintain if you have more data later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the only purpose is for a person to read that data then perhaps a report is what you actually want such as:&lt;/P&gt;
&lt;PRE&gt;data have;
  input ID   Startdate :mmddyy10.  Stopdate :mmddyy10.  Result;
  format startdate stopdate mmddyy10.;
datalines;
1           02/03/2011           02/05/2011            1
1           03/22/2016           .                     3
1           05/13/2016          05/13/2016             4
2           11/11/2011           11/13/2011            2
2           03/23/2012           03/23/2012            1
2           09/10/2016           .                     4
2            02/02/2019          02/02/2019            2
;

proc sort data=have;
   by id startdate.;
run;

data need;
   set have;
   by id;
   retain resultorder;
   if first.id then resultorder=1;
   else resultorder+1;
run;
proc report data=need;
   columns id resultorder,( startdate stopdate result);
   define id /group;
   define resultorder/across "Result order";
run;&lt;/PRE&gt;
&lt;P&gt;Please note the first data step is the preferred way to show example data. If you provide a data step then you have controlled the variable properties which are important.&lt;/P&gt;
&lt;P&gt;Whatever you want you likely need to add an order variable, which I called ResultOrder, to identify the order.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 14:46:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865953#M38270</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-23T14:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing multiple columns dataset into one row by subject ID</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865963#M38274</link>
      <description>&lt;P&gt;Thank you so much for replying. I tried your code and it worked as a report. The code creates what I wanted in a different way (which I personally like) but for data management purposes, I want the data "Need" itself to be transposed exactly as I described in my post. I have tried different codes but I end up getting 3 transposed columns each in one row, which is not really what I want to have as an outcome.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:10:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865963#M38274</guid>
      <dc:creator>Rahim_221</dc:creator>
      <dc:date>2023-03-23T15:10:26Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing multiple columns dataset into one row by subject ID</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865967#M38275</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/436120"&gt;@Rahim_221&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you so much for replying. I tried your code and it worked as a report. The code creates what I wanted in a different way (which I personally like) but for data management purposes, I want the data "Need" itself to be transposed exactly as I described in my post. I have tried different codes but I end up getting 3 transposed columns each in one row, which is not really what I want to have as an outcome.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can create data set with proc report:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc report data=need out=reporttable;
   columns id resultorder,( startdate stopdate result);
   define id /group;
   define resultorder/across "Result order";
run;&lt;/PRE&gt;
&lt;P&gt;HOWEVER the variable names in this type of out put will be _C2_ _C3_ _C4_ . You could rename them&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One of the obnoxious problems with wanting specific column orders is that &lt;STRONG&gt;you&lt;/STRONG&gt; have to spend a lot of time futzing with names.&lt;/P&gt;
&lt;P&gt;You could transpose each variable like this:&lt;/P&gt;
&lt;PRE&gt;proc transpose data=need out=start (drop=_name_)
      prefix=Startdate;
   by id;
   id resultorder;
   var startdate;
run;&lt;/PRE&gt;
&lt;P&gt;(an exercise for the interested reader to do the other variables)&lt;/P&gt;
&lt;P&gt;and then combine the sets. However the easy way to combine the sets by MERGE will not have the column order you want.&lt;/P&gt;
&lt;P&gt;Solutions to get unspecified numbers of created variables in order get into the macro language which often means that minor changes in the data can make a lot of work updating code.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 15:23:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/865967#M38275</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-03-23T15:23:56Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing multiple columns dataset into one row by subject ID</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/866030#M38277</link>
      <description>Thank you &lt;span class="lia-unicode-emoji" title=":folded_hands:"&gt;🙏&lt;/span&gt;</description>
      <pubDate>Fri, 24 Mar 2023 03:10:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/866030#M38277</guid>
      <dc:creator>Rahim_221</dc:creator>
      <dc:date>2023-03-24T03:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: Transposing multiple columns dataset into one row by subject ID</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/866089#M38286</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input ID          (Startdate             Stopdate             Result) (:$40.);
cards;
1           02/03/2011           02/05/2011            1
1           03/22/2016           .                             3
1           05/13/2016          05/13/2016             4
2           11/11/2011           11/13/2011             2
2           03/23/2012           03/23/2012            1
2           09/10/2016           .                             4
2            02/02/2019          02/02/2019             2
;

data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
run;
proc sql noprint;
select distinct catt('temp(where=(n=',n,') rename=(Startdate=Startdate',n,' Stopdate=Stopdate',n,' Result=Result',n,'))')
 into : merge separated by ' ' 
 from temp;
quit;

data want;
 merge &amp;amp;merge.;
 by id;
 drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Mar 2023 11:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Transposing-multiple-columns-dataset-into-one-row-by-subject-ID/m-p/866089#M38286</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-03-24T11:57:47Z</dc:date>
    </item>
  </channel>
</rss>

