<?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 multiple columns Long to Wide in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919905#M362324</link>
    <description>&lt;P&gt;Maxim 19: Long Beats Wide.&lt;/P&gt;
&lt;P&gt;Do not put data (years) in structure (variable names).&lt;/P&gt;
&lt;P&gt;Such datasets are sub-optimal to work with; for reporting purposes, PROC REPORT can handle this in a much better way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report data=long2;
column famid year,(faminc spend);
define famid / group;
define year / "" across;
define faminc / analysis;
define spend / analysis;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 12 Mar 2024 09:43:19 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2024-03-12T09:43:19Z</dc:date>
    <item>
      <title>Transpose multiple columns Long to Wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919887#M362320</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;Is there any better way than the one below?&lt;/P&gt;
&lt;P&gt;If I have to transpose 20 columns, I have to run that SQL 19 times. Of course, I can put that into Macro, but I bet there should be a better method.&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long2; 
  input famid year faminc spend ; 
cards; 
1 96 40000 38000 
1 97 40500 39000 
1 98 41000 40000 
2 96 45000 42000 
2 97 45400 43000 
2 98 45800 44000 
3 96 75000 70000 
3 97 76000 71000 
3 98 77000 72000 
; 
run ;

proc transpose data=long2 out=widef prefix=faminc;
   by famid;
   id year;
   var faminc;
run;

proc transpose data=long2 out=wides prefix=spend;
   by famid;
   id year;
   var spend;
run;

data wide2;
    merge  widef(drop=_name_) wides(drop=_name_);
    by famid;
run;

proc print data=wide2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2024 03:30:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919887#M362320</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2024-03-12T03:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose multiple columns Long to Wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919890#M362321</link>
      <description>&lt;P&gt;If you don't mind specifing range of year, and year is always a integer, you can use DOW-Loop to transpose long data to wide:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  array _fam_[96:98]faminc96-faminc98; 
  array _spe_[96:98]spend96-spend98; 
  do until(last.famid);
    set long2;
    by famid;
    _fam_[year]=faminc;
    _spe_[year]=spend;
  end;
  drop year faminc spend;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Mar 2024 03:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919890#M362321</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2024-03-12T03:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose multiple columns Long to Wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919895#M362322</link>
      <description>&lt;P&gt;Check the MERGE skill proposed by me, Arthur.T and Matt.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings15/2785-2015.pdf" target="_blank"&gt;Paper Template (sas.com)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data long2; 
  input famid year faminc spend ; 
cards; 
1 96 40000 38000 
1 97 40500 39000 
1 98 41000 40000 
2 96 45000 42000 
2 97 45400 43000 
2 98 45800 44000 
3 96 75000 70000 
3 97 76000 71000 
3 98 77000 72000 
; 

proc sql noprint;
select distinct catt('long2(where=(year=',year,') rename=(faminc=faminc',year,' spend=spend',year,'))') into :merge separated by ' '
 from long2;
quit;
data want;
 merge &amp;amp;merge.;
 by famid;
 drop year;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1710221614630.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94577i34FD7F44052701F0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1710221614630.png" alt="Ksharp_0-1710221614630.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2024 05:33:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919895#M362322</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-03-12T05:33:40Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose multiple columns Long to Wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919904#M362323</link>
      <description>&lt;P&gt;If you want a report of some sort that is wide, don't transpose it at all. Use PROC REPORT. Almost everything in SAS is designed to work with a long (not wide) data set, PROC REPORT produces a wide report from a long data set. This also produces a nicer format than transposing, and you have much more control over the appearance of the report, and the column headers don't have to be actual SAS variable names.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2024 09:05:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919904#M362323</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-03-12T09:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose multiple columns Long to Wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919905#M362324</link>
      <description>&lt;P&gt;Maxim 19: Long Beats Wide.&lt;/P&gt;
&lt;P&gt;Do not put data (years) in structure (variable names).&lt;/P&gt;
&lt;P&gt;Such datasets are sub-optimal to work with; for reporting purposes, PROC REPORT can handle this in a much better way.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report data=long2;
column famid year,(faminc spend);
define famid / group;
define year / "" across;
define faminc / analysis;
define spend / analysis;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Mar 2024 09:43:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919905#M362324</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-03-12T09:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose multiple columns Long to Wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919930#M362327</link>
      <description>&lt;P&gt;Use PROC SUMMARY.&amp;nbsp; You will need to know the number of copies you need to output. (you could always count first and put the number into a macro variable.)&lt;/P&gt;
&lt;P&gt;For you example data there is maximum of 3 observations per FAMID.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=long2;
  by famid;
  output out=wide3 idgroup( out[3] (year faminc spend)=);
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-1710247373580.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94583i42C8C71B6DD7B162/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1710247373580.png" alt="Tom_0-1710247373580.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2024 12:43:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/919930#M362327</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-12T12:43:08Z</dc:date>
    </item>
    <item>
      <title>Re: Transpose multiple columns Long to Wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/920042#M362378</link>
      <description>Tom,&lt;BR /&gt;The drawback of PROC SUMMARY is the  max number of "out[3]" is 100.</description>
      <pubDate>Wed, 13 Mar 2024 01:09:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Transpose-multiple-columns-Long-to-Wide/m-p/920042#M362378</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-03-13T01:09:27Z</dc:date>
    </item>
  </channel>
</rss>

