use Stata variable labels in results - stata

I have several numeric variables var_1-var_5 with variable labels attached, like "Rain" and "Snow".
Can any mainstream commands use my variable labels when printing to the results window?
For example, it would be really nice if summarize did so. So far, the labels seem to be useful only for graphics and the describe command.

It seems the answer is: if the command can do it, then it will be documented or done by default.
From http://www.stata.com/statalist/archive/2011-09/msg00902.html (2011), I quote Nick Cox:
... if a command has no option to show variable labels, then you can't
show variable labels with that command; it is usually the case that
there is no such option with statistical commands, because typically
there wouldn't be enough space to show variable labels; and if there
is such an option, then it will be documented. The only alternative is
that you learn how to program in Stata and write your own alternative
commands.
Some user-written commands that maybe do what you want (with respect to summarize) are:
ssc describe labsumm
and
ssc describe fsum
These are from http://www.stata.com/statalist/archive/2008-07/msg00850.html.

Related

How to compute p-value when number of degrees of freedom and chi-square value are known?

I know how to do this in R: e.g. pchisq(18.98, df=2, lower.tail=FALSE)
However, I've no idea about how to write Stata code to solve this problem.
In case you're interested in more general post-estimation tests check out: help test.
Otherwise is seems like chi2(2,18.98) or chi2tail(2,18.98) are what you're after (depending on what lower.tail=FALSE means.
Note that in Stata you'll probably want to put this into a "local" in order to do other things with the output.
For example if you say the following to Stata:
local pchi2 = chi2(2,18.98)
display "chi2: `pchi2'"
Stata should reply:
chi2: .9999243958967154
See for more detail and links to the Stata manual section on statistical functions:
help chi2
help chi2tail

Include begin{document}, end{document} in LaTeX tables code generated in Stata

While using estpost/esttab/esttab in Stata to generate LaTeX tables, latex initialization syntax such as documentclass{}, begin{document}, and end{document} are never included. This means that every LaTeX code generated needs for these to be added.
I have many many tables to create. Is it possible to include these through Stata itself?
There are two potential solutions, the first is to include these using the prehead and postfoot options, which allow you to do this directly, but make table formatting a bit more difficult. Or there is the option to simply use include{asdf.tex} in another file.
Solution 1 example:
sysuse auto, clear
reg price mpg
esttab using "temp.tex", ///
prehead("\documentclass{article}\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{document}\begin{tabular}{l*{1}{c}}") ///
postfoot("\end{tabular}\end{document}") ///
replace
This will make a basic table, but doing things like including a title become more difficult with this option.
Second solution, in a tex file, you can include any number of tables thusly:
\documentclass{article}
\begin{document}
\input{any_tex_file.tex}
\input{any_tex_file2.tex}
\end{document}
and in this way you can include all of your tables.
Hope this helps
A better solution could be including this command into your stata esttab output commands
booktabs page(column)

Output to Table instead of Graph in Stata

In Stata, is there a way to redirect the data that a command does into a table instead of a graph?
Example: if someone created a normal probability distribution of data with the pnorm var_name command, is there a way to redirect the data so that instead of appearing in a graph, it appears in a table?
To add to #Noobie's answer:
Different commands work in different ways. There's no better short summary.
What you can look out for includes
generate() options that produce new variables. (There is absolute rule that the options have this name, but that or a similar name is the most common single variety.)
Options that allow saving results to new datasets.
Saved results, especially those visible after return list or ereturn list. These can be quite elaborate, e.g. saving of matrices of counts after tabulate.
More broadly, Stata commands aren't functions! One characteristic of a function, as so named in many languages or programs, is that there is a result, with special cases where the result is void or null. There clearly are statistical programs which in broad terms hinge on calling functions which have results, and what you see displayed is often a side-effect of that. Stata commands don't work like that in the sense that the results of a program can be various. In the case of commands designed just to show something, the "result" may be a display. It's worth noting that Mata, which underlies and underpins Stata, is more recognisably a C-like language, with (e.g.) many matrix extensions, which is based on functions (and much else).
Yes and no. It really depends on the command you are using. You should look at the help files first.
For instance, pnorm does not allow that. You can create the data yourself using the formula for pnorm described in the help file, where the cumulative distribution at some point is plotted against the so-called plotting position.
Other Stata commands allow you to generate the points directly. This is the case for kdensity for instance.

Stata : generate/replace alternatives?

I use Stata since several years now, along with other languages like R.
Stata is great, but there is one thing that annoys me : the generate/replace behaviour, and especially the "... already defined" error.
It means that if we want to run a piece of code twice, if this piece of code contains the definition of a variable, this definition needs 2 lines :
capture drop foo
generate foo = ...
While it takes just one line in other languages such as R.
So is there another way to define variables that combines "generate" and "replace" in one command ?
I am unaware of any way to do this directly. Further, as #Roberto's comment implies, there are reasons simply issuing a generate command will not overwrite (see: replace) the contents of a variable.
To be able to do this while maintaining data integrity, you would need to issue two separate commands as your question points out (explicitly dropping the existing variable before generating the new one) - I see this as method in which Stata forces the user to be clear about his/her intentions.
It might be noted that Stata is not alone in this regard. SQL Server, for example, requires the user drop an existing table before creating a table with the same name (in the same database), does not allow multiple columns with the same name in a table, etc. and all for good reason.
However, if you are really set on being able to issue a one-liner in Stata to do what you desire, you could write a very simple program. The following should get you started:
program mkvar
version 13
syntax anything=exp [if] [in]
capture confirm variable `anything'
if !_rc {
drop `anything'
}
generate `anything' `exp' `if' `in'
end
You then would naturally save the program to mkvar.ado in a directory that Stata would find (i.e., C:\ado\personal\ on Windows. If you are unsure, type sysdir), and call it using:
mkvar newvar=expression [if] [in]
Now, I haven't tested the above code much so you may have to do a bit of de-bugging, but it has worked fine in the examples I've tried.
On a closing note, I'd advise you to exercise caution when doing this - certainly you will want to be vigilant with regard to altering your data, retain a copy of your raw data while a do file manipulates the data in memory, etc.

How to execute multiple-line selection in do-file editor of Stata?

Does anyone know how to use the "execute selection" function in the do-file editor of Stata for code that spans multiple lines?
Currently I can't find a way to do this without using the #delimit ; system which requires repeating "delimit ;" at the beginning of every block I want to run.
Any suggestions appreciated!
I believe that you might be understanding the #delimit ; command wrongly: this is useful when you are coding a do-file to execute it in its entirety afterwards. I also assume that you are using Stata 11, since previous versions behave differently (if I recall well, Stata 10 SE for Mac does not support // comments and delimiting, for example).
If you are executing only a fraction of the code, use /// at the end of a line to continue its command on the next one.
Basic example (that will clear any open data, so beware):
sysuse lifeexp, clear
sc lexp safewater, ///
mlab(country);
This should run flawlessly even if you execute the sysuse command and the sc (scatter) commands separately. The sc command has the mlab option (to add labels to the data points) on a different line, but both lines will be interpreted as only one command due to the /// indication.
Hope this helps!