Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: Re: ODBC import


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: ODBC import
Date   Sat, 14 Dec 2013 03:23:05 +0900

Janet Hill wrote:

I need to import some large tables from Access 2013 to Stata 13.1 and I have been using the following command:
clear
odbc load,exec(`"SELECT Demographics.*, Visit.* FROM Demographics INNER JOIN Visit ON Demographics.Hospid = Visit.HosPid"')///
lower allstring connectionstring(`"Driver=Microsoft Access Driver (*.mdb, *.accdb);DBQ=C:\Students\September.accdb"')

This fails with the error:
hospid already defined
r(110);

and this occurs because Stata does not prefix the column name with the table name. I can explicitly add each table.column, which works on a small dataset,but because there are 15 columns in one table and 30 in the other this becomes unwieldy and prone to typing errors. Other tables may be larger.

I would be grateful for any suggestions on how to solve this problem.
--------------------------------------------------------------------------------

You can get the list of columns of one of your tables into a Stata local macro,
and then use Stata's extended macro functions (-help extended_fcn-) to help 
create your all-but-the-join-column list of columns to insert into your SQL 
statement on the fly.  Something like that below.

As an aside, you might want to re-consider your data model.  Tables with 15 and
30 columns aren't usual.  And, from the names of the tables, Hospid sounds more
like it would be better named as patient_id.

Joseph Coveney

odbc load, exec("SELECT * FROM Visit WHERE 2 = 3;") ///
    connectionstring("`connection_string'")
foreach var of varlist _all {
    local column_list `column_list' `var'
}
local column_list : subinstr local column_list "Hospid" ""
local column_list : subinstr local column_list " " " B."
#delimit ;
local sql_statement SELECT A.*, `column_list' 
    FROM Demographics AS A 
    INNER JOIN Visit AS B 
    ON A.Hospid = B.Hospid;
#delimit cr
odbc load, exec("`sql_statement';") ///
    connectionstring("`connection_string'") clear


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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index