Hi there,
I'm trying to do something like the following to use the IDs from an existing dataset to specify which records to pull in from an external database using an odbc load, exec command. I think I'm close, but can't quite get it to work. A similar scenario others might want to use this for would be with a Stata inlist command. Would appreciate any tips!
*Put all distinct values of ID variable into a local macro
levelsof id, local(ids)
*Add single quotes around each id number and a comma afterwards, for use in SQL "IN" statement
local idsf '`ids'',
*Now pull in data from external database
#delimit ;
odbc load, exec("
SELECT id, characteristic
FROM table
WHERE id IN ( substr(`idsf',1,length(`idsf')-1)) )
") clear low u($usr) p($pwd) dsn("TABLE")
;
#delimit cr
*Above, the purpose of the substr command is to trim the comma off of the final item in the list of IDs
I'm trying to do something like the following to use the IDs from an existing dataset to specify which records to pull in from an external database using an odbc load, exec command. I think I'm close, but can't quite get it to work. A similar scenario others might want to use this for would be with a Stata inlist command. Would appreciate any tips!
*Put all distinct values of ID variable into a local macro
levelsof id, local(ids)
*Add single quotes around each id number and a comma afterwards, for use in SQL "IN" statement
local idsf '`ids'',
*Now pull in data from external database
#delimit ;
odbc load, exec("
SELECT id, characteristic
FROM table
WHERE id IN ( substr(`idsf',1,length(`idsf')-1)) )
") clear low u($usr) p($pwd) dsn("TABLE")
;
#delimit cr
*Above, the purpose of the substr command is to trim the comma off of the final item in the list of IDs
Comment