<?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: max in proc sql query for sql server in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594550#M170834</link>
    <description>there are multiple records where code (20%) is satisfied. For eg, one single account has multiple records for various dates where code (20%) is satisfied. So instead of selecting all the records for that account, I only need the record for that account with the latest date where code (20%) is satisfied. Hope this helps.</description>
    <pubDate>Mon, 07 Oct 2019 20:42:14 GMT</pubDate>
    <dc:creator>AJ_Brien</dc:creator>
    <dc:date>2019-10-07T20:42:14Z</dc:date>
    <item>
      <title>max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594525#M170819</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to get data from sql server table such that I need only the most recent observation- identified by max(dte).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I created the following code, it doesn't give an error, but it's producing results for all values of 'dte' and not just max(dte). I tried 'having' clause, but the 'group by' kept throwing me off. Would appreciate suggestions on what I should be changing in the following to get the desired result. Thank you!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	connect to SQLSVR (	user="%sysget(USER)" password="xx" dsn="xx" );
	create table table1 as select * from connection to SQLSVR
		(
			select 
					account , id, code, dte
			 from dbtable
  	    	where dte= (select max(dte) from dbtable)
			and code like '20%'
order by account;
);
	disconnect from SQLSVR;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2019 18:56:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594525#M170819</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-10-07T18:56:14Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594526#M170820</link>
      <description>&lt;P&gt;Perhaps the table only has the most recent data?&lt;/P&gt;
&lt;P&gt;Get a distribution on the values of DTE that are returned.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 18:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594526#M170820</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-07T18:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594528#M170821</link>
      <description>Thank you for your reply.&lt;BR /&gt;dte basically holds the dates, so instead of getting observations for the latest date, it's returning observations for all dates.&lt;BR /&gt;the source dbtable contains data from the last 10 years, so it's important to pull only the latest information.</description>
      <pubDate>Mon, 07 Oct 2019 19:03:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594528#M170821</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-10-07T19:03:20Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594531#M170824</link>
      <description>&lt;P&gt;How about this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	select account , id, code, dte
	from dbtable as A
        inner join 
        (select  account , id, code, max(dte) as max_dte
         from dbtable
         group by account , id, code
        ) as B
        on  A.account = B.account
        and A.id = B.id
        and A.code = B.code
        where A.code like '20%' and A.dte = B.max_dte              
        order by account;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2019 19:25:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594531#M170824</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-10-07T19:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594543#M170829</link>
      <description>thank you for the reply.&lt;BR /&gt;&lt;BR /&gt;sadly it still gives all date observations instead of the max date observation.</description>
      <pubDate>Mon, 07 Oct 2019 20:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594543#M170829</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-10-07T20:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594544#M170830</link>
      <description>&lt;P&gt;Sounds like a bug in SQL Server.&amp;nbsp; Talk to the admins for your database to understand what syntx SQL server is expecting for that query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could possibly use SAS's macro language to help by first pulling the maximum date into a macro variable and then using that date in the query that pulls the actual data.&amp;nbsp; Note you will need to understand how to make the value of the macro variable look like how SQL Server handles dates. Perhaps something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select cats("DATE('",put(datepart(max_dte),yymmdd10.),"')")
  into :max_date 
from connection to SQLSVR
  (select max(dte) as max_dte from dbtable)
