- Difficulty level: easy
- Time need to lean: 10 minutes or less
- stata does not have a concept of variable and can only exchange stata dataset as dataframe
Stata, similar to SAS, has one and only one unnamed current dataset in memory. It also supports named, untyped macros. SoS supports the exchange of variables between Stata and other kernels as follows:
When pushing data to Stata
Python | variable name | Stata |
---|---|---|
int , float , str |
variable name become macro name | local macro (string ) |
pandas.DataFrame |
unnamed in Stata | Stata Dataset |
When getting data from Stata
Stata | variable name | Type in Python |
---|---|---|
local macros | name of macro | str |
global macros | name of macro | str |
Dataset | user specified | pandas.DataFrame |
Note that non-dataframe variables are transferred to Stata as local macros, and both local and global macros can be transferred although local macros will be checked first.
For example, if you have a directory and filenames defined in SoS
You can use the variables instantly in Stata with SoS' own %expand
magic,
However, if you prefer, you can transferred the variables as local macros in Stata
and use the macros in Stata
Now if you have a dataframe, for example, mtcard
in R, getting them in Stata will replace the current dataset, and the name mtcars
no longer matters.
Sending macros and/or dataset to another kernel is a bit tricky because local and global macros can have the same name, and the dataset is unnamed. The rule is that
- If the name represents a local macro, send it as string
- Otherwise if the name represents a global macro, send it as string
- Otherwise send the current dataset as a DataFrame using the provided name
For example, because mydata
is not one of the macros, it will become the name of the DataFrame representing the Stata dataset:
The macros can be obtained
and Stata supports the automatic transfer of variables (macros) with names starting with sos
. For example,
but remember macros are untyped so they will be transferred as strings:
The %sessioninfo
magic lists the version of Stata when the stata
kernel is used: