Tips for using Stata
This document describes some tips to enhance your efficient use of Stata. We will keep adding tips to the top of our home page to encourage you to visit it each month! We will move the monthly tips to the bottom of this page when we place new tips on our home page.
Our bookshop has several publications to assist in learning Stata data management and analyses.
Options
One of the strengths of Stata is the system of options, typed after a comma. If you
list
your data you see the value labels of the variables. If you add the
nolabel
option after a comma you will see the underlying code values.
list
var1 var7-var10 var3 if gender==1
To see the underlying value codes rather than labels, add the
nolabel
option after a comma:
list var1 var7-var10 var3 if gender==1,
nolabel
Note that you can list the variables in
any order that you define.
BTW, the comma is a toggle. If used a second time it turns off the options. We
could have writen the above command as:
list var1 var7-var10 var3, nolabel,
if gender==1
Edit and browse
You know that you can use the Editor button to invoke a spreadsheet format for
entering or changing data. However, if you type the command edit
you can limit what you see to a few variables, in any order that you define:
edit var1 var7-var10 var3 if gender==1
To see the value codes rather than labels, add the
nolabel
option after a comma:
edit var1 var7-var10 var3
if gender==1, nolabel
When you want to leave the Editor, Stata checks that you want to preserve the
changes you made.
Do Editor
The Do-file Editor is very handy, invoked from a menu button or by typing
doedit. You can enter several lines or insert
another file, such as one of your earlier
*.do
files. You can select a line in the Do Editor and Do only that line. Or Do from
that line to the end of the set of instructions. Or select several lines and
Do them. At the end you can save the contents of the Do Editor as a
*.do
file.
Folders
Type adopath or sysdir
to see the location of various Stata folders for your main files, updates, STB
files, personal ado files, etc.
Statalist
There is a Stata list server with useful advice about Stata, including new programs
to help with special problems. If you subscribe to Statalist you get messages
throughout the day. If you subscribe to Statalist-digest you get a single file
with all the messages once each day. See http://www.stata.com/support/statalist/faq
The Stata Journal and Stata Technical Bulletin (STB)
These publications contain supplementary information about Stata commands and
the use of Stata in research. The first issue of The Stata Journal was released
at the end of 2001 The last issue of the Stata Technical Bulletin (#61) was
in May 2001.
From within Stata you can see what STB procedures are available and download
the ones that interest you. From the Help menu, select STB
and User-written Programs. After that, choose the hypertext links
(clickable blue words in the help window) for the Stata site then then click
on stb
For a complete list of all STB articles, see http://www.stata.com/info/products/stb/stbvols.html
Tips from our home page
The following tips were initially presented on our home page
Stata Tips
Stata tables (August 2008)
If you required a table of cell percentages you could:
sysuse auto, clear
svyset rep78
svy:tab rep78 for, per
An easier way is to use Philip Ryan's user written command tab2way:
tab2way rep78 for, cellpct
this command also has lots of other options. To download this type the following on the Stata commandline:
ssc install tab2way
Stata users have written many commands for tables. To see a list of some of them type the
following on Stata command line (when online):
findit tab table
Then to download click on the hyperlink and follow the instructions
Sending Command(s) to the Stata Do Editor from the Stata Review Window (July 2008)
While running Stata interactively, either with dialogue boxes or from the command line the command(s) that you issue to Stata are recorded in the Review Window. These commands can be put directly into the Do Editor for rerunning a session of Stata again, modifying the commands and rerunning or as a record of the analysis.
Putting the contents or some of the contents of the Review Window into the Do Editor can be done as follows:
In the Review Windows selecting the command(s) that you wish to go into the Do editor by:
Creating a Stata dataset from multiple Excel worksheets (June 2008)
There are a number of ways of doing this:
catplot (May 2008)
Nick Cox has written a useful graph command (catplot) that graphs categorical variables. This user written program
can be downloaded for free.
To download this:
On the Stata commandline window type:
findit catplot
then click on the hyperlink
catplot from http://fmwww.bc.edu/RePEc/bocode/c
and then follow instructions
If the catplot command didn't exist and you wanted to produce a bar plot of the frequencies of the categories of rep78
then you would have to do something like:
sysuse auto, clear
tab rep78, g(z)
graph hbar (sum) z* , bargap(13) asc ///
yvaroptions(relabel(1 "1" 2 "2" 3 "3" 4 "4" 5 "5"))
With catplot this is make easier with the following:
sysuse auto, clear
catplot hbar rep78
For more details on catplot see the online help help catplot (once installed)
For other graphs that Nick Cox has written see: http://www.ats.ucla.edu/stat/Stata/faq/graph/njcplot.htm
Stata Users' Group Meeting Proceedings (April 2008)
Material documenting the Stata Users' group meetings is worth looking through.
It contains articles on a large number of topics.
The list of Stata Users' Group meetings can be found at:
http://www.stata.com/meeting/proceedings.html
For example if you're not sure what regular expressions are, then have a look at:
http://ideas.repec.org/s/boc/wsug07.html
Or the following may be of interest:
Panel data methods for microeconometrics using Stata
http://repec.org/wcsug2007/cameronwcsug.pdf
Powerful new tools for time series analysis may be of interest:
http://repec.org/nasug2007/StataTS07.beamer.7727.pdf
Interested in Stata and genetics? Then have a look at:
A brief introduction to genetic epidemiology using Stata
http://repec.org/usug2007/slides_nshephard.pdf
There is lots more.
Programming Stata - learning by examples (March 2008)
Below are a number of do files that can be run in Stata thus allowing you to see how Stata programming works. By seeing the input and the
ouput you can learn some of the basics of the Stata programming language (for the finer points refer to the Stata manuals).
By learning some programming, Stata can be used more efficiently eg. the use of macro rather
than typing in the same thing again and again.
To use the tutorial:
Mata - learning by examples (February 2008)
Mata is a Stata matrix programming language.
The advantage of Mata is that it is fast and for some problems the solution to these is easier in Mata.
The Mata manuals are very useful for learning Mata. To complement the manuals attached are some Mata tutorials.
The tutorials are a series of examples in a do file. To use the tutorial:
Stata's display command (January 2008)
Stata's display command is useful for writing to the Stata results window or using it as an online calculator
The display command has features that allow various types of output and the tools to format and enhance these.
Controlling the color of the output
Example:
display as text "green" as error " red" as result " yellow" as input " white"
(text, error, result and input are styles)
Controlling where the text is placed
Example:
display _column(50) "column"
Including smcl (smcl is Stata's mark up and control language)
Examples:
display "{center: this}"
display "{hline}"
Formating
Example:
display %9.5f 9
Stata's system values (type creturn list to see these)
Example:
display ("$S_DATE")
display c(current_date)
Also see:
Stata 10 programming manual display
Stata 10 programming manual smcl
Stata Journal Ryan, Philip (2004) "Stata tip 4: Using display as an online calculator", 4:1 Page 93.
In Mata: display()
Creating a binary variable from a continuous variable (December 2007)
On way of creating a binary variable is to generate a new variable containing 0 and then replace the contents
of the variable with 1 based on a qualifier eg.
generate dummy1=0
replace dummy1=1 if mpg <=25
New subcommand for listing user written commands (November 2007)
Many user written commands are stored in the SSC (Statistical Software Components) archive. In the
lastest ado update for Stata 10 a new subcommand has been added to scc:
ssc whatshot
The syntax is: ssc whatshot [, n(#) author(name)]
Examples:
whatshot
whatshot, author(cox)
To get these commands you need to update Stata. To do this with the
pull down menu:
Help>Official Updates and then click on
www.stata.com. Then follow instructions.
For more information on SCC type help scc on the Stata commandline
Undocumented commands (October 2007)
In addition to the commands found in the Stata manual there are also undocumented commands that you may find useful.
To see these type help undocumented.
A commands that you may find useful is: twoway__histogram_gen
This command generates coordinates of the bars in a histogram. An examples of how it works is:
sysuse auto, clear
twoway__histogram_gen mpg , fraction gen(h x)
l h x in 1/20
twoway (scatter h x) (histogram mpg, fraction)
tab x h
exit
Another commands that you may find useful is the matalabel
This command generates 3 matrices in mata, one for each of: value label name, value and the label
An examples of how it works is:
sysuse auto, clear
matalabel , generate("a" "b" "c")
mata
a
b
c
mata describe
vallab=(a,c)
vallab
b
end
User written program - examples (September 2007)
When learning new commands in Stata it is often useful to have examples of how the syntax is applied. Stata's documentation
includes many examples and allows you to downloaded data sets for these (File/Example datasets), thus allowing you to
reproduce the results.
Also, Stata's online help includes many examples. Another useful source of examples is Nick Cox's examples user written program
An example of some of what you get by typing examples egen
Settings for Stata (August 2007)
Various features of Stata can be set to individual preferences or changed to meet the requirements for a particular analysis.
To see what can be set type query on the Stata command line
Amongst the things that can be set (in Stata 10) is whether or not you would like graphs tabbed on the graph window or each open graph in a separate graph window.
The syntax for this command is:
set autotabgraphs on , permanently
Other set commands that you are likely to use are:
set more off
set memory
For more information see the Stata 10 reference manuals
[R] query -- Display system parameters
[R] set -- Overview of system parameters
Copy as picture - Copying from the results windows to Word and Excel (July 2007)
Stata 10 has a copy feature that allows you to copy highlighted parts of the results windows to Word,
Excel and other packages, as a picture. To use this, highlight what you want copied in the results window,
right click the mouse button and click on to "Copy as Picture". Then paste into another package.
In the other package this can usually be cropped and edited in the normal way.
Estout - Stata Regression Tables (June 2007)
Estout is a useful user written command for outputing regression results in various forms.
For more information you can see the estout web site go here
Adoupdate (May 2007)
The commands under update are useful for keeping Stata's executable file and the official Stata ado files
up to date (see help update). However, these do not keep the user written ado files up to date. (user written programs
that you have downloaded). To ensure that you are working with the latest version of a user written ado
file type adoupdate on the Stata command line or using the pulldown menu help/SJ and user written programs
and then click on update. (You must be online to use this command)
For more information on the adoupdate command see help adoupdate.
Also, see update
Nested Do file (April 2007)
Stata allows you to break up your analysis in to logical sections; each part being a separate do file, with all the parts of the analysis contained in one do file. eg.
**master****the do files below are contained in do file that you have name master.do (can be called any other name)
.
.
.
do projA_data
do projA_error_checking
run projA_data_man
if M1==2 {
do projA _A1 // projA_A1 exits finishes analysis
}
do projA_results
exit
**master*************
In fact Stata allows nesting up to a depth of 64. eg. a do file calls another do file which calls another do file; up to 64 times.
Nesting do files has some advantages:
Personal help file (March 2007)
Stata comes with help files for it's commands. However you may wish to compile a list of frequently used, but hard to remember commands in your personal help file
Your own help file is saved as file with hlp extension eg. me.hlp on the adopath
An example of a help file is as follows:
{smcl}
{* 03may2005}{...}
{cmd:help Joe Blow } {right:updated 1 March 2007}
{hline}
{title:Wildcards and symbols}
{p2col :{helpb comments:comments} *, ?, ///, etc.}{p_end}
{hi:Wildcard zero or more} * or ~
{hi:Wildcard one character} ?
{hi:Continue onto next line} ///
{hi:Commment out line} * at beginning or /// midline
To learn more about smcl see the Stata Users Guide or look at a Stata help file (.hlp extension) in the do file editor.
spmap -- Visualization of Spatial Data (February 2007)
spmap is a user written command that can be down loaded for free.
To download:
Make sure that you are online.
Type findit spmap
Then click on the hyperlink.
Once installed type help spmap to see the help file. At the bottom of the help file there are
examples of what can be done. Click the hyperlink to see the graphs.
Here are some of the examples.
stcmd - Using Stat/Transfer within Stata (January 2007)
The user written command stcmd can be used within Stata to change the data format of data sets stored on disk. stcmd uses Stat/Transfer to do this.
To use this command you must first have Stat/Transfer and stcmd installed
To get Stat/Transfer contact Survey Design and Analysis Services (details below).
To get stcmd type findit stcmd in the Stata command Window and follow instruction to install the program
Examples
Using stcmd to convert a Stata data set to Excel
stcmd "c:\Program Files\Stata9\auto.dta" c:/auto.xls, replace
Using stcmd to convert a Stata data set to SPSS
stcmd "c:\Program Files\Stata9\auto.dta" c:/auto.sav
Using stcmd to converting many files from Excel to Stata
stcmd mat*.xls *.dta.
For more information see help stcmd (stcmd must first be installed)
Also see fdasave for another way of changing the Stata data format to SAS
encode (December 2006)
encode is a useful command for converting strings to numbers. encode does this in alphabetical order eg.
With the following dataset
var1
a
b
c
encode var1, gen(var1a)
(Note: when Stata encodes it produces a value label: to see this type label list )
If this is not the encoding that you require a way around this is to define a value label first and then use the label options for encode.
Eg.
If you have:
var1
a
b
c
But would like var1a encode like:
var1a
3
1
2
You would first define the value label eg.
label define preference1 a 3 b 1 c 2
And then applying this using the encode command
encode var1, label(preference1) gen(var1a)
Resulting in:
The code to run the above:
clear
input str1 var1
a
b
c
end
label define preference1 3 a 1 b 2 c
encode var1, label(preference1) gen(var1a)
label list
list, nolab
For more information see:
Stata 9 Data Management manual
kdensity (November 2006)
One of the problems with combining a number of histograms is that, generally where there are more than 3, the graph becomes unreadable. kdensity may be an a solution to this problem.
sysuse auto, clear
twoway (kdensity mpg if rep78==1, color(red)) ///
(kdensity mpg if rep78==2, color(blue) ) ///
(kdensity mpg if rep78==3, color(black)) ///
(kdensity mpg if rep78==4, width(1)color(green) ) ///
(kdensity mpg if rep78==5, color(purple)) , ///
legend(label( 1 mpg at rep78=1) ///
label( 2 mpg at rep78=2) ///
label( 3 mpg at rep78=3) ///
label( 4 mpg at rep78=4) ///
label( 5 mpg at rep78=5))

For more information see:
Stata 9 graphics manual
A Visual Guide to Stata Graphic by Michael Mitchael
Stata Journal Vol 3 No. 2
Intermediate graph commands (October 2006)
Graphs cannot always be combined; even with the addplot option.
However, you can still get combined graphs by using the pci ,
twoway scatteri and twoway pcarrow commands.
For example if you wished to add a box plot to a scatter plot this could be achieved with the aid of the pci command and a twoway scatter.
sysuse auto, clear
qui sum mpg, detail
local a= r(p25)
local b= r(p75)
local c=r(p50)
local uav=`b'- 1.5*(`a'-`b')
local lav=`b'+ 1.5*(`a'-`b')
twoway (scatter mpg weight) ///
(pci `a' 3000 `b' 3000, lcolor(red)) ///
(pci `a' 3400 `b' 3400, lcolor(red)) ///
(pci `a' 3000 `a' 3400, lcolor(red)) ///
(pci `b' 3000 `b' 3400, lcolor(red)) ///
(pci `c' 3000 `c' 3400, lcolor(red)) ///
(pci `uav' 3200 `b' 3200, lcolor(red)) ///
(pci `uav' 3150 `uav' 3250, lcolor(red)) ///
(pci `lav' 3200 `a' 3200, lcolor(red)) ///
(pci `lav' 3150 `lav' 3250, lcolor(red)) ///
(scatteri `c' 3450 "Median",mlabangle(45) mlabsize(8)) ///
, legend(off)
For more information see:
Stata tip 21 SJ 5 No. 2 pp282-284
Stata 9 graphics manual
MATA (September 2006)
Mata is Stata 9's new matrix programming language.
If you haven't had a look at Mata yet, then here are some examples of what you can use it for:
Example 1
Sorting rows in alphabetical order (statalist-digest V4 #2451)
(the user written program moremata must first be installed)
clear
input str20 x1 str20 x2 str20 x3
"massagli,mark" "wood,j." "dessent,harold"
"beletz,elaine" "carter,annie" "curtis,barbara"
"bradshaw,joe" "brown,arnold" "dunaway,lowell"
"schneider,mark" "mullins,bobby" "sump,lawrence"
end
list
tempfile foo
mata
C= J(3,1,"") //creates a new vector
A = st_sdata(.,.)' // a transpose view of the data in stata
for (i = 1; i <=cols(A); i++) {
A = sort(A,i)
C = C,A[.,i]
}
C=C[.,(2::cols(A)+1)]'
mm_outsheet("`foo'",C, mode="r")
end
insheet using `foo', clear tab
list
Example 2
xpose using mata (statalist-digest V4 #2328)
(the user written program moremata must first be installed)
clear
tempfile tmp1
input str16 v1 str2 v2 str2 v3 str2 v4
"Sex" M M M
"Age" 47 66 56
"Left eye"
"Right eye" Y Y Y
"Lower eyelid" Y Y Y
"Upper eyelid"
"Lateral canthus"
"Medial canthus" Y Y
"Recurrent lesion"
"Primary lesion" Y Y Y
end
list
mata
A = st_sdata(.,.)'
mm_outsheet("`tmp1'",A, mode="r")
end
insheet using "`tmp1'",clear
l, ab(15) noobs
Mata can do much more. To learn more see:
Translating Fortran
SJ 5(3), 3rd quarter 2005, 421-441
Using views onto data
SJ 5(4), 4th quarter 2005, 567-573
Creating new variables (Sounds boring, isn't)
SJ 6(1), 1st quarter 2006, 112-123
Interactive use
SJ 6(3), 3rd quarter 2006, 387-396
Various responses on Statalist
Mata Stata 9 reference Manual
numlabel (August 2006)
numlabel is a command that prefixes numeric values to value labels.
Without numlabel
numlabel , add //adding numlabel
For more information see help numlabel and the Stata 9 Data Management Manual
viewsource (July 2006)
viewsource is a command that allows a file located on the adopath to be viewed in the Stata viewer.
Example: To view the code for the t test type viewsource ttest.ado
For more information see help viewsource and the Stata 9 Programming Manual
datasignature - Determine whether data have changed (June 2006)
If you have updated Stata 9 to the latest update (17 May 2006) you will find that a new Stata
command has been added: datasignature. (to find out what has been added with the update, using the pulldown menu: Help>what's new
or type whatsnew on the Stata command line)
Datasignature give a number based on the following:
1. The number of observations and number of variables in the data.
2. The values of the variables.
3. The names of the variables.
4. The order in which the variables occur in the dataset if varlist is not specified, or in varlist
if it is.
5. The storage formats of the individual variables.
Datasignature can be used for the following:
Examples of interactive use
1. checking with previous datasignature to see if the data has changed.
2. checking if you are working with the same dataset as your colleges.
For more information see help datasignature
Simple Thematic Mapping(May 2006)
tmap is a user written Stata program that allows you to map your data.
For more information on using tmap see:
FAQ
Stata Journal Vol 4 - No 4
Some shape data sources for Australia:
AEC
VDS Technologies
Maps based on postcode can be purchased.
I mapped the Victoria electoral map using the following for actual population. Other maps can be generated by adding your own data and then mapping this.
*-----------------------start do file------------------------------------
clear
cd "C:\ASTATA INFO\learning\tmap" //where the data has been downloaded to
set matsize 3000
mif2dta VIC20030129_elb, genid(id)
use VIC20030129_elb-database
describe
tmap choropleth actual, id(id) map("VIC20030129_elb-Coordinates.dta") palette(Reds)
exit
data downloaded from
http://www.aec.gov.au/_content/Who/profiles/gis/gis_datadownload.htm
*-----------------------end do file------------------------------------
Separate (April 2006)
A useful Stata command for creating separate new variables based on either an expression or a variable is separate
Eg.
separate mpg, by(mpg>20)
will create 2 new variables one being mpg<=20 and the other mpg>20
other examples are:
separate mpg, by(mpg)
separate mpg, by(mpg) gen(MPG)
For more information on the separate command see the Stata 9 Data Management manual or online by typing help separate.
Macro Expressions (March 2006)
Rather than typing
local a=r(N)
forvalues i = 1/`a' {
}
The above code can be reduced to
forvalues i = 1/`= r(N)' {
}
For more information on macro expressions see the Stata 9 Users guide [U] 18.3.8
inlist() & inrange() functions (Febuarary 2006)
Stata has many functions that make using Stata easier. Eg.
count if mpg==22 | mpg==25 | mpg==34 | mpg==45
can be written as:
count if inlist(mpg,22,25,34,45)
Another function is inrange() eg.
count if inrange(mpg, 23,34)
Instead of
count if mpg>=23 & mpg<=34
These functions can be used after the if qualifier with commands such as generate, list, summarize etc., or after assert,
Examples:
assert inlist(mpg,22,25,34,425)
generate mpg1=mpg if inlist(mpg,22,25,34,45)
list mpg if inlist(mpg,22,25,34,45)
list mpg if inlist(mpg,22,25,34,45) | inlist(mpg,15,26,35,55) ///use 2 inlist functions when the list exceeds the max. allowed for 1 function
set trace on (January 2006)
local all `"`all' `"`=`v'[`i']'"'"'
set trace off
with our data a section of the trace will look like this:
- local all `"`all' `"`=`v'[`i']'"'"'
= local all `" `"Volvo 260"' `"11995"' `"17"' `"5"'"'
- set trace off
The first line is the line being executed. It has a - in front of it to indicate it is being executed. The second line is after macro substitution has occurred. It has a = in front of it to indicate that substitution has occurred.
For more information on trace see the Stata 9 programming manual. Also see the Stata command pause.
Do-file Editor(December 2005)
When typing commands in the Stata Do-Editor, individual commands or a selection of commands can be
run by highlighting the section that you would like to run and then pressing the do icon.
This allows you to try out your file section by section.
Regular Expressions(November 2005)
Regular expressions allow the matching of complex text patterns. Regular expression commands have been included in
Stata 9 with the commands:
regexm - regular expression match
regexs - return nth subexpression from match
regexr - replace match expression with new string
For example
you wish to have the day as a separate variable in the following data set:
date
12jan2003
1april1995
17may1977
02september2000
|
date
|
day
|
|
12jan2003
|
12
|
|
1april1995
|
1
|
|
17may1977
|
17
|
|
02september2000
|
02
|
Text Editors(October 2005)
The text editor that comes with Stata is fine for small programs. However, as the size of the program
increases other text editors are often used
to make programming easier. For a discussion on various text editors go
here
The function sum() (September 2005)
sum(x) returns the running sum of x. A basic use of sum() would be: generate running_tot =sum(1)
Another example of the use of sum() is: given the data below you need to create a new
variable that starts with zero and goes to zero for changes in id and increases by 1 for changes in var2.
id var2
1 7further information can be found by typing help sum() on the Stata command line
clonevar (July 2005)
Stata 9 has a useful commands that generates an exact copy of an existing variable.
eg clonevar MPG=mpg
for more information see help clonevar
Docking Stata 9 Windows (June 2005)
The UCLA site has much useful information on using Stata.
If you are new to Stata 9 then the movie on docking windows will be useful.
To see this go here
Getting the path and file name onto the Stata command
line (March 2005)
Stata 8 has a handy way of getting files names complete with the path onto
the command line. Rather than typing folders, sub folders, and file name use
the pull down menu File/Filename, click onto the required file and path and
file name will be shown on the command line; enclosed in quotation marks. This
is particularly handy when the path consists of many sub directories with long
names. You can then add commands such as cd, use to the command
line.
Tabout - a user written command
(February 2005)
tabout- produces publication quality tables from Stata, with the output exported
to a text file. It can be exported as tab-delimited, html code or LaTeX/TeX
code. -tabout- provides extensive user control over formating of data and labels
and generates table headers automatically
ssc install tabout
(or: ssc install tabout, replace).
To make learning the syntax easy, an example file which can be used as a tutorial is available here
window command (January 2005)
The window command can be be useful for adding your frequently used commands to the pull down menu, pushing commands to the review window and displaying the current file in the top left hand corner of the Stata window and a lot more.
To have your current file name displayed on the Stata window you can add the following to your do file:
window manage maintitle "`c(filename)'"
See your programming manual for further details on the window command
ds - Describing Variables and Saving Results (December 2004)
ds lists the variable names of the dataset currently
in memory in a compact form. The command is useful if you require a list of
variables that satisfies certain criteria. The list that results is saved in
r(varlist) which can be used in other commands
eg.
(Using the auto dataset supplied with Stata)
use "c:/stata8/auto.dta",
clear
ds, not(vall origin)
list `r(varlist)'
ds m*
list `r(varlist)'
See describe in the Stata reference manual for
more details.
Also see statalist-digest V4 #1701 & #1607
WORKING IN ROWS (December 2004)
The egen command has a number of functions that make it easier to work with data in rows. Rather than using xpose or reshape to convert the data to columns these commands may be able to be used.
Egen's row functions"
rfirst(varlist)
may not be combined with by. It gives the first nonmissing value in
varlist for each observation (row). If all values in varlist are
missing for an observation, newvar is set to missing.
rlast(varlist)
may not be combined with by. It gives the last nonmissing value in
varlist for each observation (row). If all values in varlist are
missing for an observation, newvar is set to missing.
rmax(varlist)
may not be combined with by. It gives the maximum value (ignoring
missing values) in varlist for each observation (row). If all values in
varlist are missing for an observation, newvar is set to missing.
rmean(varlist)
may not be combined with by. It creates the (row) means of the
variables in varlist, ignoring missing values; for example, if three
variables are specified and, in some observations, one of the variables
is missing, in those observations newvar will contain the mean of the
two variables that do exist. Other observations will contain the mean
of all three variables. Where none of the variables exist, newvar is
set to missing.
rmin(varlist)
may not be combined with by. It gives the minimum value in varlist for
each observation (row). If all values in varlist are missing for an
observation, newvar is set to missing.
rmiss(varlist)
may not be combined with by. It gives the number of missing variables
in varlist for each observation (row). String variables -- if specified
-- are counted as containing missing when their value is ""; numeric
variables are counted as containing missing when their value is system
missing (.) or extended missing (.a, ..., .z).
robs(varlist) [, strok]
may not be combined with by. It gives the number of nonmissing values
in varlist for each observation (row) -- this is the value used by
rmean() for the denominator in the mean calculation.
String variables may not be specified unless option strok is also
specified. If strok is specified, string variables will be counted as
containing missing values when they contain ""; numeric variables
will
be counted as containing missing when their value is ., as usual.
rsd(varlist)
may not be combined with by. It creates the (row) standard deviations
of the variables in varlist, ignoring missing values. Also see rmean().
rsum(varlist)
may not be combined with by. It creates the (row) sum of the variables
in varlist, treating missing as 0.
A REMINDER TO START A LOG (November 2004)
Would you like to be reminded to start a log each time that you start Stata.
One way of doing is this is to include the command below in your profile.do file
db log
For information on profile see the GETTING STARTED MANUAL - More on starting and stopping Stata
Version Control (October 2004)
PROBLEM: Stata is continually being improved, meaning programs and do-files written for older versions might stop working.
SOLUTION: Specify the version of Stata you are using at the top of programs and do-files that you write:
------------------------------------------ myprog.do ---
version 8.2
use mydata, clear
regress ......
------------------------------------------ myprog.do ---
---------------------------------------- example.ado ---
program myprog
version 8.2
...
end
---------------------------------------- example.ado ---
For further information see the Stata programming manual
Assert (September 2004)
Assert is a useful command for verifying your data. e.g..
assert sex=="Male" | sex=="Female"
assert mpg<50 & mpg>10
Also see Stata reference manual for further information.