<?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: how do I achieve PIVOT (the one in EXCEL) in SQL? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567316#M159527</link>
    <description>&lt;P&gt;PROC REPORT will do this easily.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Insisting that SQL produce the solution makes your coding much more difficult.&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jun 2019 15:21:03 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-06-19T15:21:03Z</dc:date>
    <item>
      <title>how do I achieve PIVOT (the one in EXCEL) in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567311#M159525</link>
      <description>&lt;P&gt;Dear sas masters, I am trying to feel like the dummest person in the world, please help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data battlerecord;
input year $ com_a $ com_b $ result_of_a $;
datalines;
2015 INTEL AMD DEFEAT
2015 INTEL AMD WIN
2015 INTEL SAMSUNG WIN
2016 INTEL AMD DRAW
2016 AMD SAMSUNG LOSE
2016 AMD INTEL LOSE
2017 INTEL QUALCOMM WIN&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;desired output (first row is the column name):&amp;nbsp;&lt;/P&gt;&lt;P&gt;sorted by com_a and year:&lt;BR /&gt;(com_a) (year) (# of 'WIN's) (#of 'LOSE's) (# of 'DRAW's)&lt;BR /&gt;INTEL&amp;nbsp; &amp;nbsp; &amp;nbsp;2015&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;INTEL&amp;nbsp; &amp;nbsp; &amp;nbsp;2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;INTEL&amp;nbsp; &amp;nbsp; &amp;nbsp;2017&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;BR /&gt;AMD&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;doesn't matter this is achieved through SAS or SQL.&lt;/P&gt;&lt;P&gt;trust me, I have been trying to use the FIRST and LAST, and GROUP BY and HAVING,&lt;/P&gt;&lt;P&gt;but with my poor techniques I can't achieve what I want. desperately needing your help.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 15:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567311#M159525</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-06-19T15:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: how do I achieve PIVOT (the one in EXCEL) in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567316#M159527</link>
      <description>&lt;P&gt;PROC REPORT will do this easily.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Insisting that SQL produce the solution makes your coding much more difficult.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 15:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567316#M159527</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-19T15:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: how do I achieve PIVOT (the one in EXCEL) in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567317#M159528</link>
      <description>Do you need the output as a table or report?&lt;BR /&gt;It's possible in many different ways, including SQL.&lt;BR /&gt;&lt;BR /&gt;You should show something that you've tried, SQL or data step and we can help you get there.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 19 Jun 2019 15:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567317#M159528</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-19T15:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: how do I achieve PIVOT (the one in EXCEL) in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567453#M159570</link>
      <description>&lt;P&gt;Tabulate and transpose can come in useful:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc tabulate data=battlerecord format=6.0 out=battleCount;
class year com_a com_b result_of_a;
table com_a*year, result_of_a=""*n="";
run;

/* Write the counts to a dataset */
proc transpose data=battleCount out=battleTable(drop=_name_);
by com_a year;
id result_of_a;
var n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Jun 2019 20:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567453#M159570</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-06-19T20:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: how do I achieve PIVOT (the one in EXCEL) in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567487#M159584</link>
      <description>&lt;P&gt;Hm ... I wonder why you couldn't do it with SQL; it's pretty straightforward:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data battlerecord ;                                                  
  input year $ com_a $ com_b $ result_of_a $ ;                       
  cards ;                                                            
2015 INTEL AMD DEFEAT                                                
2015 INTEL AMD WIN                                                   
2015 INTEL SAMSUNG WIN                                               
2016 INTEL AMD DRAW                                                  
2016 AMD SAMSUNG LOSE                                                
2016 AMD INTEL LOSE                                                  
2017 INTEL QUALCOMM WIN                                              
run ;                                                                
                                                                     
option validvarname = any ;                                          
                                                                     
proc sql ;                                                           
  create table need as                                               
  select com_a as "(com_a)"n                                         
       , year as  "(year)"n                                          
       , sum (result_of_a in ("WIN")          ) as "(# of 'WIN's)"n  
       , sum (result_of_a in ("DEFEAT","LOSE")) as "(# of 'LOSE's)"n 
       , sum (result_of_a in ("DRAW")         ) as "(# of 'DRAW's)"n 
  from   battlerecord                                                
  group  1, 2                                                        
  order  1 desc, 2                                                   
  ;                                                                  
quit ;                                                               
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 00:25:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567487#M159584</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-20T00:25:50Z</dc:date>
    </item>
    <item>
      <title>Re: how do I achieve PIVOT (the one in EXCEL) in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567571#M159624</link>
      <description>&lt;P&gt;Yes, in this simple example, creating a PIVOT table is pretty simple using SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if the real-life problem needs a final output (pivot) table that has 12 or 20 or 75 or any other large number of columns or an un-counted number of columns, SQL is not as simple as in PROC REPORT.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 13:13:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567571#M159624</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-20T13:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: how do I achieve PIVOT (the one in EXCEL) in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567820#M159730</link>
      <description>Thanx works great! i didnt know there was a counting function (sum) in SQL.&lt;BR /&gt;Plus, what does group 1,2 and order 1 desc, 2 mean?&lt;BR /&gt;I always used group followed by a column name and this is a syntax i'm not familiar with.</description>
      <pubDate>Fri, 21 Jun 2019 03:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567820#M159730</guid>
      <dc:creator>jimmychoi</dc:creator>
      <dc:date>2019-06-21T03:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: how do I achieve PIVOT (the one in EXCEL) in SQL?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567826#M159734</link>
      <description>&lt;P&gt;Welcome.&lt;/P&gt;
&lt;P&gt;SUM is not a "counting function" (there's the COUNT function for this purpose) but it does what it says, i.e. sums.&lt;/P&gt;
&lt;P&gt;The reason we're SUMmarizing here is because we're dealing with Boolean expressions, such as:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;result_of_a &lt;SPAN class="token operator"&gt;in&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"WIN"&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which yield 1 if true and 0 if false.&lt;/P&gt;
&lt;P&gt;Group 1, 2 (or group BY 1, 2 - you can omit BY in SAS but not in data base specific SQLs) means: Deem the columns 1 and 2 according to their order in the SELECT clause categorical variables by which the summary function (in this case, SUM) aggregates the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Order 1 desc, 2 means: Sort the result set by column 1 included in the SELECT clause in descending order and, within it, by column 2 ascending.&lt;/P&gt;
&lt;P&gt;These are standard SQL shortcuts; the actual column names can be used just as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2019 04:11:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-I-achieve-PIVOT-the-one-in-EXCEL-in-SQL/m-p/567826#M159734</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-06-21T04:11:03Z</dc:date>
    </item>
  </channel>
</rss>

