<?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: Conversion of SQL pivot to proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835492#M330309</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How does &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s solution incorporate the MAX value portion of the query? Or will it always be only one value?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Which is why I asked whether the MAX() was actually part of the request or just an artifact of how the PIVOT syntax works.&amp;nbsp; I might be that for PIVOT to work you are required to use some type of aggregate function.&lt;/P&gt;</description>
    <pubDate>Tue, 27 Sep 2022 19:53:41 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-09-27T19:53:41Z</dc:date>
    <item>
      <title>Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835176#M330165</link>
      <description>&lt;P&gt;Any help to convert the following SQL DB query to SAS proc sql? Libname is already available to connect to SQL server.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;SELECT * FROM (
                SELECT [MATRL_NBR]
                                  ,[CLAS]
                                  ,[CHRSTC_NME]
                                  ,[CHRSTC_VALUE]
                  FROM [ALLIAN].[KPA].[MATRL_CLASFCTN] 
) as t1
                PIVOT (
                                max([CHRSTC_VALUE])
                                FOR [CHRSTC_NME] in ([IP_BU], [IP_BRAND]) 
) as t2
&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Sep 2022 14:23:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835176#M330165</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-09-26T14:23:30Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835221#M330198</link>
      <description>&lt;P&gt;Could you share some example data? Both before and after SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 16:37:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835221#M330198</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2022-09-26T16:37:18Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835223#M330200</link>
      <description>Sure, I'd like to know how to convert that SQL with Pivot in SAS.&lt;BR /&gt;</description>
      <pubDate>Mon, 26 Sep 2022 16:39:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835223#M330200</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-09-26T16:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835224#M330201</link>
      <description>&lt;P&gt;PROC REPORT is one way to get a "pivot table" in SAS, but that depends on exactly what is meant by "pivot table", and so yes, definitely seeing (a portion of) the data and the desired output would be extremely helpful.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 16:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835224#M330201</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-09-26T16:41:33Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835228#M330204</link>
      <description>&lt;P&gt;I'd do this in two steps personally, fully dynamic of course.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table t1 as
select matrl_nbr, clas, chrstc_nme, max(chrstc_value) as chrstc_value 
from kpa.matrl_clasfctn /*this may vary depending on how it's set up*/
where chrstc_nme in ('IP_BU', 'IP_BRAND');
group by matrl_nbr, clas, chrstc_nme;
quit;

proc transpose data=t1 out=t2;
by matrl_nbr clas chrstc_nme;
id chrstc_nme;
var chrstc_value;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There's possibly a way to do this in one using proc summary and ID statement but&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;is better at that type of code than I am.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How exactly you refer to the table name depends a bit on your set up to the library but you kpa should be the libname reference and the table name will be the same. Schema and DB are not relevant in this context, as they're specified in the libname definition.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 17:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835228#M330204</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-09-26T17:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835229#M330205</link>
      <description>&lt;P&gt;Describe it words what you are trying to accomplish.&lt;/P&gt;
&lt;P&gt;Explain what this PIVOT keyword does.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Share input and desired output datasets.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As SAS data steps to create the dataset(s) so we have something to test code with.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 17:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835229#M330205</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-26T17:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835260#M330226</link>
      <description>&lt;P&gt;Why do you need to convert it? You can run it unchanged in SAS SQL Passthru.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname sqlsrvr odbc noprompt = "server=MyServerName;DRIVER=SQL Server;Trusted Connection=yes;";

