-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpython_plugin.tex
More file actions
2746 lines (2221 loc) · 101 KB
/
python_plugin.tex
File metadata and controls
2746 lines (2221 loc) · 101 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{article}
\usepackage{tocloft}% http://ctan.org/pkg/tocloft
\usepackage{listings}
\usepackage{stata}
\usepackage{color}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{multicol}
\usepackage{titling}
\setlength{\droptitle}{-3em}
\setlength{\cftsubsecnumwidth}{3em}% Set length of number width in ToC for \subsection
\setlength{\arraycolsep}{50pt}
\lstset{basicstyle=\ttfamily}
\lstset{upquote=true}
\title{The Python plugin for Stata, version 0.2.0}
\author{James Fiedler}
\date{}
\begin{document}
\maketitle
\tableofcontents
\section{Introduction}
This document describes a Stata plugin for embedding the Python programming language within Stata. In short, the plugin gives the user the ability to use Python to interact with Stata data values, matrices, macros, and numeric scalars. The plugin can be used interactively inside the Stata GUI, or can be used to execute Python files. Python files can be used separately or in combination with \lstinline{.ado} or \lstinline{.do} files.
This code has been tested only on Windows 7, 64-bit computers, in Stata versions 12.1 and 13.0, with Python 3.3. Python 3.2 and 3.1 can probably be used instead of Python 3.3, but that has not been tested. The plugin will not work with Python 2. The code was developed for Stata 12.1 but works in Stata 13.0, except that string values must be \textsc{ASCII} and be no more than 244 characters long. In other words, the code works in Stata 13.0 when used with string values allowed in Stata 12.1.
Users will need to compile the plugin themselves. Instructions for compiling on Windows are given in \S\ref{installing}. Users will need Stata, Python (specifically, CPython, the most common version), and a C compiler. Users will also need access to the file \lstinline{Python.h}, which is included in many distributions of Python. The Windows installer at \lstinline{http://www.python.org/getit/} will install all of the Python files you need.
This document assumes the reader has some experience with Python, but extensive experience is not required.
\section{What's new}
Changes in version 0.2.0
\begin{itemize}
\item \lstinline{st_Matrix} and \lstinline{st_View} (capital \lstinline{M} and \lstinline{V}) have been replaced with \lstinline{st_matrix} and \lstinline{st_view}.
\item The usual ``\lstinline{>>>}'' Python prompt has been added to help differentiate Python mode from Stata Ado mode. Unfortunately, the default dot prompt remains, so the full prompt is ``\lstinline{>>>.}''.
\item New \lstinline{st_mirror} function. See the description of \lstinline{st_mirror} in \S\ref{func_descript}. See example usage in \S\ref{st_mirror_example}.
\item New \lstinline{stata_math} module. See \S\ref{stata_math_module} and see \S\ref{st_mirror_example} for example usage with \lstinline{st_mirror}.
\item \lstinline{python.ado} will now search for Python files when using the \lstinline{file} option. The referenced Python file can be anywhere in your Ado path.
\item In the \lstinline{st_matrix} and \lstinline{st_view} returned objects (instances of \lstinline{StataMatrix} and \lstinline{StataView} classes), the ``camelCase'' method names have been replaced with ``underscore\_case'' names. Also, these objects now do not show their contents as their default representation. To see their contents, use their \lstinline{list} method, as in examples \S\ref{st_view_example} and \S\ref{st_matrix_example}.
\end{itemize}
\section{Use with caution}
The plugin and helper files described here are experimental. Save your data before using the plugin. There is currently one known limitation/bug which can crash Stata. There may be other, unknown bugs that can crash Stata, too.
\subsection{Limitations} \label{limitations}
\begin{enumerate}
\item[1.] Dropping a Stata program that uses the Python plugin and then re-running it can crash Stata, depending on what Python modules are used in the program, and whether it's the only Stata program that uses the plugin. For many Python modules this is not a problem. Nor does it seem to be a problem to drop and re-run \lstinline{python.ado}, even if it's the only program using the plugin.
\textbf{Remedy:} It's not clear what is causing this problem, but there seems to be a simple solution. If wanting to drop a program that uses the plugin, make sure that another program also uses it---for example, use \lstinline{python.ado} at least once---or declare the plugin in Stata directly, with \lstinline{program python_plugin, plugin}.
\item[2.] The interactive Python interpreter within Stata is limited to single-line inputs. Unfortunately there is no remedy for this at the moment. With some creativity, though, quite a bit of Python code can be packed into a single line, or combinations of single-line inputs. If more than one line of input is needed in a single statement, you can write the code in a Python \lstinline{.py} file, and run the file using the \lstinline{file} option of \lstinline{python.ado} or \lstinline{import} it in an interactive session.
\item[3.] The Stata \textsc{GUI}'s Break button does not interrupt the plugin. There is not recourse for infinite loops in the plugin besides closing Stata.
\item[4.] The plugin does not have continuous access to user input. Python code requiring continuous control over \lstinline{stdin}, such as the \lstinline{input()} function, will not work.
\item[5.] Calling \lstinline{sys.exit()} in a Python file will close Stata. In the interactive interpreter, \lstinline{sys.exit()} may be safely used to exit the plugin only.
\end{enumerate}
\section{Installing} \label{installing}
The necessary files for this project, besides those that come with your Python installation, are
\begin{itemize}
\item \lstinline$stplugin.h$ (from \verb|http://www.stata.com/plugins/|)
\item \lstinline$stplugin.c$ (from \verb|http://www.stata.com/plugins/|)
\item \lstinline$python_plugin.c$
\item \lstinline$python.ado$
\item \lstinline$stata.py$
\item \lstinline$stata_missing.py$
\end{itemize}
\subsection{Windows, using Visual Studio Express}
Below are the steps I used for compiling the plugin using Visual Studio Express 2012 and Python version 3.3 on Windows 7. StataCorp has notes for compiling plugins for other versions of Visual Studio at \verb|http://www.stata.com/plugins/|
\begin{enumerate}
\setcounter{enumi}{-1}
\item You will need Stata, Python, and Visual Studio Express 2012 installed. You will also need the Stata plugin header file \lstinline$stplugin.h$ and C file \lstinline$stplugin.c$ from section 2 of \verb|http://www.stata.com/plugins/|.
\item Open Visual Studio. From the main menu at the top, select \textbf{File $>$ New Project}.
\item A window pops up. Under the menu on the left, expand the \textbf{Visual C++} item, then select \textbf{Win32}. In the center pane of the window choose \textbf{Win32 Project}. On the bottom, change the name and solution name, if desired, then click \textbf{OK}.
\item Another window pops up. Click on \textbf{Next}. On the next screen, under \textbf{Application type}, choose \textbf{DLL}. Below that, check the box for \textbf{empty project}. Click on \textbf{Finish}.
\item In the main application window, on the right hand side, find \textbf{Resource Files}. Right click, select \textbf{Add $>$ Existing Item}. Add each of these (you might have to right click and choose \textbf{Add $>$ Existing Item} multiple times):
\begin{enumerate}
\item \lstinline$python_plugin.c$
\item \lstinline$stplugin.h$
\item \lstinline$stplugin.c$
\item \lstinline$python33.lib$ (for me this resides in \verb|C:/Python33/libs|)
\end{enumerate}
\item Under \textbf{Resource Files}, click on \lstinline$python_plugin.c$ so that it's highlighted. In the main menu bar (at the top of the Visual Studio) select \textbf{VIEW $>$ Property Pages}.
A new window pops up. On the left, select \textbf{C/C++ $>$ General}. On the right, click in the field next to \textbf{Additional Include Directories}, and type in the directory for Python.h (for me it is \verb|C:/Python33/include|). Press enter or click on \textbf{OK}.
\item At the top, find \textbf{Debug} \emph{below} the main menu bar (not the \textbf{DEBUG} \emph{in} the main menu bar), and change this to \textbf{Release}. (Alternately, you could rename the Python file \lstinline$python33.lib$ to \lstinline$python33_d.lib$.)
You might have to repeat this and the previous step if you make other changes to settings or do these steps out of order.
\item If you have an x64 machine, change the field next to \textbf{Debug} from \textbf{Win32} to \textbf{x64}. This will require several steps. First, click on the field to open the menu, and choose \textbf{Configuration Manager...}. A new window pops up. Under platform, select \textbf{New...}, then select x64. Click on \textbf{OK}, then \textbf{Close}.
\item In the main menu bar select \textbf{BUILD $>$ Build Solution} or use the shortcut, F7. You should get a message in the Output window, below the main window, that says the project was successfully compiled.
\item Rename the compiled dll (if necessary) to \lstinline$python_plugin.plugin$.
Using the default settings, for me the compiled dll is found in
\verb|C:/Users/<my username>/My Documents/Visual Studio 2012/|\newline
\verb| Projects/<project name>/x64/Release|
(with \verb|<my username>| and \verb|<project name>| replaced).
Put \lstinline$python_plugin.plugin$ and \lstinline$python.ado$ in Stata's Ado path (in Stata use command \lstinline{adopath} to see the Ado path), and put \lstinline$stata.py$ and \lstinline$stata_missing.py$ in Python's path (in Python use \lstinline{import sys} then \lstinline{sys.path} to see directories in the path).
As an alternative to putting files in the Ado path and/or the Python path, you can put some or all of these files into a common directory and \lstinline{cd} to that directory in Stata before first calling the plugin. This works because the current working directory is always in the Ado path, and the directory in which the Python plugin was first called will be in the Python path.
\item Open Stata and type \lstinline$python$. If everything has worked, this should start an interactive session of Python within Stata. A horizontal line should appear, with text to indicate a Python interactive session has started, similar to when starting an interactive session of Mata. Try some of the examples from \S\ref{examples}. If error messages and results are not printed to the screen, check to make sure \lstinline$stata.py$ is somewhere that Python can find it.
\end{enumerate}
\subsection{Mac OS X}
{\small (thanks to Kit Baum for working on this)}\newline
The plugin was successfully installed on Mac OS X with the following steps. First, make sure that Python3.3 is installed. An OS X installer can be found at \lstinline{http://www.python.org/getit/}. After installing Python3.3, you might need change the definition of \lstinline{python} to point to the \lstinline{python3.3} executable. You can do this by renaming \lstinline{/usr/local/python} to \lstinline{/usr/local/python2.7} (assuming Python2.7 is the default version) and then adding a symlink from \lstinline{/usr/local/python} to \lstinline{/usr/local/bin/python3.3}.
You will also need \lstinline{gcc}. You can get \lstinline{gcc} with Xcode (\lstinline{https://developer.} \lstinline{apple.com/xcode/}), or ``Command Line Tools for Xcode'' (see, for example, \lstinline{http://www.mkyong.com/mac/how-to-install-gcc-compiler-on-mac-os-x/}).
Next, make sure \lstinline{python_plugin.c}, \lstinline{stplugin.c}, and \lstinline{stplugin.h} reside in the same directory. To compile the plugin, start with the compiler command from \lstinline{http://www.stata.com/plugins/}, modified for this plugin:
\begin{lstlisting}
gcc -bundle -DSYSTEM=APPLEMAC stplugin.c
python_plugin.c -o python_plugin.plugin
\end{lstlisting}
Add to that compiler and linker flags for Python, which can be obtained as in \newline
\lstinline{http://docs.python.org/3.3/extending/embedding.html#compiling-and-} \newline \lstinline{linking-under-unix-like-systems}.
After compiling, \lstinline{python_plugin.plugin} and \lstinline{python.ado} need to be put in Stata's Ado path and \lstinline{stata.py} and \lstinline{stata_missing.py} need to be put in the Python path. Alternately, any or all of these files can be in the directory from which the \lstinline{python} command is first invoked, because that directory should be in both the Ado path and Python path.
\section{Syntax of \lstinline$python.ado$} \label{syntax}
The syntax for \lstinline$python.ado$ is
\begin{lstlisting}
python [varlist] [if] [in] [, file(some_file.py)
args(some_args) ]
\end{lstlisting}
If no file is specified, an interactive session is begun. The number of arguments in the \lstinline{args} option is stored in Stata local \lstinline$_pynargs$, and the arguments are stored in \lstinline$_pyarg0$, \lstinline$_pyarg1$, etc. The number of variables in the varlist and their names are stored in Stata locals \lstinline$_pynvars$, and \lstinline$_pyvar0$, \lstinline$_pyvar1$, etc. (see example in \S\ref{file_example}).
If a file is specified and it is not specified with an asolute path, then the file is searched for in the \lstinline{adopath} (not the Python path). Thus you can keep Python files with related \lstinline{.ado} or \lstinline{.do} files by giving the Python file a similar name. To prevent searches along the \lstinline{adopath}, specify the absolute path the file.
There is one drawback to using \lstinline$python.ado$ rather than using the plugin directly. With \lstinline$python.ado$ the user will have access only to those locals defined within \lstinline$python.ado$ or within the Python session or script. Any locals defined interactively before starting an interactive session will be invisible if using \lstinline$python.ado$ to invoke the interactive session. See example in \S\ref{local_example}.
\section{Using the plugin directly} \label{using_plugin}
\subsection{Syntax}
Two ways of calling the plugin will be shown here, the minimal syntax, which is not generally recommended, and then the recommended syntax, which is more cumbersome. With either syntax, the plugin will need to be introduced to Stata with \lstinline{program python_plugin, plugin}.
Contrary to usual plugin usage, arguments for the plugin should not be included in the plugin call. Arguments can instead be put in locals and accessed through \lstinline{st_local} inside the plugin, as done in \lstinline{python.ado} (see \S\ref{syntax}).
The mininal syntax for calling the plugin is
{\small
\begin{lstlisting}
plugin call python_plugin [varlist] [, file_name ]
\end{lstlisting}
}
\noindent but this syntax should not be used if wanting to interact with Stata variables. In fact, because of reasons described below, the plugin has been written so that the varlist is seen to be empty when using the minimal syntax. If wanting to use the plugin to interact with Stata variables, the plugin should be called with
{\small
\begin{lstlisting}
ereturn clear
local _pynallvars = 0
if (c(k) > 0) {
foreach var of varlist * {
local _pyallvars`_pynallvars' = "`var'"
local _pynallvars = `_pynallvars' + 1
}
}
plugin call python_plugin `=cond(c(k) > 0, "*", "")' ///
[, file_name ]
\end{lstlisting}
}
With either version, the plugin runs the file if \lstinline{file_name} is given. Unlike with the \lstinline{python} command, a specified \lstinline{file_name} is not searched for. You must provide the path, or the file must be in the same directory. The Mata function \lstinline{findfile()} is useful for finding the path to a file in the \lstinline{adopath} (see \S\ref{file_example}).
If no \lstinline{file_name} is given, an interactive session is begun. With the recommended syntax above, variables of interest can be specified in locals, as is done in \lstinline{python.ado} (see \S\ref{syntax}).
The recommended way to call the plugin solves several problems:
\begin{enumerate}
\item Estimation commands can introduce ``hidden'' variables that are partially visible in the plugin and occupy a position in the varlist, but cannot be interacted with (as far as I know).
The purpose of the \lstinline{ereturn clear} is to clear any hidden variables.
\item If a subset of variables could be specified, indexing of variables can be inconsistent between functions in the plugin. Some functions index relative to the specified set, and some index relative to the entire varlist (including hidden variables). Clearing hidden variables with \lstinline{ereturn clear} and using \lstinline{`=cond(c(k) > 0, "*", "")'} in the plugin call help to ensure that the indexing is consistent.
The minimal, non-recommeded syntax above implies that a subset of variables can be specified. In fact, while the syntax is allowed, the plugin tries to disallow actually using subsets because of the problems discussed here.
\item The remainder of the extra lines in the second version provide the variable names to the plugin so that \lstinline{st_varname} can look up names by index and \lstinline{st_varindex} can return the index for a name. Supplying the variable names in this way also allows the C code to be written so that if the simpler, not-recommended syntax is used, the user is presented with an empty varlist rather than inconsistent indexing and hidden variables.
\end{enumerate}
\section{The \lstinline$stata_missing$ module} \label{stata_missing}
The purpose of the \lstinline$stata_missing$ module is to implement an analog of Stata's missing values. This is accomplished with the class \lstinline$MissingValue$. The module contains analogs of the 27 usual missing values, \lstinline$.$, \lstinline$.a$, \lstinline$.b$, etc., in a tuple called \lstinline$MISSING_VALS$. Stata supports missing values other than these, but the \lstinline$stata_missing$ module does not. The analog of Stata's \lstinline$.$ missing value, \lstinline$MISSING_VALS[0]$, is also given the name \lstinline$MISSING$ within the \lstinline$stata_missing$ module.
Users wanting direct access to analogs of Stata's missing values should use the existing instances of \lstinline$MissingValue$ rather than construct new instances. Users wanting to determine which instance of \lstinline$MissingValue$ corresponds to a large floating point number should use the function \lstinline$getMissing$, which takes a single \lstinline{float} or \lstinline{int} argument and returns an instance of \lstinline{MissingValue}.
Any plugin function that sets Stata numeric values can accept a \lstinline{float}, \lstinline{int}, \lstinline$None$, or an instance of \lstinline{MissingValue}. In such cases, \lstinline$None$ will translated into Stata's \lstinline$.$ missing value.
Example usage of the \lstinline{stata_missing} module is given in \S\ref{missing_value_example}, \S\ref{st_view_example}, and \S\ref{st_matrix_example}.
\section{The \lstinline$stata$ module} \label{stata_module}
The \lstinline$stata$ module provides functions for interacting with Stata variables, matrices, macros, and numeric scalars. The module is automatically imported with the first use of the plugin, just as if the user had typed \lstinline$from stata import *$. The module is imported whether the plugin was invoked directly (``\lstinline$plugin call$ ...'') or through \lstinline$python.ado$, and for both the interactive interpreter and running files.
Almost all of the functionality provided by the \lstinline$stata$ module is mirrored by functionality in Mata (but not vice versa). In an effort to be easier to use, the functions in the \lstinline$stata$ module were made to mimic functions in Mata. For example, in Mata the function \lstinline$st_local$ retrieves the value of a local macro if given one argument (its name) or sets the value of the macro if given two arguments (name and value). In the \lstinline$stata$ module there is a function \lstinline$st_local$ with the same behavior. Of course, some changes had to be made between Mata and Python versions of functions. In Mata, when the user tries to access a non-existent numeric scalar, an empty matrix \lstinline$J(0,0)$ is returned. Python has no inherent notion of a matrix, so instead the Python function raises a \lstinline{ValueError}.
Many of the functions in the \lstinline$stata$ module have indexing arguments. Keep in mind that while Stata uses 1-based indexing (i.e., the first meaningful index is 1 for data variables, observations, and matrix elements), the Python convention is to begin indexing at 0. Thus, if the first variable in a dataset is numeric, its first observation can be obtained via either
\begin{quote}
\lstinline$_st_data(0, 0)$
\end{quote}
or
\begin{quote}
\lstinline$st_data(0, 0)$,
\end{quote}
and its value can be changed via
\begin{quote}
\lstinline$_st_store(0, 0, some_val)$
\end{quote}
or
\begin{quote}
\lstinline$st_store(0, 0, some_val)$.
\end{quote}
\subsection{List of functions} \label{stata_func_list}
\begin{multicols}{3}
\setcounter{finalcolumnbadness}{0}
\lstinline$st_cols$
\lstinline$_st_data$
\lstinline$st_data$
{\color{gray}\lstinline$_st_display$}
{\color{gray}\lstinline$_st_error$}
\lstinline$st_format$
\lstinline$st_global$
\lstinline$st_ifobs$
\lstinline$st_in1$
\lstinline$st_in2$
\lstinline$st_isfmt$
\lstinline$st_islmname$
\lstinline$st_ismissing$
\lstinline$st_isname$
\lstinline$st_isnumfmt$
\lstinline$st_isnumvar$
\lstinline$st_isstrfmt$
\lstinline$st_isstrvar$
\lstinline$st_isvarname$
\lstinline$st_local$
\lstinline$st_matrix$
\lstinline$st_matrix_el$
\lstinline$st_mirror$
\lstinline$st_nobs$
\lstinline$st_numscalar$
\lstinline$st_nvar$
\lstinline$st_rows$
\lstinline$_st_sdata$
\lstinline$st_sdata$
\lstinline$_st_sstore$
\lstinline$st_sstore$
\lstinline$_st_store$
\lstinline$st_store$
\lstinline$st_varindex$
\lstinline$st_varname$
\lstinline$st_view$
\lstinline$st_viewobs$
\lstinline$st_viewvars$
\end{multicols}
\subsection{Function descriptions} \label{func_descript}
\ \newline
\noindent \lstinline$st_cols(matname)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{matname} & str \\
returns: & \multicolumn{2}{l}{int}
\end{tabular}
\vspace{1.5mm}
\noindent Get number of columns in given matrix. Returns 0 if there is no Stata matrix with name \lstinline$matname$. \newline
\ \newline
\noindent \lstinline$_st_data(obsnum, varnum)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnum} & int \\
& \texttt{varnum} & int \\
returns: & \multicolumn{2}{l}{float or \lstinline$MissingValue$}
\end{tabular}
\vspace{1.5mm}
\noindent Get value in given observation and Stata numeric variable. The allowed argument values are
\[
-\texttt{st\_nobs()} \leq \texttt{obsnum} < \texttt{st\_nobs()}
\]
and
\[
-\texttt{st\_nvar()} \leq \texttt{varnum} < \texttt{st\_nvar()}
\]
(assuming plugin is called through \lstinline$python.ado$ or used in accordance with recommendations made in \S\ref{using_plugin}). Negative values are interpreted in the usual way for Python indices. Values outside of these ranges will cause an \lstinline$IndexError$. Note this last detail is unlike in Mata, where \lstinline{_st_data()} does not abort with error for invalid indices, but instead returns a \lstinline{.} missing value.
\newline
\ \newline
\noindent \lstinline$st_data(obsnums, vars)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnums} & single int or iterable of int \\
& \texttt{vars} & single int, single str, or iterable of int or str \\
returns: & \multicolumn{2}{l}{list of lists of float or \lstinline$MissingValue$}
\end{tabular}
\vspace{1.5mm}
\noindent Get values in given observations and given nuemric Stata variables. The function returns a list of lists, with one sub-list for each observation. See \S\ref{data_and_store_example} for example usage and return values.
This function uses \lstinline{_st_data()}, so \lstinline{obsnums} and \lstinline{var} (if integer) can be negative and if out of range will raise an \lstinline{IndexError}. If strings are used in \lstinline{vars}, a \lstinline{ValueError} will be raised for ambiguous or incorrect abbreviations. \newline
\ \newline
\noindent \lstinline$_st_display(text)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{text} & str \\
returns: & \multicolumn{2}{l}{\texttt{None}}
\end{tabular}
\vspace{1.5mm}
\noindent Print text in Stata's results window, with included \textsc{SMCL} tags interpreted. The usual \texttt{print} function is routed through \lstinline$_st_display$, so there's usually no need to call \lstinline$_st_display$ directly. Unlike most other functions listed here, this function is not autmatically imported into the main namespace. To use it, first import it with \lstinline{from stata import _st_display}. \newline
\ \newline
\noindent \lstinline$_st_error(text)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{text} & str \\
returns: & \multicolumn{2}{l}{\texttt{None}}
\end{tabular}
\vspace{1.5mm}
\noindent Print text as error. There's usually no need to call this function directly. Python errors are automatically routed through \lstinline{_st_error}, and if wanting to display a message as an error, the user can simply use \lstinline$print("{err}<message>")$. Like \lstinline{_st_display}, this function is not automatically imported into the main namespace. To use it, first import it with \lstinline{from stata import _st_error}. \newline
\ \newline
\noindent \lstinline$st_format(fmt, value)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{fmt} & str \\
& \texttt{value} & int, float, \lstinline$MissingValue$, or \lstinline$None$ \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Return string representation of \lstinline{value} according to Stata format given in \lstinline{fmt}. The first argument should be a valid Stata format, but the function will return a meaningful string regardless. \newline
\ \newline
\noindent \lstinline$st_global(macroname)$ \\
\noindent \lstinline$st_global(macroname, value)$
\vspace{1.5mm}
\noindent
\indent with 1 argument:
\indent \qquad \begin{tabular}{rrl}
arguments: & \texttt{macroname} & str \\
returns: & \multicolumn{2}{l}{str}
\end{tabular}
\vspace{1.5mm}
\noindent
\indent with 2 arguments:
\indent \qquad \begin{tabular}{rrl}
arguments: & \texttt{macroname} & str \\
& \texttt{value} & str \\
returns: & \multicolumn{2}{l}{None}
\end{tabular}
\vspace{1.5mm}
\noindent Get value from given global macro if using 1-argument version, or set the value of the global macro if using the 2-argument version. In the 1-argument version, if the global macro does not exist the return value will be the empty string. In either version, if the global macro name is malformed a \lstinline{ValueError} will be raised.
Unlike Mata's \lstinline{st_global}, the \lstinline{st_global} here cannot access characteristics and cannot access \lstinline{r()}, \lstinline{e()}, \lstinline{s()}, and \lstinline{c()} macros. \newline
\ \newline
\noindent \lstinline$st_ifobs(obsnum)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnum} & int \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Query the \texttt{if} condition (specified when invoking \lstinline{python} or the plugin) for the given observation number. If no \texttt{if} condition was specified, this will evaluate to \texttt{True} for all observations. The allowed values for \lstinline{obsnum} are
\[
-\texttt{st\_nobs()} \leq \texttt{obsnum} < \texttt{st\_nobs()}
\]
with negative values interpreted in the usual way. Values outside this range will cause an \lstinline{IndexError}.\newline
\ \newline
\noindent \lstinline$st_in1()$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rl}
returns: & int
\end{tabular}
\vspace{1.5mm}
\noindent Get the first index in the \texttt{in} range (specified when invoking \lstinline{python} or the plugin). If no \texttt{in} was specified, this will evaluate to \texttt{0}. \newline
\ \newline
\noindent \lstinline$st_in2()$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rl}
returns: & int
\end{tabular}
\vspace{1.5mm}
\noindent Get the second index of the \texttt{in} range (specified when invoking \lstinline{python} or the plugin), plus one. If no \texttt{in} condition was specified, this will return the maximum observation index, plus one. The reason for returning the first index {\em beyond} the \texttt{in} range, rather that {\em last index within}, is to facilitate the common Python syntax of index slicing. For example, if Python variable \texttt{v} is an instance of \lstinline{st_view} (see below), then \lstinline$v[st_in1():st_in2(), ]$ would be a reference to the observations within the \texttt{in} condition. (See \S\ref{examples} for other examples of slice indexing.) \newline
\ \newline
\noindent \lstinline$st_isfmt(fmt)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{fmt} & str \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Determine if given \lstinline{fmt} is a valid Stata format. In calendar formats, the calendar name is not checked for validity. \newline
\ \newline
\noindent \lstinline$st_islmname(name)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{name} & str \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Determine if given \lstinline{name} is a valid local macro name. \newline
\ \newline
\noindent \lstinline$st_ismissing(value)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{value} & any Python object \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Determine if the given value is considered a missing value. The function returns \lstinline{False} if the value is not a \lstinline{float}, \lstinline{int}, \lstinline{MissingValue} instance or \lstinline{None}. It returns \lstinline{True} if \lstinline{value} is \lstinline{None} or a \lstinline{MissingValue} instance. If \lstinline{value} is \lstinline{float}, the function tests whether the value is inside the non-missing range for doubles in Stata, which is approximately $[-1.798 \times 10^{308},\ 8.988 \times 10^{307}]$ (see \lstinline{help dta} in Stata, specifically, ``Representation of numbers''), returning \lstinline{True} if it's outside this range, \lstinline{False} if it's inside this range. \newline
\ \newline
\noindent \lstinline$st_isname(name)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{name} & str \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Determine if given \lstinline{name} is a valid name, for example, for scalars or global macros. To test for validity as a local macro name use \lstinline$st_islmname$. To test for validity as a Stata variable name use \lstinline$st_isvarname$. \newline
\ \newline
\noindent \lstinline$st_isnumfmt(fmt)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{fmt} & str \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Determine if given \lstinline{fmt} is a valid Stata numeric format. Numeric formats are any valid formats that are not string formats. \newline
\ \newline
\noindent \lstinline$st_isnumvar(var)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{var} & int or str \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Determine if given Stata variable is a numeric variable. The variable can be specified by its integer index, by its name, or by abbreviation of its name. If \lstinline{var} is an integer, then it should be in the range
\[
-\texttt{st\_nvar()} \leq \texttt{var} < \texttt{st\_nvar()}
\]
with negative values interpreted in the usual way. Values outside this range will cause an \lstinline{IndexError}. If \lstinline{var} is a string, an invalid or ambiguous abbreviation will cause a \lstinline{ValueError}. \newline
\ \newline
\noindent \lstinline$st_isstrfmt(fmt)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{fmt} & str \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Determine if given \lstinline{fmt} is a valid Stata string format. \newline
\ \newline
\noindent \lstinline$st_isstrvar(var)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{var} & int or str \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Check whether given Stata variable is a string variable. The variable can be specified by its integer index, by its name, or by abbreviation of its name. If \lstinline{var} is an integer, then it should be in the range
\[
-\texttt{st\_nvar()} \leq \texttt{var} < \texttt{st\_nvar()}
\]
with negative values interpreted in the usual way. Values outside this range will cause an \lstinline{IndexError}. If \lstinline{var} is a string, an invalid or ambiguous abbreviation will cause a \lstinline{ValueError}. \newline
\ \newline
\noindent \lstinline$st_isvarname(name)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{name} & str \\
returns: & \multicolumn{2}{l}{bool}
\end{tabular}
\vspace{1.5mm}
\noindent Determine if given \lstinline{name} is a valid Stata variable name. See manual [U] \S11.3 Naming conventions. \newline
\ \newline
\noindent \lstinline$st_local(macroname)$ \\
\noindent \lstinline$st_local(macroname, value)$
\vspace{1.5mm}
\noindent
\indent with 1 argument:
\indent \qquad \begin{tabular}{rrl}
arguments: & \texttt{macroname} & str \\
returns: & \multicolumn{2}{l}{str}
\end{tabular}
\vspace{1.5mm}
\noindent
\indent with 2 arguments:
\indent \qquad \begin{tabular}{rrl}
arguments: & \texttt{macroname} & str \\
& \texttt{value} & str \\
returns: & \multicolumn{2}{l}{None}
\end{tabular}
\vspace{1.5mm}
\noindent Get value from given local macro if using 1-argument version, or set the value of the local macro if using the 2-argument version. In the 1-argument version, if the local macro does not exist the return value will be the empty string. In either version, if the local name is malformed a \lstinline{ValueError} will be raised. \newline
\ \newline
\noindent \lstinline$st_matrix(matname)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{matname} & str \\
returns: & \multicolumn{2}{l}{instance of \lstinline$StataMatrix$ class}
\end{tabular}
\vspace{1.5mm}
\noindent This function creates a \emph{view} onto a Stata matrix. See \S\ref{st_matrix_example} for example usage. If no matrix is found with name \lstinline{matname}, a \lstinline{ValueError} is raised.
Unlike Mata's \lstinline{st_matrix}, the object returned by the \lstinline{st_matrix} here is only a view on the Stata matrix. If you change elements of the Python object you are actually changing the matrix in Stata. Also unlike Mata's \lstinline{st_matrix}, the \lstinline{st_matrix} here cannot access \lstinline{r()} and \lstinline{e()} matrices. \newline
\ \newline
\noindent \lstinline$st_matrix_el(matname, row, col)$ \\
\noindent \lstinline$st_matrix_el(matname, row, col, value)$
\vspace{1.5mm}
\noindent
\indent with 3 arguments:
\indent \qquad \begin{tabular}{rrl}
arguments: & \texttt{matname} & str \\
& \texttt{row} & int \\
& \texttt{col} & int \\
returns: & \multicolumn{2}{l}{float or \lstinline$MissingValue$}
\end{tabular}
\vspace{1.5mm}
\indent with 4 arguments:
\indent \qquad \begin{tabular}{rrl}
arguments: & \texttt{matname} & str \\
& \texttt{row} & int \\
& \texttt{col} & int \\
& \texttt{value} & int, float, \lstinline$MissingValue$, or \texttt{None} \\
returns: & \multicolumn{2}{l}{\texttt{None}}
\end{tabular}
\vspace{1.5mm}
\noindent Get the value in the given matrix, row, and column if using the 3-argument version, or replace the value if using the 4-argument version. If no matrix is found with name \lstinline{matname}, a \lstinline{ValueError} is raised. \newline
\ \newline
\noindent \lstinline$st_mirror()$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
returns: & \multicolumn{2}{l}{instance of \lstinline$StataMirror$ class}
\end{tabular}
\vspace{1.5mm}
\noindent This function creates a view onto the current Stata data set. Unlike \lstinline{st_view}, the object returned by \lstinline{st_mirror} is `aware' of changes made in Stata. However, the main advantage of \lstinline{st_mirror} is that the returned object provides quick access to Stata variables as attributes. See \S\ref{st_mirror_example} for example usage. See \S\ref{after_changes_example} for a comparison of \lstinline{st_mirror} and \lstinline{st_view}. \newline
\ \newline
\noindent \lstinline$st_nobs()$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rl}
returns: & int
\end{tabular}
\vspace{1.5mm}
\noindent Get the number of observations in the current Stata data set. \newline
\ \newline
\noindent \lstinline$st_numscalar(scalarname)$ \\
\noindent \lstinline$st_numscalar(scalarname, value)$
\vspace{1.5mm}
\noindent
\indent with 1 argument:
\indent \qquad \begin{tabular}{rrl}
arguments: & \texttt{scalarname} & str \\
returns: & \multicolumn{2}{l}{float or \lstinline$MissingValue$}
\end{tabular}
\vspace{1.5mm}
\noindent
\indent with 2 arguments:
\indent \qquad \begin{tabular}{rrl}
arguments: & \texttt{scalarname} & str \\
& \texttt{value} & int, float, \lstinline$MissingValue$, or \texttt{None} \\
returns: & \multicolumn{2}{l}{\texttt{None}}
\end{tabular}
\vspace{1.5mm}
\noindent Get contents of given numeric scalar if using 1-argument version, or set the contents if using 2-argument version. In the 1-argument version, if the scalar does not exist a \lstinline{ValueError} will be raised. Note this is unlike Mata, where \lstinline{st_numscalar} returns \lstinline{J(0,0,.)} if the scalar does not exist. In both 1-argument and 2-argument versions, if the scalar name is malformed a \lstinline{ValueError} will be raised.
Unlike Mata's \lstinline{st_numscalar}, this \lstinline{st_numscalar} cannot access \lstinline{r()}, \lstinline{e()}, and \lstinline{c()} numeric scalars. \newline
\ \newline
\noindent \lstinline$st_nvar()$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rl}
returns: & int
\end{tabular}
\vspace{1.5mm}
\noindent Get the number of Stata variables in the current data set. \newline
\ \newline
\noindent \lstinline$st_rows(matname)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{matname} & str \\
returns: & \multicolumn{2}{l}{int}
\end{tabular}
\vspace{1.5mm}
\noindent Get the number of rows in given matrix. Returns 0 if there is no Stata matrix with name \lstinline$matname$. \newline
\ \newline
\noindent \lstinline$_st_sdata(obsnum, varnum)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnum} & int \\
& \texttt{varnum} & int \\
returns: & \multicolumn{2}{l}{str}
\end{tabular}
\vspace{1.5mm}
\noindent Get value in given Stata string variable and observation. The allowed argument values are
\[
-\texttt{st\_nobs()} \leq \texttt{obsnum} < \texttt{st\_nobs()}
\]
and
\[
-\texttt{st\_nvar()} \leq \texttt{varnum} < \texttt{st\_nvar()}
\]
(assuming plugin is called through \lstinline$python.ado$ or used in accordance with recommendations made in \S\ref{using_plugin}). Negative values are interpreted in the usual way for Python indices. Values outside of these ranges will cause an \lstinline$IndexError$. Note this is unlike in Mata, where \lstinline{_st_sdata()} does not abort with error for invalid indices, but instead returns an empty string. \newline
\ \newline
\noindent \lstinline$st_sdata(obsnums, vars)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnums} & single int or iterable of int \\
& \texttt{vars} & single int, single str, or iterable of int or str \\
returns: & \multicolumn{2}{l}{list of lists of str}
\end{tabular}
\vspace{1.5mm}
\noindent Get values in given observations and given Stata string variables. The function returns a list of lists, with one sub-list for each observation. See \S\ref{data_and_store_example} for example usage and return values.
This function uses \lstinline{_st_sdata()}, so \lstinline{obsnums} and \lstinline{var} (if integer) can be negative and if out of range will raise an \lstinline{IndexError}. If strings are used in \lstinline{vars}, a \lstinline{ValueError} will be raised for ambiguous or incorrect abbreviations. \newline
\ \newline
\noindent \lstinline$_st_sstore(obsnum, varnum, value)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnum} & int \\
& \texttt{varnum} & int \\
& \texttt{value} & str \\
returns: & \multicolumn{2}{l}{\texttt{None}}
\end{tabular}
\vspace{1.5mm}
\noindent Set value in given Stata string variable in given observation. The allowed argument values are
\[
-\texttt{st\_nobs()} \leq \texttt{obsnum} < \texttt{st\_nobs()}
\]
and
\[
-\texttt{st\_nvar()} \leq \texttt{varnum} < \texttt{st\_nvar()}
\]
(assuming plugin is called through \lstinline$python.ado$ or used in accordance with recommendations made in \S\ref{using_plugin}). Negative values are interpreted in the usual way for Python indices. Values outside of these ranges will cause an \lstinline$IndexError$. Note this is unlike in Mata, where \lstinline{_st_sstore()} does not abort with error for invalid indices. \newline
\ \newline
\noindent \lstinline$st_sstore(obsnums, vars, values)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnums} & single int or iterable of int \\
& \texttt{vars} & single int, single string, or iterable of int or str \\
& \texttt{values} & iterable of str \\
returns: & \multicolumn{2}{l}{None}
\end{tabular}
\vspace{1.5mm}
\noindent Set values in given observations and given Stata string variables. The dimensions of the input \lstinline{values} should match the dimensions implied by \lstinline{obsnums} and \lstinline{vars}. For example, if \lstinline{obsnums} is \lstinline{(0,1,2)} and \lstinline{vars} is \lstinline{(2,4)} (and if those are valid for the loaded data set), then any of these input \lstinline{values} would be valid:
\begin{align*}
\texttt{values} &= \texttt{[['a','b'], ['c','d'], ['e','f']]} \\
\texttt{values} &= \texttt{(('a','b'), ('c','d'), ('e','f'))} \\
\texttt{values} &= \texttt{(['a','b'], ['c','d'], ['e','f'])} \\
\texttt{values} &= \texttt{[['a']*2]*3}
\end{align*}
and these would be invalid:
\begin{align*}
\texttt{values} &= \texttt{[['a','b','c'], ['d','e','f']]} \\
\texttt{values} &= \texttt{(('a','b','c','d','e','f'))} \\
\texttt{values} &= \texttt{('a','b','c','d','e','f')}
\end{align*}
See \S\ref{data_and_store_example} for other examples.
This function uses \lstinline{_st_sstore()}, so \lstinline{obsnums} and \lstinline{var} (if integer) can be negative and if out of range will raise an \lstinline{IndexError}. If there is an invalid index, some values may be set before the \lstinline{IndexError} is raised. If strings are used in \lstinline{vars}, a \lstinline{ValueError} will be raised for ambiguous or incorrect abbreviations. \newline
\ \newline
\noindent \lstinline$_st_store(obsnum, varnum, value)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnum} & int \\
& \texttt{varnum} & int \\
& \texttt{value} & int, float, \lstinline$MissingValue$, or \texttt{None} \\
returns: & \multicolumn{2}{l}{\texttt{None}}
\end{tabular}
\vspace{1.5mm}
\noindent Set value in given Stata numeric variable in given observation. The allowed argument values are
\[
-\texttt{st\_nobs()} \leq \texttt{obsnum} < \texttt{st\_nobs()}
\]
and
\[
-\texttt{st\_nvar()} \leq \texttt{varnum} < \texttt{st\_nvar()}
\]
(assuming plugin is called through \lstinline$python.ado$ or used in accordance with recommendations made in \S\ref{using_plugin}). Negative values are interpreted in the usual way for Python indices. Values outside of these ranges will cause an \lstinline$IndexError$. Note this is unlike in Mata, where \lstinline{_st_store()} does not abort with error for invalid indices. \newline
\ \newline
\noindent \lstinline$st_store(obsnums, vars, values)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnums} & single int or iterable of int \\
& \texttt{vars} & single int, single string, or iterable of int or str \\
& \texttt{values} & iterable of int, float, \lstinline$MissingValue$, or None \\
returns: & \multicolumn{2}{l}{None}
\end{tabular}
\vspace{1.5mm}
\noindent Set values in given observations and given Stata numeric variables. The dimensions of the input \lstinline{values} should match the dimensions implied by \lstinline{obsnums} and \lstinline{var}. For example, if \lstinline{obsnums} is \lstinline{(0,1,2)} and \lstinline{vars} is \lstinline{(2,4)} (and if those are valid for the loaded data set), then any of these input \lstinline{values} would be valid:
\begin{align*}
\texttt{values} &= \texttt{[[0,1], [2,3], [4,5]]} \\
\texttt{values} &= \texttt{((0,1), (2,3), (4,5))} \\
\texttt{values} &= \texttt{([0,1], [2,3], [4,5])} \\
\texttt{values} &= \texttt{[[0]*2]*3}
\end{align*}
and these would be invalid:
\begin{align*}
\texttt{values} &= \texttt{[[0,1,2], [3,4,5]]} \\
\texttt{values} &= \texttt{((0,1,2,3,4,5))} \\
\texttt{values} &= \texttt{(0,1,2,3,4,5)}
\end{align*}
See \S\ref{data_and_store_example} for other examples.
This function uses \lstinline{_st_store()}, so \lstinline{obsnums} and \lstinline{var} (if integer) can be negative and if out of range will raise an \lstinline{IndexError}. If there is an invalid index, some values may be set before the \lstinline{IndexError} is raised. If strings are used in \lstinline{vars}, a \lstinline{ValueError} will be raised for ambiguous or incorrect abbreviations. \newline
\ \newline
\noindent \lstinline$st_varindex(varname)$ \\
\noindent \lstinline$st_varindex(varname, abbr_ok)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{varname} & str \\
& \texttt{abbr\_ok} & bool or coercible to bool \\
returns: & \multicolumn{2}{l}{int}
\end{tabular}
\vspace{1.5mm}
\noindent Find the index of the given Stata variable. Abbreviations are allowed if using the two-argument version and the second argument is truthy. Otherwise, \lstinline{varname} must match a Stata variable name exactly. A \lstinline$ValueError$ will be raised if the text does not match a Stata variable or if the abbreviation is ambiguous. Unlike Mata's \lstinline{st_varindex}, this \lstinline{st_varindex} only allows a single name or abbreviation per call. \newline
\ \newline
\noindent \lstinline$st_varname(varnum)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{varnum} & int \\
returns: & \multicolumn{2}{l}{str}
\end{tabular}
\vspace{1.5mm}
\noindent Return the name of the Stata variable at the given index. The allowed argument values are
\[
-\texttt{st\_nvar()} \leq \texttt{varnum} < \texttt{st\_nvar()}
\]
(assuming plugin is called through \lstinline$python.ado$ or used in accordance with recommendations made in \S\ref{using_plugin}). Negative values are interpreted in the usual way for Python indices. A value outside of this ranges will cause an \lstinline$IndexError$. \newline
\ \newline
\noindent \lstinline$st_view(rownums, varnums, selectvar)$
\vspace{1.5mm}
\noindent
\indent \begin{tabular}{rrl}
arguments: & \texttt{obsnums} & single int or iterable of int \\
& \texttt{varnums} & single int or iterable of int \\
& \texttt{selectvar} & string, int, \lstinline$None$, or \lstinline$MissingValue$ instance \\
returns: & \multicolumn{2}{l}{instance of \lstinline$StataView$ class}
\end{tabular}