0

I am wondering how can I convert Stata code into Python code.

For example, my Stata code looks like

if ("`var1'"=="") {
    local l_QS "select distinct CountryName from `l_tableName'"
    ODBCLoad, exec("`l_QS'") dsn("`dsn'") clear

}

And I want to convert it to Python code such as

if (f"{var1}"=="") :
    l_QS = f"select distinct CountryName from {l_tableName}"
    SQL_read(f"{l_QS}", dsn = f"{dsn}")

I am new to coding so I don't know what branch of computer science knowledge or what tools/techniques are relevant. I suppose knowledge about compilers and/or using regular expressions may help so I put those tags on my question. Any high-level pointers are appreciated, and specific code examples would be even better. Thanks in advance.

GoCurry
  • 899
  • 11
  • 31
  • What is the purpose of converting the code? Is there a reason you want to change it to python code if it works in Stata? – pythonweb Aug 15 '18 at 22:09
  • Because we are moving the entire code base from Stata to Python. Stata is not performant and hard to maintain. – GoCurry Aug 15 '18 at 22:11
  • Generally, you would need to understand both languages and write a translation. – juanpa.arrivillaga Aug 16 '18 at 00:54
  • @juanpa.arrivillaga How do I write a translation? Where do I start with? Can you point me to some examples? Thanks a lot. – GoCurry Aug 16 '18 at 02:14
  • @GoCurry At a high level, this would "just" be a compiler that outputs Python code instead of assembler. Lots of talk on that [here](https://stackoverflow.com/questions/1669/learning-to-write-a-compiler) But I'd imagine it would be easier to just do it manually... –  Aug 16 '18 at 19:30

1 Answers1

0

A very simple workaround would be to use the subprocess module included with python and write a basic command line wrapper to your scripts to use their functionality, then build your code from now on in python.

You could also look into possible API functionality in Stata if you have a whole lot of Stata code and it would take forever to convert it manually to python. This would require you to have access to a server and could be potentially costly, but would be cleaner than the subprocess module and wouldn't require the source code to be contained on your local machine. Also note that it's possible that Stata does not have tools to build an API.

As far as I am aware there are no projects that will directly parse a file from any language and convert it into python. This would be a huge project, although maybe with machine learning or AI it would be possible, though still very difficult. There are libraries for wrapping code in C and C++ (others too I'm sure I just know that these are available), but I can't find anything for Stata.

pythonweb
  • 1,024
  • 2
  • 11
  • 26