Stata to Mysql: Escaping Quotes

If you use multiple linked databases or tables, I highly recommend linking mysql and Stata for both data merging and analysis. You can store the data in one clean place with relational tables and have all of Stata’s capabilities at hand with the odbc command. Before you start running “odbc insert” commands, you have to make sure the data is compatible with SQL queries. This means all strings need the quotes escaped. I was doing so many of these escapes that I wrote an ado file to simplify the process.

Download: escape_all_quotes

The script loops through all the variables in your dataset, checks if each is a string and if so, escapes any quotes. Although it has worked for 1000s of my “odbc insert”s, their could be bugs or speed improvements. Let me know.

Leave a Reply