Edit this page on our live server and create a PR by running command !create-pr in the console panel

Working with Stata

  • 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

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

In [1]:

You can use the variables instantly in Stata with SoS' own %expand magic,

In [2]:
/Users/bpeng1/Downloads/StataIntro/dataSets


marital  age      educ     sex      inc      happy    region

However, if you prefer, you can transferred the variables as local macros in Stata

In [3]:
S_FNDATE:       11 Sep 2009 09:55
S_FN:           gss.dta
stata_kernel_graph_counter:
                0
S_ADO:          BASE;SITE;.;PERSONAL;PLUS;OLDPLACE;`"/Users/bpeng1/anaconda3/envs/sos/lib/python3.6/site-packages/stata_kernel/ado"'
S_level:        95
F1:             help advice;
F2:             describe;
F7:             save
F8:             use
S_StataMP:      MP
S_StataSE:      SE
S_CONSOLE:      console
S_FLAVOR:       Intercooled
S_OS:           Unix
S_MACH:         Macintosh (Intel 64-bit)
_filename:      gss.dta
_datadir:       ~/Downloads/StataIntro/dataSets

and use the macros in Stata

In [4]:
/Users/bpeng1/Downloads/StataIntro/dataSets


marital  age      educ     sex      inc      happy    region

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.

In [5]:
Loading required package: feather
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
       index |          0
         mpg |         32    20.09062    6.026948       10.4       33.9
         cyl |         32      6.1875    1.785922          4          8
        disp |         32    230.7219    123.9387       71.1        472
          hp |         32    146.6875    68.56287         52        335
-------------+---------------------------------------------------------
        drat |         32    3.596563    .5346787       2.76       4.93
          wt |         32     3.21725    .9784574      1.513      5.424
        qsec |         32    17.84875    1.786943       14.5       22.9
          vs |         32       .4375    .5040161          0          1
          am |         32      .40625    .4989909          0          1
-------------+---------------------------------------------------------
        gear |         32      3.6875    .7378041          3          5
        carb |         32      2.8125      1.6152          1          8
In [6]:
(bin=5, start=52, width=56.6)

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

  1. If the name represents a local macro, send it as string
  2. Otherwise if the name represents a global macro, send it as string
  3. 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:

In [7]:
Out[7]:
index mpg cyl disp hp drat wt qsec vs am gear carb
0 Mazda RX4 21.0 6.0 160.0 110.0 3.90 2.620 16.46 0.0 1.0 4.0 4.0
1 Mazda RX4 Wag 21.0 6.0 160.0 110.0 3.90 2.875 17.02 0.0 1.0 4.0 4.0
2 Datsun 710 22.8 4.0 108.0 93.0 3.85 2.320 18.61 1.0 1.0 4.0 1.0
3 Hornet 4 Drive 21.4 6.0 258.0 110.0 3.08 3.215 19.44 1.0 0.0 3.0 1.0
4 Hornet Sportabout 18.7 8.0 360.0 175.0 3.15 3.440 17.02 0.0 0.0 3.0 2.0
5 Valiant 18.1 6.0 225.0 105.0 2.76 3.460 20.22 1.0 0.0 3.0 1.0
6 Duster 360 14.3 8.0 360.0 245.0 3.21 3.570 15.84 0.0 0.0 3.0 4.0
7 Merc 240D 24.4 4.0 146.7 62.0 3.69 3.190 20.00 1.0 0.0 4.0 2.0
8 Merc 230 22.8 4.0 140.8 95.0 3.92 3.150 22.90 1.0 0.0 4.0 2.0
9 Merc 280 19.2 6.0 167.6 123.0 3.92 3.440 18.30 1.0 0.0 4.0 4.0
10 Merc 280C 17.8 6.0 167.6 123.0 3.92 3.440 18.90 1.0 0.0 4.0 4.0
11 Merc 450SE 16.4 8.0 275.8 180.0 3.07 4.070 17.40 0.0 0.0 3.0 3.0
12 Merc 450SL 17.3 8.0 275.8 180.0 3.07 3.730 17.60 0.0 0.0 3.0 3.0
13 Merc 450SLC 15.2 8.0 275.8 180.0 3.07 3.780 18.00 0.0 0.0 3.0 3.0
14 Cadillac Fleetwood 10.4 8.0 472.0 205.0 2.93 5.250 17.98 0.0 0.0 3.0 4.0
15 Lincoln Continental 10.4 8.0 460.0 215.0 3.00 5.424 17.82 0.0 0.0 3.0 4.0
16 Chrysler Imperial 14.7 8.0 440.0 230.0 3.23 5.345 17.42 0.0 0.0 3.0 4.0
17 Fiat 128 32.4 4.0 78.7 66.0 4.08 2.200 19.47 1.0 1.0 4.0 1.0
18 Honda Civic 30.4 4.0 75.7 52.0 4.93 1.615 18.52 1.0 1.0 4.0 2.0
19 Toyota Corolla 33.9 4.0 71.1 65.0 4.22 1.835 19.90 1.0 1.0 4.0 1.0
20 Toyota Corona 21.5 4.0 120.1 97.0 3.70 2.465 20.01 1.0 0.0 3.0 1.0
21 Dodge Challenger 15.5 8.0 318.0 150.0 2.76 3.520 16.87 0.0 0.0 3.0 2.0
22 AMC Javelin 15.2 8.0 304.0 150.0 3.15 3.435 17.30 0.0 0.0 3.0 2.0
23 Camaro Z28 13.3 8.0 350.0 245.0 3.73 3.840 15.41 0.0 0.0 3.0 4.0
24 Pontiac Firebird 19.2 8.0 400.0 175.0 3.08 3.845 17.05 0.0 0.0 3.0 2.0
25 Fiat X1-9 27.3 4.0 79.0 66.0 4.08 1.935 18.90 1.0 1.0 4.0 1.0
26 Porsche 914-2 26.0 4.0 120.3 91.0 4.43 2.140 16.70 0.0 1.0 5.0 2.0
27 Lotus Europa 30.4 4.0 95.1 113.0 3.77 1.513 16.90 1.0 1.0 5.0 2.0
28 Ford Pantera L 15.8 8.0 351.0 264.0 4.22 3.170 14.50 0.0 1.0 5.0 4.0
29 Ferrari Dino 19.7 6.0 145.0 175.0 3.62 2.770 15.50 0.0 1.0 5.0 6.0
30 Maserati Bora 15.0 8.0 301.0 335.0 3.54 3.570 14.60 0.0 1.0 5.0 8.0
31 Volvo 142E 21.4 4.0 121.0 109.0 4.11 2.780 18.60 1.0 1.0 4.0 2.0

The macros can be obtained

In [8]:
Out[8]:
'BASE;SITE;.;PERSONAL;PLUS;OLDPLACE;`"/Users/bpeng1/anaconda3/envs/sos/lib/python3.6/site-packages/stata_kernel/ado"\''

and Stata supports the automatic transfer of variables (macros) with names starting with sos. For example,

In [9]:

but remember macros are untyped so they will be transferred as strings:

In [10]:
Out[10]:
'3.14159'

The %sessioninfo magic lists the version of Stata when the stata kernel is used: