<?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: Dynamically split and transpose data, joining by ID. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/802019#M315686</link>
    <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; -- yes, this simple transpose is working great! It turns out my actual dataset had been sorted incorrectly (not by&amp;nbsp;&lt;EM&gt;id&lt;/EM&gt;), so the transpose was not working properly. I was definitely overthinking this. Thanks for your second set of eyes.&lt;/P&gt;</description>
    <pubDate>Mon, 14 Mar 2022 18:02:10 GMT</pubDate>
    <dc:creator>mklangley</dc:creator>
    <dc:date>2022-03-14T18:02:10Z</dc:date>
    <item>
      <title>Dynamically split and transpose data, joining by ID.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/801995#M315677</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am looking for a simpler solution to transform the HAVE data into the format shown in WANT. The data in HAVE needs to be split by&amp;nbsp;&lt;EM&gt;type,&amp;nbsp;&lt;/EM&gt;transposed, then joined by&amp;nbsp;&lt;EM&gt;id&lt;/EM&gt;. In the transposition, the&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;variable is used as the new columns and the&amp;nbsp;&lt;EM&gt;amt&amp;nbsp;&lt;/EM&gt;variable are the value to transpose. The &lt;EM&gt;id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;variable is the common variable in the join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data is ordered by&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;and &lt;EM&gt;id&lt;/EM&gt;, ascending. There could be any number of&amp;nbsp;&lt;EM&gt;type&lt;/EM&gt;s--right now, HAVE contains four ("a" through "d"). In HAVE,&amp;nbsp;there are 5 rows for each&amp;nbsp;&lt;EM&gt;type. &lt;/EM&gt;Note that the&amp;nbsp;&lt;EM&gt;id&amp;nbsp;&lt;/EM&gt;starting number for each&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;increments.&amp;nbsp;For&amp;nbsp;the first&amp;nbsp;&lt;EM&gt;type, id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;goes from 1 to 5, then for the next&amp;nbsp;&lt;EM&gt;type, id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;goes from 2 to 6, then from 3 to 7, and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number of rows for each&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;is always the consistent within HAVE, although it may not always be 5; it could be 10, or 100. Hence the desire&amp;nbsp;for a dynamic (not hard-coded) solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One approach is to split HAVE by&amp;nbsp;&lt;EM&gt;type&lt;/EM&gt;&amp;nbsp;into intermediate datasets (i.e., all the &lt;EM&gt;type &lt;/EM&gt;= "a" data, all the&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;= "b" data, etc.), transpose each of these, then join them all together by &lt;EM&gt;id&lt;/EM&gt;. But I'm wondering if there is a simpler way that doesn't require numerous intermediate datasets and joins. (For the PROC TRANSPOSE pros: Can this all be done with a single PROC TRANSPOSE statement?)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the starting data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input type $ id amt;
    datalines;
a 1 99
a 2 98
a 3 97
a 4 96
a 5 95
b 2 94
b 3 93
b 4 92
b 5 91
b 6 90
c 3 89
c 4 88
c 5 87
c 6 86
c 7 85
d 4 84
d 5 83
d 6 82
d 7 81
d 8 80
    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is the desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    input id a b c d;
    datalines;
1 99 .  .  .
2 98 94 .  .
3 97 93 89 .
4 96 92 88 84
5 95 91 87 83
6 .  90 86 82
7 .  .  85 81
8 .  .  .  80
    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 17:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/801995#M315677</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2022-03-14T17:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically split and transpose data, joining by ID.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/801998#M315679</link>
      <description>&lt;P&gt;A straightforward transpose seems to work correctly here. Is there something in this output that doesn't align with what you're looking to achieve?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by id;
run;

proc transpose data=have out=want;
by id;
id type;
var amt;
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/223296"&gt;@mklangley&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am looking for a simpler solution to transform the HAVE data into the format shown in WANT. The data in HAVE needs to be split by&amp;nbsp;&lt;EM&gt;type,&amp;nbsp;&lt;/EM&gt;transposed, then joined by&amp;nbsp;&lt;EM&gt;id&lt;/EM&gt;. In the transposition, the&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;variable is used as the new columns and the&amp;nbsp;&lt;EM&gt;amt&amp;nbsp;&lt;/EM&gt;variable are the value to transpose. The &lt;EM&gt;id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;variable is the common variable in the join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data is ordered by&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;and &lt;EM&gt;id&lt;/EM&gt;, ascending. There could be any number of&amp;nbsp;&lt;EM&gt;type&lt;/EM&gt;s--right now, HAVE contains four ("a" through "d"). In HAVE,&amp;nbsp;there are 5 rows for each&amp;nbsp;&lt;EM&gt;type. &lt;/EM&gt;Note that the&amp;nbsp;&lt;EM&gt;id&amp;nbsp;&lt;/EM&gt;starting number for each&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;increments.&amp;nbsp;For&amp;nbsp;the first&amp;nbsp;&lt;EM&gt;type, id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;goes from 1 to 5, then for the next&amp;nbsp;&lt;EM&gt;type, id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;goes from 2 to 6, then from 3 to 7, and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number of rows for each&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;is always the consistent within HAVE, although it may not always be 5; it could be 10, or 100. Hence the desire&amp;nbsp;for a dynamic (not hard-coded) solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One approach is to split HAVE by&amp;nbsp;&lt;EM&gt;type&lt;/EM&gt;&amp;nbsp;into intermediate datasets (i.e., all the &lt;EM&gt;type &lt;/EM&gt;= "a" data, all the&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;= "b" data, etc.), transpose each of these, then join them all together by &lt;EM&gt;id&lt;/EM&gt;. But I'm wondering if there is a simpler way that doesn't require numerous intermediate datasets and joins.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the starting data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input type $ id amt;
    datalines;
