<?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 proc sql question - min function in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100014#M28074</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Okay,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So this question isn't quite "how to do something within proc sql" but more about &lt;EM&gt;how&lt;/EM&gt; proc sql implements the min() function -- &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Essentially, I have this piece of SAS code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table day_t1_path_t as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select Acct as acct_t1, CurBal as curbal_t1, Date as date_t1, &lt;STRONG&gt;min(date) as path_start_t1&lt;/STRONG&gt;, 2 as tenure_t1,&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; min((select curbal_t from day_t_path_t where acct_t = Acct),curbal) as minbal_t1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; from small_init;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now what I am trying to do is port this code over to be able to run in SQL management studio with raw SQL (no sas functions) -- I know that the Min() function works differently in SAS than SQL, for example to be able to implement the second min() call in SQL I need to use the case statement because SQL's min function doesn't allow for variable comparisons, only minimums across columns (i.e: "case when (select curbal_t from day_t_path_t where acct_t = Acct) &amp;lt; curbal then (select curbal_t from day_t_path_t where acct_t = Acct) else CURBAL end) as minbal_t1")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, the problem I'm having is with the first call to min() "&lt;STRONG&gt;min(date) as path_start_t1&lt;/STRONG&gt;"&amp;nbsp; -- I am trying to take the minimum value for "date" in the "from" table, and use that value for observations of this "&lt;STRONG&gt;path_start_t1&lt;/STRONG&gt;" variable in my select clause. This way the new table I am creating will have a column "&lt;STRONG&gt;path_start_t1&lt;/STRONG&gt;" where it's value in each row is equal to the minimum date in the "from" table. -- This works perfectly in SAS, but in SQL I get an error for all of the variables in the select clause other than the min(date) because they aren't in a group by (or aggregate function) -- this makes sense because in SQL the min(function) is pulling one single value and so must collapse all other variables in the select clause by groupings in the select clause so that it is clear to SQL how to determine the return value for min() in each row of the output table. What I would like to understand is &lt;EM&gt;how&lt;/EM&gt; this works in SAS - is the under lying function implementing some extra SQL queries to make this possible, or is this not at all possible in SQL and done with some other internal SAS functions?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope my question is clear and applicable to this forum. I'd appreciate any information, thank you very much!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 01 Oct 2013 17:15:54 GMT</pubDate>
    <dc:creator>AllSoEasy</dc:creator>
    <dc:date>2013-10-01T17:15:54Z</dc:date>
    <item>
      <title>proc sql question - min function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100014#M28074</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Okay,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So this question isn't quite "how to do something within proc sql" but more about &lt;EM&gt;how&lt;/EM&gt; proc sql implements the min() function -- &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Essentially, I have this piece of SAS code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table day_t1_path_t as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select Acct as acct_t1, CurBal as curbal_t1, Date as date_t1, &lt;STRONG&gt;min(date) as path_start_t1&lt;/STRONG&gt;, 2 as tenure_t1,&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp; min((select curbal_t from day_t_path_t where acct_t = Acct),curbal) as minbal_t1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; from small_init;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now what I am trying to do is port this code over to be able to run in SQL management studio with raw SQL (no sas functions) -- I know that the Min() function works differently in SAS than SQL, for example to be able to implement the second min() call in SQL I need to use the case statement because SQL's min function doesn't allow for variable comparisons, only minimums across columns (i.e: "case when (select curbal_t from day_t_path_t where acct_t = Acct) &amp;lt; curbal then (select curbal_t from day_t_path_t where acct_t = Acct) else CURBAL end) as minbal_t1")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, the problem I'm having is with the first call to min() "&lt;STRONG&gt;min(date) as path_start_t1&lt;/STRONG&gt;"&amp;nbsp; -- I am trying to take the minimum value for "date" in the "from" table, and use that value for observations of this "&lt;STRONG&gt;path_start_t1&lt;/STRONG&gt;" variable in my select clause. This way the new table I am creating will have a column "&lt;STRONG&gt;path_start_t1&lt;/STRONG&gt;" where it's value in each row is equal to the minimum date in the "from" table. -- This works perfectly in SAS, but in SQL I get an error for all of the variables in the select clause other than the min(date) because they aren't in a group by (or aggregate function) -- this makes sense because in SQL the min(function) is pulling one single value and so must collapse all other variables in the select clause by groupings in the select clause so that it is clear to SQL how to determine the return value for min() in each row of the output table. What I would like to understand is &lt;EM&gt;how&lt;/EM&gt; this works in SAS - is the under lying function implementing some extra SQL queries to make this possible, or is this not at all possible in SQL and done with some other internal SAS functions?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope my question is clear and applicable to this forum. I'd appreciate any information, thank you very much!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Oct 2013 17:15:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100014#M28074</guid>
      <dc:creator>AllSoEasy</dc:creator>
      <dc:date>2013-10-01T17:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql question - min function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100015#M28075</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Do it in a subquery cross joined in?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;select a.*, b.path_start_t1&lt;/P&gt;&lt;P&gt;from small_init&lt;/P&gt;&lt;P&gt;cross join (select min(date) as path_start_t1 from small_init) b&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Oct 2013 17:34:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100015#M28075</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-10-01T17:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql question - min function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100016#M28076</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ah ha yes it is that simple! Very interesting that SAS internally does this work automatically. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Oct 2013 19:04:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100016#M28076</guid>
      <dc:creator>AllSoEasy</dc:creator>
      <dc:date>2013-10-01T19:04:51Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql question - min function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100017#M28077</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It does do it automatically but then tells you it does it, so the same amount of "work" behind the scenes, but less up front for the programmer perhaps &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2671&amp;nbsp; proc sql;&lt;/P&gt;&lt;P&gt;2672&amp;nbsp; create table want as&lt;/P&gt;&lt;P&gt;2673&amp;nbsp; select *, min(age) as min_age&lt;/P&gt;&lt;P&gt;2674&amp;nbsp; from sashelp.class;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;NOTE: The query requires remerging summary statistics back with the original&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;NOTE: Table WORK.WANT created, with 19 rows and 6 columns.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Oct 2013 20:13:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100017#M28077</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2013-10-01T20:13:24Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql question - min function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100018#M28078</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wow I didn't even notice that, I never thought that the Log would reveal that kind of information, that's super interesting! Definitely does make things a little more straight-forward for the user/programmer -- in that context the intended result is clear; if only SQL studio performed that kind of functionality wrapping behind the scenes!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks Reeza!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Oct 2013 20:17:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-question-min-function/m-p/100018#M28078</guid>
      <dc:creator>AllSoEasy</dc:creator>
      <dc:date>2013-10-01T20:17:14Z</dc:date>
    </item>
  </channel>
</rss>