;
create table table1 as 
select * from connection to SQLSVR
(
  select 
    account 
  , id
  , code
  , dte
  from dbtable
  where dte = &amp;amp;max_dte 
    and code like '20%'
  order by account
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2019 20:24:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594544#M170830</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-07T20:24:11Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594545#M170831</link>
      <description>&lt;P&gt;No too sure why you code doesn't work.&lt;/P&gt;
&lt;P&gt;If you want to try another syntax, all three queries return the same result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  select *
  from SASHELP.CLASS 
  where AGE = (select max(AGE) from SASHELP.CLASS)
  and SEX = 'M';
 
  select *
  from SASHELP.CLASS
  group by '1'
  having AGE = max(AGE)
     and SEX = 'M';

  select a.* 
  from SASHELP.CLASS a
      ,(select max(AGE) as MAX
        from SASHELP.CLASS)
  where AGE = MAX
    and SEX = 'M';&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 20:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594545#M170831</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-10-07T20:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594547#M170832</link>
      <description>&lt;P&gt;Because you are selecting by code (20%) I assumed you want the maximum date for each code combination, not on all records, so you will get multiple rows coming back, but just the latest for each code combination. If this is not what you want then please advise.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 20:32:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594547#M170832</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-10-07T20:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594550#M170834</link>
      <description>there are multiple records where code (20%) is satisfied. For eg, one single account has multiple records for various dates where code (20%) is satisfied. So instead of selecting all the records for that account, I only need the record for that account with the latest date where code (20%) is satisfied. Hope this helps.</description>
      <pubDate>Mon, 07 Oct 2019 20:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594550#M170834</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-10-07T20:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594559#M170838</link>
      <description>&lt;P&gt;That is what I was trying to do. Try running just this then. You should only get one row for each value of account, id and code combination with the maximum date in each combination. Do you?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select  account , id, code, max(dte) as max_dte
         from dbtable
         where code like '20%'
         group by account , id, code&lt;BR /&gt;         order&amp;nbsp;by&amp;nbsp;account , id, code&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2019 21:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594559#M170838</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-10-07T21:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594587#M170852</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265086"&gt;@AJ_Brien&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;there are multiple records where code (20%) is satisfied. For eg, one single account has multiple records for various dates where code (20%) is satisfied. So instead of selecting all the records for that account, I only need the record for that account with the latest date where code (20%) is satisfied. Hope this helps.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is a totally different query than what you originally posted. The original query is only looking for the latest date independent of account. Then further filtering those to just the ones that match the code restriction.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please post some example data, made up data is fine, and the expected output for that example.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 00:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594587#M170852</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-08T00:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594597#M170860</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265086"&gt;@AJ_Brien&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below SQL works for a SAS table. There is no reason why this shouldn't yield the same result with SQL Server.&lt;/P&gt;
&lt;P&gt;There is nothing in the SQL which SAS couldn't push to the database so eventually try the implicit SQL from below first.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dbtable(drop=_:);
  do account=2 to 1 by -1;
    do code='2000','2010','1000';
      do _i=1 to 5;
        id+1000;
        dte=datetime()+id;
        output;
      end;
    end;
  end;

  format dte datetime20.;
  stop;
run;

options sastrace=',,,d' sastraceloc=saslog nostsuffix; 
proc sql;
  select 
    account , id, code, dte
  from dbtable
    where code like '20%'
      group by account
        having max(dte)=dte
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Oct 2019 02:37:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594597#M170860</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-08T02:37:24Z</dc:date>
    </item>
    <item>
      <title>Re: max in proc sql query for sql server</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594663#M170888</link>
      <description>&lt;P&gt;I understand that you want the latest record for each account, not just the records that are from the latest date in the whole table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One possibility is to use SAS SQL instead, you just need to assign a SAS libname referencing the SQL server schema:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname SQLSVR ODBC user="%sysget(USER)" password="xx" dsn="xx" schema=dbo;
proc sql;
	create table table1 as 
			select 
					account , id, code, dte
			 from SQLSVR.dbtable
  	    	where code like '20%'
       group by account
      having dte=max(dte)
order by account;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another is to restructure the SQL Server query a bit:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	connect to SQLSVR (	user="%sysget(USER)" password="xx" dsn="xx" );
create table table1 as select * from connection to SQLSVR
   (select account , id, code, dte
     from dbtable join
       (select account,max(dte) from dbtable                        
  	    	where code like '20%'
        group by account) lastdate on
      dbtable.account=lastdate.account and dbtable.dte=lastdate.dte
order by account;
);
	disconnect from SQLSVR;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Oct 2019 08:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-in-proc-sql-query-for-sql-server/m-p/594663#M170888</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-10-08T08:07:25Z</dc:date>
    </item>
  </channel>
</rss>