a 1 99
a 2 98
a 3 97
a 4 96
a 5 95
b 2 94
b 3 93
b 4 92
b 5 91
b 6 90
c 3 89
c 4 88
c 5 87
c 6 86
c 7 85
d 4 84
d 5 83
d 6 82
d 7 81
d 8 80
    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is the desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    input id a b c d;
    datalines;
1 99 .  .  .
2 98 94 .  .
3 97 93 89 .
4 96 92 88 84
5 95 91 87 83
6 .  90 86 82
7 .  .  85 81
8 .  .  .  80
    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 17:41:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/801998#M315679</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-03-14T17:41:20Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically split and transpose data, joining by ID.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/801999#M315680</link>
      <description>&lt;P&gt;I don't know what you think a "split" would be.&lt;/P&gt;
&lt;P&gt;For your example Have data set:&lt;/P&gt;
&lt;PRE&gt;proc sort data=have;
   by id type;
run;

proc transpose data=have out=trans (drop=_name_);
   by id;
   id type;
   var amt;
run;&lt;/PRE&gt;
&lt;P&gt;seems to work as desired.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warning, if you have multiple values of the same Type for any given ID this won't work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/223296"&gt;@mklangley&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am looking for a simpler solution to transform the HAVE data into the format shown in WANT. The data in HAVE needs to be split by&amp;nbsp;&lt;EM&gt;type,&amp;nbsp;&lt;/EM&gt;transposed, then joined by&amp;nbsp;&lt;EM&gt;id&lt;/EM&gt;. In the transposition, the&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;variable is used as the new columns and the&amp;nbsp;&lt;EM&gt;amt&amp;nbsp;&lt;/EM&gt;variable are the value to transpose. The &lt;EM&gt;id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;variable is the common variable in the join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The data is ordered by&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;and &lt;EM&gt;id&lt;/EM&gt;, ascending. There could be any number of&amp;nbsp;&lt;EM&gt;type&lt;/EM&gt;s--right now, HAVE contains four ("a" through "d"). In HAVE,&amp;nbsp;there are 5 rows for each&amp;nbsp;&lt;EM&gt;type. &lt;/EM&gt;Note that the&amp;nbsp;&lt;EM&gt;id&amp;nbsp;&lt;/EM&gt;starting number for each&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;increments.&amp;nbsp;For&amp;nbsp;the first&amp;nbsp;&lt;EM&gt;type, id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;goes from 1 to 5, then for the next&amp;nbsp;&lt;EM&gt;type, id&lt;/EM&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;goes from 2 to 6, then from 3 to 7, and so on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The number of rows for each&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;is always the consistent within HAVE, although it may not always be 5; it could be 10, or 100. Hence the desire&amp;nbsp;for a dynamic (not hard-coded) solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One approach is to split HAVE by&amp;nbsp;&lt;EM&gt;type&lt;/EM&gt;&amp;nbsp;into intermediate datasets (i.e., all the &lt;EM&gt;type &lt;/EM&gt;= "a" data, all the&amp;nbsp;&lt;EM&gt;type&amp;nbsp;&lt;/EM&gt;= "b" data, etc.), transpose each of these, then join them all together by &lt;EM&gt;id&lt;/EM&gt;. But I'm wondering if there is a simpler way that doesn't require numerous intermediate datasets and joins. (For the PROC TRANSPOSE pros: Can this all be done with a single PROC TRANSPOSE statement?)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the starting data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input type $ id amt;
    datalines;
a 1 99
a 2 98
a 3 97
a 4 96
a 5 95
b 2 94
b 3 93
b 4 92
b 5 91
b 6 90
c 3 89
c 4 88
c 5 87
c 6 86
c 7 85
d 4 84
d 5 83
d 6 82
d 7 81
d 8 80
    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is the desired output:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    input id a b c d;
    datalines;
1 99 .  .  .
2 98 94 .  .
3 97 93 89 .
4 96 92 88 84
5 95 91 87 83
6 .  90 86 82
7 .  .  85 81
8 .  .  .  80
    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 17:43:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/801999#M315680</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-14T17:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically split and transpose data, joining by ID.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/802019#M315686</link>
      <description>&lt;P&gt;Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; -- yes, this simple transpose is working great! It turns out my actual dataset had been sorted incorrectly (not by&amp;nbsp;&lt;EM&gt;id&lt;/EM&gt;), so the transpose was not working properly. I was definitely overthinking this. Thanks for your second set of eyes.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 18:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/802019#M315686</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2022-03-14T18:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically split and transpose data, joining by ID.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/802023#M315688</link>
      <description>This works, too. Thank you!</description>
      <pubDate>Mon, 14 Mar 2022 18:03:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-split-and-transpose-data-joining-by-ID/m-p/802023#M315688</guid>
      <dc:creator>mklangley</dc:creator>
      <dc:date>2022-03-14T18:03:18Z</dc:date>
    </item>
  </channel>
</rss>

