Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: How to cite a macro in ODBC's SQL select statement


From   "Joseph Coveney" <jcoveney@bigplanet.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: How to cite a macro in ODBC's SQL select statement
Date   Wed, 23 Nov 2011 13:45:49 +0900

G. Dai wrote:

I have one problem about how to cite macro in ODBC.
For example, I try to read some variables yesterday's value into the
memory of Stata, what I did is

local todate=date(c(current_date),"DMY")
local ydate=`todate'-1
local yyear=year(`ydate')
local ymonth=month(`ydate')
local yday=day(`ydate')

odbc load, exec("SELECT BEGIN_DATE END_DATE VARIABLES  WHERE
BEGIN_DATE BETWEEN TO_DATE ('`ymonth'/`yday'/`yyear' 00', 'MM/DD/YYYY
HH24') AND TO_DATE  ('`ymonth'/`yday'/`yyear' 00', 'MM/DD/YYYY
HH24')")  dsn("SQL Database1")

However, this doesn't work since the SQL select statement ignors the
Stata macro.
What should I do?

--------------------------------------------------------------------------------

I use local macros in -odbc exec()- all of the time without any problem, so your
problem is probably along the lines of Nick's guess.

One easy thing that you can do is to verify that -odbc- sees what you intend it
to.  At the point in your code where you've got the -odbc exec()- line insert a
-display- of its -exec()- argument, like below.  (It will be wrapped in the
e-mail processing, but you'll see what I mean.)

display in smcl as text "SELECT BEGIN_DATE END_DATE VARIABLES WHERE BEGIN_DATE
BETWEEN TO_DATE('`ymonth'/`yday'/`yyear' 00', 'MM/DD/YYYY HH24') AND
TO_DATE('`ymonth'/`yday'/`yyear' 00', 'MM/DD/YYYY HH24')"

In Stata's Results window, take a look at the arguments of the two TO_DATE()
function calls and verify that they're what you intend.

Joseph Coveney


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index