<?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 to: use MAX and CASE in one PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787455#M251616</link>
    <description>&lt;P&gt;Also, it appears to me that all you have accomplished by all this SQL code is to transpose your data set. If so, use PROC TRANSPOSE! Again the benefits are that if you get it to work for 4 questions, it also works in the case that you have 40 questions, with no extra coding; and even in the case of 4 questions it is a lot less coding than your SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=got out=want prefix=m;
    by id;
    id question;
	var response;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 27 Dec 2021 14:15:17 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-12-27T14:15:17Z</dc:date>
    <item>
      <title>How to: use MAX and CASE in one PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787452#M251613</link>
      <description>&lt;P&gt;Good People -&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A sample program is shown below with two PROC SQLs.&amp;nbsp; I would like to know how I can combine the second PROC SQL, which uses MAX and GROUP&amp;nbsp; BY, with the first PROC SQL, which establishes a series of new variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data got;&lt;BR /&gt;input id question response;&lt;BR /&gt;datalines;&lt;BR /&gt;1 1 0&lt;BR /&gt;1 2 2&lt;BR /&gt;1 3 1&lt;BR /&gt;1 4 0&lt;BR /&gt;2 1 1&lt;BR /&gt;2 2 2&lt;BR /&gt;2 3 3&lt;BR /&gt;2 4 3&lt;BR /&gt;3 1 0&lt;BR /&gt;3 2 0&lt;BR /&gt;3 3 0&lt;BR /&gt;3 4 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc print data=got;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table one as&lt;BR /&gt;select id&lt;BR /&gt;,case when question=1 then response else 0 end as question_1&lt;BR /&gt;,case when question=2 then response else 0 end as question_2&lt;BR /&gt;,case when question=3 then response else 0 end as question_3&lt;BR /&gt;,case when question=4 then response else 0 end as question_4&lt;BR /&gt;from got&lt;BR /&gt;order by id;&lt;BR /&gt;quit;&lt;BR /&gt;proc print data=one;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table two as&lt;BR /&gt;select id&lt;BR /&gt;,max(question_1) as m1&lt;BR /&gt;,max(question_2) as m2&lt;BR /&gt;,max(question_3) as m3&lt;BR /&gt;,max(question_4) as m4&lt;BR /&gt;from one&lt;BR /&gt;group by id;&lt;BR /&gt;quit;&lt;BR /&gt;proc print data=two;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 13:25:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787452#M251613</guid>
      <dc:creator>rogerward</dc:creator>
      <dc:date>2021-12-27T13:25:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to: use MAX and CASE in one PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787454#M251615</link>
      <description>&lt;P&gt;In another thread, I mentioned (not to you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17916"&gt;@rogerward&lt;/a&gt; , but in general) that people should not choose SQL as their first choice for performing statistical or mathematical calculations (in this case, the maximum of many values). This is a great example why. This is a lot of typing, and if you have more than 4 questions, the typing increases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On the other hand, if you use PROC SUMMARY (or PROC MEANS), this is 5 lines of code, regardless of how many questions there are. You see, SAS has already programmed most common statistical analyses, and it has programmed the ability to handle many (arbitrary) levels of a variable; but in SQL you have to write your own code. If all you ever have is 4 questions, you can probably get away with doing it in SQL (the answers will be the same if you do it right), but if you have 40 questions, SQL would be a nightmare.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=got nway;
    class id question;
    var response;
    output out=want max=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 14:03:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787454#M251615</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-27T14:03:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to: use MAX and CASE in one PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787455#M251616</link>
      <description>&lt;P&gt;Also, it appears to me that all you have accomplished by all this SQL code is to transpose your data set. If so, use PROC TRANSPOSE! Again the benefits are that if you get it to work for 4 questions, it also works in the case that you have 40 questions, with no extra coding; and even in the case of 4 questions it is a lot less coding than your SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=got out=want prefix=m;
    by id;
    id question;
	var response;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 14:15:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787455#M251616</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-27T14:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to: use MAX and CASE in one PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787459#M251617</link>
      <description>&lt;P&gt;Thanks for your creativity.&amp;nbsp; I had been using PROC SUMMARY and PROC MEANS without issue. In my search for tricks to do the same task using multiple methods, I was pondering how to accomplish the same using PROC SQL, and was mystified.&amp;nbsp; I thought I was missing something.&amp;nbsp; PROC SQL is appealing since many times it executes faster.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 15:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787459#M251617</guid>
      <dc:creator>rogerward</dc:creator>
      <dc:date>2021-12-27T15:32:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to: use MAX and CASE in one PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787461#M251619</link>
      <description>&lt;P&gt;Just take the MAX() of the value of the CASE clause.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table two as
  select id
       , max(case when question=1 then response else 0 end) as m_1
       , max(case when question=2 then response else 0 end) as m_2
       , max(case when question=3 then response else 0 end) as m_3
       , max(case when question=4 then response else 0 end) as m_4
  from got
  group by id
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But for your actual data just use PROC TRANSPOSE, since there is only one response per id per question.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=got out=three(drop=_name_) prefix=m_;
  by id;
  id question;
  var response;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 15:34:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787461#M251619</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-27T15:34:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to: use MAX and CASE in one PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787463#M251621</link>
      <description>&lt;P&gt;I'm surprised when you say SQL executes faster than PROC MEANS/PROC SUMMARY or PROC TRANSPOSE. That hasn't been my experience. I guess it depends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, as I pointed out, coding time is much shorter using PROC MEAN/PROC SUMMARY or PROC TRANSPOSE. As a general rule, statistical or mathematical functions ought to be computed using built-in SAS functions or PROCs, instead of coding it up yourself in SQL. (I have seen people try to code their own functions in SQL or a DATA step, instead of using built-in methods, and they get it wrong, even for something as simple as an average)&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 16:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787463#M251621</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-27T16:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to: use MAX and CASE in one PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787470#M251625</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17916"&gt;@rogerward&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;PROC SQL is appealing since many times it executes faster.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In this case, I used 50000 IDs (instead of your 3 records), and 40 questions (instead of your 4 questions), and ran the one SQL solution from &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="sasLogNote4_1640621744628" class="sasNote"&gt;NOTE: PROCEDURE SQL used (Total process time):&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;real time 2.28 seconds&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;user cpu time 2.62 seconds&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;system cpu time 0.64 seconds&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;
&lt;DIV id="sasLogNote7_1640621744628" class="sasNote"&gt;NOTE: PROCEDURE TRANSPOSE used (Total process time):&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;real time 1.62 seconds&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;user cpu time 1.62 seconds&lt;/DIV&gt;
&lt;DIV class="sasNote"&gt;system cpu time 0.00 seconds&lt;/DIV&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;/DIV&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 16:19:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-MAX-and-CASE-in-one-PROC-SQL/m-p/787470#M251625</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-27T16:19:30Z</dc:date>
    </item>
  </channel>
</rss>

