<?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 a table with non-unique IDs - create unknown number of columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transpose-a-table-with-non-unique-IDs-create-unknown-number-of/m-p/830665#M328223</link>
    <description>&lt;P&gt;I would like to transpose the 'have' table so that there is one row per ID. I would like to order the events by date, so that 'type1' corresponds to the type of event that happened first and 'type2' to the type of event that happened second, etc. I don't necessarily know how many rows there are per ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help! I still find proc transpose to be confusing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  input id type $ (date) (:yymmdd.) amount;
  format date yymmdd10.;
datalines;                      
1 A 2019/01/01 100
1 B 2019/10/01 200 
1 A 2018/10/01 200 
2 C 2019/08/01 150
2 A 2020/08/08 250
2 C 2021/08/08 200 
2 B 2022/08/08 250
;

data want; 
input id type1 $ (date1) (:yymmdd.) amount1 type2 $ (date2) (:yymmdd.) amount2 type3 $ (date3) (:yymmdd.) amount3 type4 $ (date4) (:yymmdd.) amount4;
format date1 date2 date3 date4 yymmdd10.;
datalines; 
1 A 2018/10/01 200 A 2019/01/01 100 B 2019/10/01 200 . . . 
2 C 2019/08/01 150 A 2020/08/08 250 C 2021/08/08 200 B 2022/08/08 250
; &lt;/PRE&gt;</description>
    <pubDate>Fri, 26 Aug 2022 18:56:13 GMT</pubDate>
    <dc:creator>kz_</dc:creator>
    <dc:date>2022-08-26T18:56:13Z</dc:date>
    <item>
      <title>Transpose a table with non-unique IDs - create unknown number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-a-table-with-non-unique-IDs-create-unknown-number-of/m-p/830665#M328223</link>
      <description>&lt;P&gt;I would like to transpose the 'have' table so that there is one row per ID. I would like to order the events by date, so that 'type1' corresponds to the type of event that happened first and 'type2' to the type of event that happened second, etc. I don't necessarily know how many rows there are per ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help! I still find proc transpose to be confusing.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  input id type $ (date) (:yymmdd.) amount;
  format date yymmdd10.;
datalines;                      
1 A 2019/01/01 100
1 B 2019/10/01 200 
1 A 2018/10/01 200 
2 C 2019/08/01 150
2 A 2020/08/08 250
2 C 2021/08/08 200 
2 B 2022/08/08 250
;

data want; 
input id type1 $ (date1) (:yymmdd.) amount1 type2 $ (date2) (:yymmdd.) amount2 type3 $ (date3) (:yymmdd.) amount3 type4 $ (date4) (:yymmdd.) amount4;
format date1 date2 date3 date4 yymmdd10.;
datalines; 
1 A 2018/10/01 200 A 2019/01/01 100 B 2019/10/01 200 . . . 
2 C 2019/08/01 150 A 2020/08/08 250 C 2021/08/08 200 B 2022/08/08 250
; &lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 18:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-a-table-with-non-unique-IDs-create-unknown-number-of/m-p/830665#M328223</guid>
      <dc:creator>kz_</dc:creator>
      <dc:date>2022-08-26T18:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose a table with non-unique IDs - create unknown number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-a-table-with-non-unique-IDs-create-unknown-number-of/m-p/830670#M328225</link>
      <description>&lt;P&gt;That is not what PROC TRANSPOSE is designed to handle.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Either write your own data step, probably using ARRAYs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or use PROC SUMMARY with IDGROUP.&amp;nbsp;&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings10/102-2010.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings10/102-2010.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;You need to tell PROC SUMMARY how many copies of the variables you want to create.&lt;/P&gt;
&lt;P&gt;You could just hard code some maximum value, like 4, in this case.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id type $ date :yymmdd. amount;
  format date yymmdd10.;
datalines;                      
1 A 2019/01/01 100
1 B 2019/10/01 200 
1 A 2018/10/01 200 
2 C 2019/08/01 150
2 A 2020/08/08 250
2 C 2021/08/08 200 
2 B 2022/08/08 250
;

%let max=4 ;

proc summary data=have;
  by id;
  output out=want idgroup(out[&amp;amp;max] (type date amount)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1661541126791.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74778i40843A8898BB7146/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1661541126791.png" alt="Tom_0-1661541126791.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If you need to count the maximum number of copies then you can add an SQL step before the PROC SUMMARY step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select max(n) into :max trimmed
  from (select id,count(*) as n from have group by id)
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Aug 2022 19:14:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-a-table-with-non-unique-IDs-create-unknown-number-of/m-p/830670#M328225</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-26T19:14:00Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose a table with non-unique IDs - create unknown number of columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-a-table-with-non-unique-IDs-create-unknown-number-of/m-p/830683#M328230</link>
      <description>This is perfect! And so simple too. Thank you!</description>
      <pubDate>Fri, 26 Aug 2022 20:16:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-a-table-with-non-unique-IDs-create-unknown-number-of/m-p/830683#M328230</guid>
      <dc:creator>kz_</dc:creator>
      <dc:date>2022-08-26T20:16:13Z</dc:date>
    </item>
  </channel>
</rss>

