<?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: Long to wide in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785809#M250831</link>
    <description>Two proc transposes plus a merge/join. &lt;BR /&gt;Or an array method.</description>
    <pubDate>Mon, 13 Dec 2021 17:32:17 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-12-13T17:32:17Z</dc:date>
    <item>
      <title>Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785803#M250828</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is the way to create data set "wanted"&amp;nbsp; from dataset "have" ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
Data have;
input id serial  Revenue  profit;
cards;
1 1 10 2
1 2 20 5
1 3 30 7
1 4 40 9
2 1 5 3
2 2 10 8
2 3 . .
2 4 . .
;
Run;

data wanted;
input ID Revenue1 Revenue2 Revenue3 Revenue4 profit1 profit2 profit3 profit4;
cards;
1 10 20 30 40 2 5 7 9
2 5 10 . . 3 8 . .
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Dec 2021 17:12:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785803#M250828</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-12-13T17:12:45Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785807#M250829</link>
      <description>&lt;P&gt;If you're looking for a solution for just this one specific case (and not trying to generalize it), then you could use brute force:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input id serial  Revenue  profit;
cards;
1 1 10 2
1 2 20 5
1 3 30 7
1 4 40 9
2 1 5 3
2 2 10 8
2 3 . .
2 4 . .
;
Run;

data wanted (drop=serial revenue profit); set have;
retain revenue1 revenue2 revenue3 revenue4 profit1 profit2 profit3 profit4;
if serial=1 then do;
 revenue1=revenue;
 profit1=profit;
 end;
if serial=2 then do;
 revenue2=revenue;
 profit2=profit;
 end;
if serial=3 then do;
 revenue3=revenue;
 profit3=profit;
 end;
if serial=4 then do;
 revenue4=revenue;
 profit4=profit;
 end;
if serial=4 then output;
run;

proc print data=wanted; run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="table.png" style="width: 605px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66707i35EBA75A81F3337A/image-size/large?v=v2&amp;amp;px=999" role="button" title="table.png" alt="table.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Dec 2021 17:26:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785807#M250829</guid>
      <dc:creator>GraphGuy</dc:creator>
      <dc:date>2021-12-13T17:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785809#M250831</link>
      <description>Two proc transposes plus a merge/join. &lt;BR /&gt;Or an array method.</description>
      <pubDate>Mon, 13 Dec 2021 17:32:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785809#M250831</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-13T17:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785810#M250832</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;The real data including 20 million rows in long structure and 20 variables so I am looking for a more efficient solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Dec 2021 17:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785810#M250832</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-12-13T17:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785812#M250833</link>
      <description>&lt;P&gt;this trick seen first time from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select max(n) into :n from (select count(*) as n from have group by id);
quit;

proc summary data=have;
by id;
output out=wanted(drop=_:) idgroup(out[&amp;amp;n] (revenue profit)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Dec 2021 17:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785812#M250833</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2021-12-13T17:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785813#M250834</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose
	data = have (keep = id revenue:)
	out = want_rev (drop = _name_)
	prefix = revenue;
	by id;
		var revenue;
run;

proc transpose
	data = have (keep = id profit:)
	out = want_prof (drop = _name_)
	prefix = profit;
	by id;
		var profit;
run;

data want;
	merge want_rev want_prof;
	by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Dec 2021 17:37:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785813#M250834</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-12-13T17:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785826#M250838</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The real data including 20 million rows in long structure and 20 variables so I am looking for a more efficient solution&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;More efficient solution: work with the data in the long structure.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Dec 2021 18:09:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785826#M250838</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-13T18:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785836#M250844</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;The real data including 20 million rows in long structure and 20 variables so I am looking for a more efficient solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Array method is slightly more efficient, single pass of the data then. The tutorial below shows exactly what you need.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But why?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Transposing data tutorials:&lt;BR /&gt;Long to Wide:&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/modules/reshaping-data-long-to-wide-using-the-data-step/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Dec 2021 18:22:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/785836#M250844</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-13T18:22:35Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/787181#M251464</link>
      <description>&lt;P&gt;If your data's in a massively-parallel database like Oracle, Teradata, Snowflake, DB2, check out the LONG2WIDE macro in this old 2008 SGF paper:&lt;BR /&gt;&lt;BR /&gt;It's a Bird, It's a Plane, It's SQL Transpose!&lt;BR /&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/pdfs/sgf2008/089-2008.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings/pdfs/sgf2008/089-2008.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An SQL CASE statement approach still work with SAS datasets, but I'm thinking a DATA step using arrays or hard-coding would be faster, although SAS SQL has gotten much better at parallel SQL execution over the years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;p.s. As an alternative to using a SAS macro to generate SQL, I've also seen people use Excel formulas to generate transpose CASE statement logic from a list of column names and copy-and-paste that into their queries!&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 23:50:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/787181#M251464</guid>
      <dc:creator>tc</dc:creator>
      <dc:date>2021-12-22T23:50:29Z</dc:date>
    </item>
  </channel>
</rss>

