I have a Stata plug-in that allows me to read SQL from a file, run it on my PostgreSQL server, and have the data delivered to Stata.
Unfortunately, a lot of my SQL relies on newlines being correctly parsed so as to generate correct (or even valid) SQL. For example,
SELECT *
FROM some_table
-- Require something to be true
WHERE some_boolean;
ends up in Stata as SELECT * FROM some_table -- Require something to be true WHERE some_boolean;
, which clearly doesn't work.
Here is the .ado
file I use to read the data. I have tried many approaches, but can't find a way to put newlines into the exec
variable.
program define loadsql
*! Load the output of an SQL file into Stata, version 1.4 (iandgow@gmail.com)
version 13.1
syntax using/, CONN(string)
#delimit;
tempname sqlfile exec line;
file open `sqlfile' using `"`using'"', read text;
file read `sqlfile' `line';
while r(eof)==0 {;
local `exec' `"``exec'' ``line''
"';
file read `sqlfile' `line';
};
file close `sqlfile';
* display "`conn'";
pgload "`conn'" "``exec''", clear;
* pgload "``dsn''" "SELECT permno, date, abs(prc) AS prc FROM crsp.dsf LIMIT 10", clear;
end;