<?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: Transpose Data; Multiple Observations into one row per subject in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602578#M174472</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285252"&gt;@sassy_lm&lt;/a&gt;:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Read the file to find out what it the largest [id,bid] group on file&lt;/LI&gt;
&lt;LI&gt;Use the result to size up arrays and create the requisite number of array variables for the output&lt;/LI&gt;
&lt;LI&gt;Read the file again by [id,bid] (it's assumed sorted accordingly) and populate the arrays using the record number in each BY group as an index, starting for each BY group at 1&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;In other (i.e. SAS) words:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                           
  input ID BID Dx_year dx_code $ short_description &amp;amp; :$12. ;                          
  cards ;                                                                             
1  250  2000  12345  HTN                                                              
1  250  2000  12344  obesity                                                          
1  250  2000  12333  vomiting                                                         
2  870  2002  12223  TB                                                               
2  870  2002  14322  anemia                                                           
2  870  2003  12355  tobacco use                                                      
2  870  2003  14325  infection                                                        
;                                                                                     
run ;                                                                                 
                                                                                      
proc sql noprint ;                                                                    
  select cats (max (g)) into :g from (select count (*) as g from have group id, bid) ;
quit ;                                                                                
                                                                                      
%put &amp;amp;=g ;                                                                            
                                                                                      
data want (drop = dx_year dx_code short_description) ;                                
  do _i_ = 1 by 1 until (last.bid) ;                                                  
    set have ;                                                                        
    by id bid ;                                                                       
    array dxy     dx_year1-dx_year&amp;amp;g ;                                                
    array dxc $ 8 dx_code1-dx_code&amp;amp;g ;                                                
    array sd  $12 short_description1-short_description&amp;amp;g ;                            
    dxy = dx_year ;                                                                   
    dxc = dx_code ;                                                                   
    sd  = short_description ;                                                         
  end ;                                                                               
run ;                                                                                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The implicit OUTPUT before RUN writes out the array variables populated through the iterations of the DoW-loop as one record.&lt;/LI&gt;
&lt;LI&gt;Then program control is passed back to the top of the step and the array variables are auto-populated with missing values since they are not retained.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;You can kill the %PUT statement. It's just FYI.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Nov 2019 20:08:36 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-11-07T20:08:36Z</dc:date>
    <item>
      <title>Transpose Data; Multiple Observations into one row per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602536#M174466</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am wondering how I can transpose the data below which currently has multiple rows per subject (Table 1). I would like to create a new dataset with one row per subject (Table 2). The variables with multiple obs are associated with ID and BID. Thank you in advance! - Liz&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table 1:&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;BID&lt;/TD&gt;&lt;TD&gt;Dx_year&lt;/TD&gt;&lt;TD&gt;dx_code&lt;/TD&gt;&lt;TD&gt;short_description&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;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t;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;BID&lt;/TD&gt;&lt;TD&gt;Dx_year1&lt;/TD&gt;&lt;TD&gt;Dx_year2&lt;/TD&gt;&lt;TD&gt;Dx_year3&lt;/TD&gt;&lt;TD&gt;Dx_year4&lt;/TD&gt;&lt;TD&gt;dx_code1&lt;/TD&gt;&lt;TD&gt;dx_code2&lt;/TD&gt;&lt;TD&gt;dx_code3&lt;/TD&gt;&lt;TD&gt;dx_code4&lt;/TD&gt;&lt;TD&gt;short_description1&lt;/TD&gt;&lt;TD&gt;short_description2&lt;/TD&gt;&lt;TD&gt;short_description3&lt;/TD&gt;&lt;TD&gt;short_description4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;250&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;2000&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;12345&lt;/TD&gt;&lt;TD&gt;12344&lt;/TD&gt;&lt;TD&gt;12333&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;HTN&lt;/TD&gt;&lt;TD&gt;obesity&lt;/TD&gt;&lt;TD&gt;vomiting&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;870&lt;/TD&gt;&lt;TD&gt;2002&lt;/TD&gt;&lt;TD&gt;2002&lt;/TD&gt;&lt;TD&gt;2003&lt;/TD&gt;&lt;TD&gt;2003&lt;/TD&gt;&lt;TD&gt;12223&lt;/TD&gt;&lt;TD&gt;14322&lt;/TD&gt;&lt;TD&gt;12355&lt;/TD&gt;&lt;TD&gt;14325&lt;/TD&gt;&lt;TD&gt;TB&lt;/TD&gt;&lt;TD&gt;anemia&lt;/TD&gt;&lt;TD&gt;tobacco use&lt;/TD&gt;&lt;TD&gt;infection&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 07 Nov 2019 18:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602536#M174466</guid>
      <dc:creator>sassy_lm</dc:creator>
      <dc:date>2019-11-07T18:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Data; Multiple Observations into one row per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602545#M174467</link>
      <description>&lt;P&gt;Are BID, Dx_year and dx_code all numeric variables, or are some character variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Thu, 07 Nov 2019 18:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602545#M174467</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2019-11-07T18:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Data; Multiple Observations into one row per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602561#M174469</link>
      <description>BID and Dx_year are numeric variables. However, the variable dx_code is a character variable.&lt;BR /&gt;&lt;BR /&gt;Thanks for asking! Happy to answer any other questions.&lt;BR /&gt;&lt;BR /&gt;- Liz</description>
      <pubDate>Thu, 07 Nov 2019 19:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602561#M174469</guid>
      <dc:creator>sassy_lm</dc:creator>
      <dc:date>2019-11-07T19:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Data; Multiple Observations into one row per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602578#M174472</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285252"&gt;@sassy_lm&lt;/a&gt;:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Read the file to find out what it the largest [id,bid] group on file&lt;/LI&gt;
&lt;LI&gt;Use the result to size up arrays and create the requisite number of array variables for the output&lt;/LI&gt;
&lt;LI&gt;Read the file again by [id,bid] (it's assumed sorted accordingly) and populate the arrays using the record number in each BY group as an index, starting for each BY group at 1&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;In other (i.e. SAS) words:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                           
  input ID BID Dx_year dx_code $ short_description &amp;amp; :$12. ;                          
  cards ;                                                                             
1  250  2000  12345  HTN                                                              
1  250  2000  12344  obesity                                                          
1  250  2000  12333  vomiting                                                         
2  870  2002  12223  TB                                                               
2  870  2002  14322  anemia                                                           
2  870  2003  12355  tobacco use                                                      
2  870  2003  14325  infection                                                        
;                                                                                     
run ;                                                                                 
                                                                                      
proc sql noprint ;                                                                    
  select cats (max (g)) into :g from (select count (*) as g from have group id, bid) ;
quit ;                                                                                
                                                                                      
%put &amp;amp;=g ;                                                                            
                                                                                      
data want (drop = dx_year dx_code short_description) ;                                
  do _i_ = 1 by 1 until (last.bid) ;                                                  
    set have ;                                                                        
    by id bid ;                                                                       
    array dxy     dx_year1-dx_year&amp;amp;g ;                                                
    array dxc $ 8 dx_code1-dx_code&amp;amp;g ;                                                
    array sd  $12 short_description1-short_description&amp;amp;g ;                            
    dxy = dx_year ;                                                                   
    dxc = dx_code ;                                                                   
    sd  = short_description ;                                                         
  end ;                                                                               
run ;                                                                                 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The implicit OUTPUT before RUN writes out the array variables populated through the iterations of the DoW-loop as one record.&lt;/LI&gt;
&lt;LI&gt;Then program control is passed back to the top of the step and the array variables are auto-populated with missing values since they are not retained.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;You can kill the %PUT statement. It's just FYI.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 20:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602578#M174472</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-07T20:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Data; Multiple Observations into one row per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602583#M174475</link>
      <description>&lt;P&gt;My suggestion would be to use the macro a group of us wrote and presented at SAS Global Forum a couple of years ago.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to do that would be to run the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename ut url 'https://raw.githubusercontent.com/art297/transpose/master/transpose.sas';
%include ut ;

%transpose(data=have, out=want, by=ID BID, Guessingrows=1000,
  var=dx_year dx_code short_description)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to see or download the actual macro and/or the paper, you can find it at:&amp;nbsp;&lt;A href="https://github.com/art297/transpose" target="_blank"&gt;https://github.com/art297/transpose&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 22:58:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602583#M174475</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2019-11-07T22:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose Data; Multiple Observations into one row per subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602608#M174479</link>
      <description>&lt;P&gt;And how is that data set to be used?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2019 20:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-Data-Multiple-Observations-into-one-row-per-subject/m-p/602608#M174479</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-07T20:32:06Z</dc:date>
    </item>
  </channel>
</rss>