proc sql;
 connect using sqlsrvr;
  create table Want  as 
  select * from connection to sqlsrvr
  (SELECT * FROM (
                SELECT [MATRL_NBR]
                                  ,[CLAS]
                                  ,[CHRSTC_NME]
                                  ,[CHRSTC_VALUE]
                  FROM [ALLIAN].[KPA].[MATRL_CLASFCTN] 
    ) as t1
                PIVOT (
                                max([CHRSTC_VALUE])
                                FOR [CHRSTC_NME] in ([IP_BU], [IP_BRAND]) 
    ) as t2
   )
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Sep 2022 19:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835260#M330226</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-09-26T19:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835381#M330275</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp; Hopefully you can understand now if you read my question from the post and then you see this data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Input table:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="391"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;&lt;STRONG&gt;MATRL_NBR&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;&lt;STRONG&gt;CLAS&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;&lt;STRONG&gt;CHRSTC_NM&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;&lt;STRONG&gt;CHRSTC_VAL&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;82011101811&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SALES_DATA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;IP_BRAND&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;XGIRL SCOUT AWARD&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;7500547966&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SALES_DATA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;IP_BRAND&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;NOT ASSIGN&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;82011101811&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SALES_DATA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;IP_BU&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;X COOKIES&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;7500547966&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SALES_DATA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;IP_BU&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;NA&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;K1634890000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;PACKAGING&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;FOOD_FORM&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;WAFFLES&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;K1634890000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;PACKAGING&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SPECIAL_ID&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;24CT&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;82011101811&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SALES_DATA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;FOOD_FORM&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;WAFFLES&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;7500547966&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SALES_DATA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SPECIAL_ID&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;24CT&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Excepted Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="379"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;&lt;STRONG&gt;MATRL_NBR&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;&lt;STRONG&gt;CLAS&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;&lt;STRONG&gt;IP_BU&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;&lt;STRONG&gt;IP_BRAND&lt;/STRONG&gt;&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;82011101811&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SALES_DATA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;X COOKIES&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;XGIRL SCOUT AWARD&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;7500547966&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;SALES_DATA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;NA&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;NOT ASSIGN&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;
&lt;P&gt;K1634890000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="84"&gt;
&lt;P&gt;PACKAGING&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;NULL&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="139"&gt;
&lt;P&gt;NULL&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 27 Sep 2022 07:41:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835381#M330275</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2022-09-27T07:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835413#M330285</link>
      <description>&lt;P&gt;I don't see any description of what it is you are trying to do.&lt;/P&gt;
&lt;P&gt;Looks like you created new variables where the name is based on the values of one of the variables with the actual content is based on a second variable.&amp;nbsp; You grouped the data by two of the variables.&lt;/P&gt;
&lt;P&gt;SO that is what PROC TRANSPOSE does.&amp;nbsp; Assuming that the MAX() aggregate function mentioned in the "pivot" code is just an artifact of how that tool works.&amp;nbsp; If you actually have multiple IP_BRAND values in the same group then you need to explain why taking the maximum value makes any sense for your problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  infile cards dsd truncover;
  input (MATRL_NBR CLAS CHRSTC_NM CHRSTC_VAL) (:$30.);
cards;
82011101811,SALES_DATA,IP_BRAND,XGIRL SCOUT AWARD
7500547966,SALES_DATA,IP_BRAND,NOT ASSIGN
82011101811,SALES_DATA,IP_BU,X COOKIES
7500547966,SALES_DATA,IP_BU,NA
K1634890000,PACKAGING,FOOD_FORM,WAFFLES
K1634890000,PACKAGING,SPECIAL_ID,24CT
82011101811,SALES_DATA,FOOD_FORM,WAFFLES
7500547966,SALES_DATA,SPECIAL_ID,24CT
;

proc sort data=have;
  by MATRL_NBR CLAS ;
run;

proc transpose data=have out=want;
  where CHRSTC_NM =: 'IP_' ;
  by MATRL_NBR CLAS ;
  id CHRSTC_NM ;
  var CHRSTC_VAL ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs     MATRL_NBR        CLAS         _NAME_      IP_BRAND             IP_BU

 1     7500547966     SALES_DATA    CHRSTC_VAL    NOT ASSIGN           NA
 2     82011101811    SALES_DATA    CHRSTC_VAL    XGIRL SCOUT AWARD    X COOKIES
&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Sep 2022 12:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835413#M330285</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-27T12:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835490#M330307</link>
      <description>How does &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s solution incorporate the MAX value portion of the query? Or will it always be only one value?</description>
      <pubDate>Tue, 27 Sep 2022 19:52:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835490#M330307</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-09-27T19:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: Conversion of SQL pivot to proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835492#M330309</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;How does &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s solution incorporate the MAX value portion of the query? Or will it always be only one value?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Which is why I asked whether the MAX() was actually part of the request or just an artifact of how the PIVOT syntax works.&amp;nbsp; I might be that for PIVOT to work you are required to use some type of aggregate function.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Sep 2022 19:53:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conversion-of-SQL-pivot-to-proc-sql/m-p/835492#M330309</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-27T19:53:41Z</dc:date>
    </item>
  </channel>
</rss>

