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
Useful links
The Stata resources web page is worth a look at. It has links to free downloadable tutorial etc.
http://www.stata.com/links/resources1.html
UCLA graphics page using Stata may also be of interest:
http://www.ats.ucla.edu/stat/stata/Library/GraphExamples/default.htm
Setting up docked windows (Great for learning to set up Stata 9 windows)
http://www.ats.ucla.edu/stat/stata/faq/stata9gui/dockfloatpin.html
These are tutorials for learning Stata
http://data.princeton.edu/stata/
http://dss.princeton.edu/online_help/stats_packages/stata/stata.htm
Stata Programming
http://www.stata.com/meeting/11uk/baum.pdf
Tips
from our home page
The following tips were initially presented on our home page. To see the current monthly tip click here
Stata Tips
List of past tips
| The input command | July 2010 |
| Splitting the Do Editor | June 2010 |
| Factor variables and lincom to produce a table | May 2010 |
| Stata graphs | April 2010 |
| Tabdisp | March 2010 |
| Tables to spreadsheet | February 2010 |
| Tables to spreadsheet | January 2010 |
| Point Estimates for a Regression | December 2009 |
| Doing thing quietly in Stata | November 2009 |
| Graphing functions | October 2009 |
| Stata 11 - Variable manager | September 2009 |
| Getting a Subset of a large dataset into Stata | August 2009 |
| Capture | July 2009 |
| Transparent Graphs | June 2009 |
| Getting Stata's Graph editor commands into Stata graphs | May 2009 |
| Weaving Stata results into a Word Report | April 2009 |
| Stopping Stata during the running of a do file | March 2009 |
| Putting Greek symbols in graphs | February 2009 |
| Doing things by levels of a variable | January 2009 |
| Automation of Tables in Stata | December 2008 |
| Memory usage in Stata | November 2008 |
| Stata Comment | October 2008 |
| Stata user written graphs | September 2008 |
| Stata tables | August 2008 |
| Sending Command(s) to the Stata Do Editor from the Stata Review Window | July 2008 |
| Creating a Stata dataset from multiple Excel worksheets | June 2008 |
| catplot | May 2008 |
| Stata Users' Group Meeting Proceedings | April 2008 |
| Programming Stata - learning by examples | March 2008 |
| Mata - learning by examples | February 2008 |
| Stata's display command | January 2008 |
| Creating a binary variable from a continuous variable | December 2007 |
| New subcommand for listing user written commands | November 2007 |
| Undocumented commands | October 2007 |
| User written program - examples | September 2007 |
| Settings for Stata | August 2007 |
| Copy as picture - Copying from the results windows to Word and Excel | July 2007 |
| Estout - Stata Regression Tables | June 2007 |
| Adoupdate | May 2007 |
| Nested Do file | April 2007 |
| Personal help file | March 2007 |
| spmap -- Visualization of Spatial Data | February 2007 |
| stcmd - Using Stat/Transfer within Stata | January 2007 |
| encode | December 2006 |
The input command
When you're working with a data management or statistical command in Stata that you have not previously used, you may not be confident that you are doing this correctly. So rather then work with the complete data set it's often useful to
make up a small data set that contains the critical points and run this to see if it is doing what you had anticipated. Once
satisfied you can run this on the complete data set. For example if I wished to identify the observations that included the current
date and up to 4 days in advance the following could be used:
clear
input ///
str15 dates
"12/7/2010"
"13/7/2010"
"14/7/2010"
"15/7/2010"
"16/7/2010"
"17/7/2010"
"18/7/2010"
"19/7/2010"
end
list
gen date1=1 if inrange(date(dates, "DMY"), date(c(current_date),"DMY"),date(c(current_date),"DMY")+4)
list
exit
After running the above we see the result
. list
+-------------------+
| dates date1 |
|-------------------|
1. | 12/7/2010 . |
2. | 13/7/2010 . |
3. | 14/7/2010 1 |
4. | 15/7/2010 1 |
5. | 16/7/2010 1 |
|-------------------|
6. | 17/7/2010 1 |
7. | 18/7/2010 1 |
8. | 19/7/2010 . |
+-------------------+
. exit
Errors in logic can now more easily be spotted and you have saved time by not running the complete data set.
When this had been satisfactorily run it could be included in the main do file.
For further information on this command see:
help input
For further help on the above code see:
help comments
help date
help dates
help inrange()
help creturn list
Splitting the Do Editor
In Stata 11 the do editor can be split, making it easier to do some types of work. To do this there must be at least two tabs on your do editor. Pull one of these to the middle of the editor. When a selection box appears select one and 2 tabbed do editors windows appear.
Pulling a tab to the centre
Now there are two do editor windows
Factor variables and lincom to produce a table (May 2010)
Stata 11's factor variable can be combined with lincom to quickly produce tables.
In this example we look at the table on P226 of "Statistical Modeling for Biomedical Researchers: A Simple Introduction to the Analysis of Complex Data, 2nd Edition by William D. Dupont" (See out bookshop to order).
The data set can be downloaded: http://biostat.mc.vanderbilt.edu/dupontwd/wddtext/index.html
set more off
cd "C:\data\dupont" //if the data is stored in a different directory change this
//to where it has been stored
use "5.5.EsophagealCa.dta", clear
recode tobacco 3=2 4=3, g(smoke)
label define q_smoke 1 "0-9" 2 "10-29" 3 ">=30"
label value smoke q_smoke
logistic cancer i.alcohol i.smoke i.age [fw=patients]
forvalues i=1/4 { //alcohol
forvalues j=1/3 { //smoke
qui: lincom `i'.alcohol + `j'.smoke, or
local a`i'`j'=r(estimate)
}
}
local a11=1
decode alcohol, gen(a)
contract a
keep a
matrix aa=( `a11', `a12', `a13' \ `a21', `a22' ,`a23' \ `a31', `a32' ,`a33'\ `a41', `a42' ,`a43')
svmat aa
rename aa1 Tobacco_0_9
rename aa2 Tobacco_10_29
rename aa3 Tobacco_30
list
exit
In the above the forvalue loop gets the different levels of alcohol and smoke. These are then applied to the factor variables in the lincom command. The returned values from lincom are then stored in a Stata matrix; one at a time. After going through all the combination of alcohol and smoke the matrix is then put into Stata and some labels applied.
For more information on the specific commands type help and then the command eg. help lincom
Stata Graphs (April 2010)
From time to time Stata is used to produce non-standard/interesting graphs. I have compiled some of these graphs. These have mainly
been presented on the Statalist. To see these graphs click
here .
This page will be updated from time to time.
To see some of the User written graph commands click
here .
(from a previous tip)
Tabdisp (March 2010)
tabdisp is a Stata command that allows you to display Stata tables. This command allows lots
control of the way that the elements are displayed.
If cell percentages are required the following can be used:
sysuse auto, clear
contract for rep78
list
summarize _freq
generate percentage=(_freq/r(sum))*100
tabdisp for rep78, cell(percentage) cellwidth(7)
Or if the % symbol is also required:
sysuse auto, clear
contract for rep78
list
summarize _freq
generate percentage=(_freq/r(sum))*100
gen freq=string(percentage, "%5.2f")
replace freq=freq + "%"
tabdisp for rep78, cell(freq) cellwidth(7)
If the above is what was required then instead the user written program: tab2way or tab3way could be used
There are many other ways to display your data eg. including the words max and min in the table cells
sysuse auto, clear
contract for rep78
list
sort _freq
tostring _freq, gen(freq)
replace freq=freq+ " Max" in `=_N'
replace freq=freq+ " Min" in `=_n'
tabdisp for rep78, cell(freq) cellwidth(7)
For help on the individual commands type help and then the command name.
To download the user written command tab2way or tab3way , type: ssc install tab2way or ssc install tab3way
Tables to spreadsheet (February 2010)
The tabulate command allows the values of the table to be save as matrices eg. options for the tabulate command are: matcell(), matrow() and matcol(). These matrices can be put into a spreadsheet. The table command however does not have these matrix options. However, there are workarounds that make it easy to put the results that the table command would have given into a spreadsheet. This tip explores a number of ways that this can be done:
This is the command what we wish use and then get the resulting table out of Stata and into a spread sheet
sysuse auto, clear
//table in offical stata
table for rep78 , c(mean price)
The following gives us what we want but does not allow the output to be put into a spreadsheet
sysuse auto, clear
collapse (mean) price, by(foreign rep78)
list
tabdisp foreign rep78 , c(price)
This time getting the table into a Stata data set so it can be exported to a spreadsheet
This method has the advantage that the colum and row labels are also included
sysuse auto, clear
collapse (mean) price, by(foreign rep78)
list
drop if rep78==.
reshape wide price, i(foreign) j(rep78) //because the data is in long form it can be reshape
// into the required table
list
outsheet using c:/table, replace //outputting the table to a form that can be read with a spreadsheet
This time using Mata to manipulate the initial data
sysuse auto, clear
collapse (mean) price, by(foreign rep78)
fillin foreign rep78
drop if rep78==.
sort for rep78
list
mata: //start of Mata
a=st_data(.,.)
a
s=J(2,6,.)
s
for(i=1; i<=10; i++) {
r=a[i,2]
c=a[i,1]
s[r+1,c]=a[i,3]
}
names = st_varname((1..3))
names
b2=st_varvaluelabel(names[1,1])
b2
if(b2!="") {
zy2=uniqrows(a[.,1])
b3=st_vlmap(b2, zy2)
b3
}
else {
b3=strofreal(uniqrows(a[.,1]))
b3
}
b2a=st_varvaluelabel(names[1,2])
b2a
if(b2a!="") {
zy2a=uniqrows(a[.,2])
b3a=st_vlmap(b2a, zy2a)
b3a
}
else {
b3a=strofreal(uniqrows(a[.,2]))
b3a
}
table=(""\b3a) ,(b3',"."\strofreal(s))
table
mm_outsheet( "c:/table1" ,table, mode="r") //user written program output to a Excel readable file
end
As you can see there are a number of different ways of getting table information out of Stata.
For help on the individual commands type help and then the command name.
To download the user written command mm_outsheet, type: ssc install moremata
Tables to spreadsheet (January 2010)
When a large number of tables are required to be put into a spreadsheet and no use written program is available to easily do this
the following method can be used:
Write a program for the particular table (or any output) that you require. If there are a number of different tables
then written a program for each type of table.
The program starts a log file and then runs the table command. It then closes the log file. The log file is
then put through a file filter to remove any unwanted text.
The partially cleaned up log file is then imported into Stata using the insheet command and then further
cleaned up; removing any unwanted text and then the columns in the table are split into Stata columns.
The extent of the clean up depends on the desired output.
Having finished the cleaning up, this is either saved or appended to, using the required program option.
Then you go on to append the next table to the file.
When finished the file containing the tables can be opened in a spreadsheet
| clear programs |
//Clears the previous program to allow for modfications. |
|
//This can be removed when you are happy with the program |
| program tables |
|
| version 11.0 |
|
| syntax varlist(max=2 min=2) [, append] gen(string) |
|
| tokenize `varlist' |
//split varlist |
| |
|
| capture log using a, text replace |
|
| label var `1' `=strtoname("`:var lab `1'' " )' |
//combining the label into one word |
| label var `2' `=strtoname("`:var lab `2'' " )' |
|
| table `1' `2', stubwidth(40) |
|
| log close |
|
| |
|
| filefilter a.log a1.log , from("-") to("") replace |
//deleting unwanted text in the log file |
| filefilter a1.log a2.log , from("|") to("") replace |
|
| filefilter a2.log a3.log , from("+") to("") replace |
|
| insheet using a3.log, clear |
//brings the modified log file into Stata |
| drop in -4/-1 |
//get rid of other material |
| drop if strpos(v1,"log") |
|
| drop if strpos(v1,"pause") |
|
| drop if strpos(v1,"resumed") |
|
| drop if strpos(v1,"unnamed") |
|
| drop if strpos(v1,":") |
|
| capture drop v2 |
|
| split v1 |
|
| drop v1 |
|
| |
|
| //additional cleaning up if required |
|
| replace v11=subinstr(v11,"_", " ",. ) in 1/2 |
|
| quietly: d |
|
| local a1=round(`r(k)'/2) |
|
| replace v1`a1'=v11[1] in 1 |
|
| replace v11="" in 1 |
|
| |
|
| set obs `=_N+2' |
//two line space between tables |
| |
|
| if "`append'"!="append" { |
|
| save `gen', replace |
|
| } |
|
| |
|
| if "`append'"=="append" { |
|
| append using `gen' |
|
| save `gen' , replace |
//saves file to hard disc |
| } |
|
| end |
//end of program |
| |
|
| |
|
| |
|
*********************************************
**the commands that calls the above program
*********************************************
|
|
| sysuse auto, clear |
|
| set more off |
|
| cd c:/ |
|
| tables for rep78, gen(aa) |
//1st table |
| |
|
| sysuse auto, clear |
//2nd table |
| tables rep78 for, append gen(aa) |
|
| |
|
| sysuse auto, clear |
//3rd table |
| tables for rep78, append gen(aa) |
|
| |
|
| list ,noheader sep(0) noobs |
|
| |
|
| outsheet using c:/aa.csv, comma nonames replace |
//saving to disk this can be opened in a spreadsheet |
| |
|
| exit |
|
For more information see:
see help for the specific command
User written table output commands include:
tabout
logout
esttab
Point Estimates for a Regression (December 2009)
After a regression point estimates can be obtained with:
Examples:
sysuse auto, clear
regress mpg weight
display _b[weight]*3000+_b[_cons]
OR
sysuse auto, clear
regress mpg weight
lincom weight*3000+_cons
OR
sysuse auto, clear
regress mpg weight
//then open the data editor and add an additional observation for the weight variable eg. 3000
//then run the following
predict a
//then display the point estimate with the following
display a[_N]
This then displays the point estimate. The last method is useful when a number of estimates need to be made.
For more information see:
help predict
Doing thing quietly in Stata (November 2009)
Stata's quietly command allows commands to be run without outputting to the results window.
This is useful if you only require the returned results (eg. r(mean) etc see help return list ) and not the actual output.
Example:
sysuse auto, clear
quietly summarize mpg, detail
or
quietly: summarize mpg, detail
Also you can have a block quiet:
sysuse auto, clear
quietly {
summarize mpg, detail
local a=r(mean)
summarize price, detail
local a=r(mean)
}
If you wish to see specific output in a quiet block you can add noisily to this
Example:
sysuse auto, clear
quietly {
summarize mpg, detail
local a=r(mean)
noisily summarize price, detail
local a=r(mean)
}
For more information see:
help quietly
Graphing functions (October 2009)
The graph histogram command allows a normal distribution option to be included in this graph. The twoway graph however does not have this option. However, this can also be easily done by adding a function graph, as shown in the following example:
sysuse auto, clear
quietly summarize mpg
twoway (histogram mpg, bin(10), ) ///
(function y=normalden(x, `r(mean)', `r(sd)'), range(4 44) xlabel(#10) )
Lots of other functions can be drawn eg.
twoway function t=tden(1, x), range(-5 5) xsize(4) ysize(2) color(blue) ///
lstyle(p1solid) xlabel(-5(1)5) recast(area) || function z=normden(x), range(-5 5) ///
color(maroon) lwidth(thick)
twoway function c=chi2(1,x), range(0 5) xsize(4) ysize(3) yline(.5)
twoway function c=Fden(5, 10, x), range(0 5) xsize(4) ysize(3) yline(.3)
Stata 11 - Variable manager (September 2009)
Getting variable names into a do file:
The Stata 11 variable manager makes this easy. Just highlight the variable name(s) in the variable manager,
right click and then click onto "copy variable list" . Go to the do Editor and paste where required.
Filtering variable names
On the top left hand side of the variable manager is the variable filter. Start typing any part of the variable name
in the filter and the variables that include this text remain in the variable manager list; the others disappear. This is a great
feature for looking for a particular variable in a large dataset.
For more information:
help varmanage (Access Stata's PDF manual by clicking on the online help hyperlink: [D] varmanage )
Getting a Subset of a large dataset into Stata (August 2009)
The various flavours of Stata have limits on various commands, label lengths, macro lengths etc. One of the limits is the maximum number of variables that can be loaded into Stata.
In Stata/IC 11 the limit is set at 2,047 variables
To see the limits of the various flavours of Stata see: help limits
If your data set contains more than 2047 variables and you do not need all of these in Stata then the second syntax of Stata's use
command can be used to get a subset of this data set into Stata
help use
use [varlist] [if] [in] using filename [, clear nolabel]
example:
use mpg using "c:/program files/stata11/auto", clear
This loads only the mpg variable into Stata.
If you wish to inspect a dataset in memory (to see variable names etc.) you can use the second syntax of Stata's describe command
describe [varlist] using filename [, file_options]
example:
describe using "c:/program files/stata11/auto", varlist
return list
Also see:
help memory
Capture (July 2009)
Controlling the unknown
Stata commands that result in an error, issue a non zero return code (_rc). In Stata 10 and Stata 11 the return codes can be seen in the
Review Windows (you may need to expand the Reviews window to see the _rc column)
If a command in an do file produces an error the do file will stop. This can be prevented by
prefixing the command with the capture command eg.
log close //example 1
capture log close //example 2
In the above example 1, a do file/program would stop running if there was no log file open. Stata requires a log file to
be open before it can be closed and no other log file open before it can open a log file.
In example 2, a do file/program would continue to run even if there was no log file open. The capture command
allows errors to be ignored.
Apart from preventing a do file/program from stopping, the capture command can also capture a
command's return code in _rc. The return code (_rc) can then be used to make a decision
in your do file/program.
Example
sysuse auto, clear
tostring mpg, replace //for the purposes of the example convert mpg to a string variable
describe
foreach v of varlist price-foreign {
capture confirm numeric variable `v'
display _rc //allow you to see the return code
if _rc { //if _rc is not 0 (zero) the statement is true and Stata goes into the loop
destring `v',replace
describe `v'
}
}
Also see:
http://www.stata.com/statalist/archive/2009-06/msg00623.html (An example of how to use a return code to set up the default directory in Stata.)
help confirm
help capture
Transparent Graphs (June 2009)
Stata graphs can be made transparent in MS Word and other software. For example the following graph was produced in Stata
and then made transparent in Word.
The above graph was produced in Stata by:
sysuse auto, clear
twoway ///
(histogram mpg if rep78==3, fcolor(green)) ///
(histogram mpg if rep78==4, fcolor(blue))
graph export c:/hist.wmf, replace
Then in Word 2003
Insert>Picture>from file and then c:/hist.wmf
Click on graph
Edit picture
Right Click on a bar that you wish to make transparent
Format AutoShape>Color and lines tab>Fill section and the move the transparency slider to 50% and press OK
Continue to edit all the bars this way. The legend can also be modified as per above
Save
Also see:
http://www.stata.com/statalist/archive/2009-04/msg00574.html
http://www.stata.com/statalist/archive/2009-04/msg00612.html
Getting Stata's Graph editor commands into Stata graphs (May 2009)
Stata has a great graph editor. However, after you have modified your graph the editor will not produce the normal
Stata code for this graph. However, it is possible to retrieve the editing commands if they have been recorded
using the Stata graph editor recorder, adding gr_edit at the start of each editor line and then adding this to the initial graph code.
Now you have the code to reproduce the graph.
Example:
Assume that you have run the following
sysuse auto, clear
histogram mpg
Then click on the Start Graph Editor icon and pressed the Start recording icon. Then altered the color of the histogram bins.
Then stop the recorder and saved the record on the hard disk with a suitable name and path. Then opened the record (just saved) in
Stata's do editor.
the line:
plotregion1.plot1.style.editstyle area(shadestyle(color(gs7))) editcopy
was retreived and gr_edit added to the from of this.
the complete file would look like:
sysuse auto, clear
histogram mpg
gr_edit plotregion1.plot1.style.editstyle area(shadestyle(color(gs7))) editcopy
this is run and will produce the original graph complete with the edit.
Alternatively you could save the recording and include it as follows:
sysuse auto, clear
histogram mpg, play(hist1) //hist1 is the name of the recording
Also see:
http://www.stata.com/statalist/archive/2008-07/msg00932.html
help graph play
Weaving Stata results into a Word Report (April 2009)
It is possible to put results from Stata into a word document by first obtaining your data in Stata
and then using mail merge to get this into Word.
For example, if you wish to automate you report writing and required the max. and min. mpg in a Word report
(using the auto.dta data set that comes with Stata )
this can be done with the following do file:
The user written program moremata is used this must first be installed. To install type the following on the Stata command line eg.
ssc install moremata
Once installed run the following Stata do file is run
********************weaving do file*********************************
sysuse auto, clear
*determine max and min mpg
quietly: sum mpg
local max_mpg =r(max)
local min_mpg =r(min)
di `max_mpg' //only if required to see results in Stata
di `min_mpg' //only if required to see results in Stata
mata
a="max_mpg"\st_local("max_mpg")
a1="min_mpg"\st_local("min_mpg")
a2=a,a1
a2
mm_outsheet("c:/tips.txt", a2, mode="r")
end
********************weaving do file*********************************
After running the above the text file tips.txt is produced (in C:/ drive)
Then in your MS Word report include the following:
The maximum value of mpg is: {MERGEFIELD "max_mpg"}
The minimum value of mpg is: {MERGEFIELD "min_mpg"}
Open the data source in Word and then run Mail Merge
After running mail merge your report should look like:
The maximum value of mpg is: 41
The minimum value of mpg is: 12
Using this method you can include tables, graphs etc. into your Word document.
References:
http://ideas.repec.org/p/boc/asug05/14.html
Also look at:
findit texdoc
findit esttab
findit estout
Stopping Stata during the running of a do file (March 2009)
When running a do file you may wish to inspect the data at various points. Stata has a number of way of doing this. For example:
Option 1:
Using the edit command. Opens the data editor and allows you to inspect the data. When the editor is closed
the do file continues to run. (Instead of edit you could have used browse to open the data browser)
sysuse auto, clear
regress mpg weight
edit //stops Stata and opens the data edit window
summarize
exit
Options 2:
Stopping Stata by using the more command
sysuse auto, clear
regress mpg weight
more
summarize
exit
Options 3:
sleep stops Stata for a specified number of milliseconds
sysuse auto, clear
regress mpg weight
sleep 1000 //sleep specifies the number of milliseconds to wait
beep //used to wake you up if the sleep is too long
summarize
exit
Options 4:
exit stops a do file. To run more of the do file move the exit command down the do file and run again.
sysuse auto, clear
regress mpg weight
local a 1
exit //program stop at this point then move to another line and run again
display `a'
For more information see:
help edit
help browse
help more
help exit
Putting Greek symbols in graphs (February 2009)
Greek symbols (or other symbols) can be added to Stata graphs. To add these you must first set up your computer for this eg.
In Windows XP
Click on the start button (bottom left hand side of screen)
Click on the Control Panel
Click on Regional and language option
Click on the Advanced tab
Select Greek (or another language with you require this)
then click Apply and then OK (the computer will then be required to be restarted )
then in Stata:
using the pull down menu:
Edit>Preferences>Graph Preferences
Then font select Arial Greek
To see the numbers used in the extended code you can use the Nick Cox written graph:
asciiplot
(this is a user written command and must first be downloaded)
To download asciiplot type the following on the Stata command line
ssc install asciiplot
then for example, type:
scatter weight mpg, title( Example of Greek characters in a Graph `=char(238)' `=char(243)' `=char(236)' )
Or the Stata graphics Editor can be used to include Greek symbols
For more information see:
Data Management Manual: char(n)
For an article on char()
See http://www.stata-journal.com/sjpdf.html?articlenum=dm0006
Doing things by levels of a variable (January 2009)
Using Stata's bysort prefix command
bysort is a Stata prefix command that allows you to execute commands by levels/groups of the variable(s) that you specify
Example:
If you wanted to generate a new variable with a 1 at the first occurrence of each level of mpg the following can be used (using the auto data set that comes with Stata):
sysuse auto, clear //load the auto data set into Stata
bysort mpg: gen first=1 if _n==1
If you wanted to generate a new variable with a 1 at the last occurrence of a level of mpg the following can be used:
sysuse auto, clear //load the auto data set into Stata
bysort mpg: gen last=1 if _n==_N
Sorting within the group eg. if you wanted the car with the smallest weight within each level of mpg the following can be used:
bysort mpg (weight): gen first_low_weight=1 if _n==1
Note that the brackets around the weight variable name indicates to Stata that this is not be used as
the level/group criteria but weight is to be sorted within each level of mpg
For more information see:
Stata 10 Data Management manual
Online help bysort
Online help for other prefix commands: help prefix
help _n
help _N
Automation of Tables in Stata (December 2008)
A tutorial showing different options for the automatic production of tables can be obtained by the following commands:
ssc install tabletutorial
to install and then
help tabletutorial
Memory usage in Stata (November 2008)
Stata generally stores all of the dataset that it is working with, in the computer's memory.
Therefore, the computer should have sufficient RAM to load all of the data.
Storing data in memory allows fast access to the data. If the computer has insufficient memory and the operating system allows, the
data is stored on the computer hard disk, however this can be very slow ie. Stata uses virtual memory where the operating system allows
Stata assigns an amount of memory for it's self so that it can store the data in RAM, so whatever this is set
to must be sufficient to store the entire data set. The memory settings in Stata can be changed to allow
sufficient memory for the data set.
What is sufficient memory?
To determine this the online calculator can be used
online calculator
A quick way to determine the average width of the variable ( bytes) is as follows:
(type the following on the command line or into a do file:)
describe
display r(width)/r(k)
Then put this number (average variable width) into the online calculator. The result from the online calculator
is the minimum memory required so allow 30-50% more then this for additional variables etc.
then set the memory using the set memory command eg. set memory 50m
Other useful Stata memory commands are:
compress
memory
References
http://www.stata.com/statalist/archive/2005-07/msg00348.html
Stata Comment (October 2008)
Stata has a number of ways of adding comments to Stata code. Some of these are:
*
The star at the start of a line tells Stata to ignore what follows eg.
*this is ignored
/* */
The /* */ are used to add comments between code eg.
regress mpg /* weight is the independent variable */weight
or /* */ can be used to concatenate two lines of code eg.
twoway scatter (mpg weight) /*
*/ (lfitci mpg weight)
///
Stata ignores what is after /// and continues on the next line eg.
regress mpg /// dependent variable
weight
The #delim ; command is useful in a number of ways. One use is to comment out blocks of code/text eg.
#delimit ;
*
display "this is a comment"
display "this is a comment"
display "this is a comment"
*;
#delimit cr
di "this is the end"
exit
the lines between #delimit; and #delimit cr are ignored
For further information on comments type help comments
For further information on #delimit type help delimit
Stata user written graphs (September 2008)
New graphs have been added.
Apart from the official Stata graphs many users have written special graph commands and have made these available for download.
To see just some of these click
More user written graph will be added in the future.
To see how these have been written (the code) use Stata's viewsource command.
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:
- Clicking on the command; if a single command is required
- If more than one command is required. Holding down the shift key and select the commands
- If all the commands that are currently in the Review Window are required then right click and and press select all
Then: Right clicking the mouse button and selecting send to do-file editor
The Do editor will then open with the highlighted command(s)in it. To run this using the Do Editor pulldown memu select: Tools>Do or using the icon (in Stata 10 this is the icon on the far right) or save this file and run from the command line eg. Saving this as c:/dofile and run by typing do c:/dofile on the Stata commandline.
For more details the Stata command type the following on the Stata commandline:
help do
Creating a Stata dataset from multiple Excel worksheets (June 2008)
There are a number of ways of doing this:
- odbc
- Stat/transfer
- Stata with the append command
In this example the Excel file is called book2 and is in c:/ drive. The file has two work sheets: kk1 and kk2
odbc
clear
tempfile kka
odbc load, dsn("Excel Files;DBQ=c:\book2.xls") table("kk1$")
save `kka'
list
clear
odbc load, dsn("Excel Files;DBQ=c:\book2.xls") table("kk2$")
list
append using `kka'
list
exit
Also see:
http://www.ats.ucla.edu:80/stat/stata/faq/odbc.htm
Using Stat/Transfer 9
With Stat/Transfer this would be done as follows:
Open tab: option 3
And then tick "concatenate worksheet pages"
Stata with the append command
Save each Excel worksheet as a csv in Excel. In this example c:/book2_kk1.csv and c:/book2_kk2.csv are the two files created
insheet using c:/book2_kk1.csv, clear
save c:/book2_kk1
list
clear
insheet using c:/book2_kk2.csv, clear
list
append using c:/book2_kk1
list
For more details the Stata commands type the following on the Stata commandline:
help append
help insheet
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:
Download the tutorial (do file)
Open the do file in the Stata do editor
Highlight an example
click on the do icon
Look at the result in the Results window - If you wish to confirm that you understand what is happening
in the example make some changes to the example and check that the results are as you would expect.
Tutorial 1 - do files
Tutorial 2 - macros
Tutorial 3 - loops
Tutorial 4 - if statement
Tutorial 5 - incrementing, _n, and _N
Tutorial 6 - local extended macros
More tutorials will be added in the following weeks
Also see:
Stata 10 Users Guide
Stata 10 Programming Manual
The Stata Journal (2005) Nicholas J. Cox "Suggestions on Stata programming style" 5, Number 4, pp. 560-566 Nicholas J. Cox
The Stata Journal (2002) Nicholas J. Cox "How to face lists with fortitude" 2, Number 2: pp. 202-222 click here Nicholas J. Cox
Stata Netcourse NC151 "Introduction to Stata programming"
Stata Netcourse NC152 "Advanced Stata programming"
(Back issues of the Stata journal can be purchase from Survey Design and Analysis - contact details below)
(To enroll in a Stata Netcourse please contact us)
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:
Download the tutorial (do file)
Open the do file in the Stata do editor
Highlight an example
click on the do icon
Look at the result in the Results window - If you wish to confirm that you understand what is happening
in the example make some changes to the example and check that the results are as you would expect.
Tutorial 1 Getting Data in Mata
Tutorial 2 Looping, If statement and examples
Tutorial 3 Subscripting matrics
Tutorial 4 string and numerical matrices, getting a mata matrix into Stata
Tutorial 5 Mata functions
Tutorial 6 Mata pointers and Mata optimize
Tutorial 7 Mata matrix maths and Solving simultaneous equation
Also see:
Stata 10 Mata manuals (The entire Mata manual can be found in Stata's online help for Mata eg. help Mata
The Stata Journal (2007) William Gould (2004) "Mata Matters: Structures", 7, Number 4, pp. 556 – 570
The Stata Journal (2007) William Gould (2004) "Mata Matters: Subscripting", 7, Number 1, pp. 106 – 116
The Stata Journal (2006) William Gould (2004) "Mata Matters: Precision", 6, Number 4, pp. 550 – 560
The Stata Journal (2006) William Gould (2004) "Mata Matters: Interactive use", 6, Number 3, pp. 387 – 396
The Stata Journal (2006) William Gould (2004) "Mata Matters: Creating new variables–sounds boring, isn't", 6, Number 1, pp. 112 – 123
The Stata Journal (2005) William Gould (2004) "Mata Matters: Using views onto the data", 5, Number 4, pp. 567 – 573
The Stata Journal (2005) William Gould (2004) "Mata matters: Translating Fortran.", 5, Number 3, pp. 421 – 441
(Back issues of the Stata journal can be purchase from Survey Design and Analysis - contact details below)
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
this can be simplified to one line eg.
generate dummy1= mpg <=25
this works because mpg <=25 is either true or false. Stata qualifiers evaluates to 1 if true and 0 if false.
If the variable that is part of the qualifier contains missing values then include the if condition: !=missing() eg.
generate dummy1= mpg <=25 if !=missing(mpg)
Other ways of creating dummy variables can be found at:Stata FAQ
Also see:
What is true and false in Stata?
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
Setup
. sysuse auto, clear
Create highrep78 containing the value of rep78 if rep78 is equal to 3, 4, or 5, otherwise highrep78 contains
missing (.)
. egen highrep78 = anyvalue(rep78), v(3/5)
List the result
. list rep78 highrep78
To see a description of examples type the following on the Stata command line when online
ssc describe examples
To install examples, type the following on the Stata command line when online
ssc install examples
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:
- Being able to reuse do files (that you have previously used an know that have no bugs) for other projects
- Stata doesn't have a "goto line X" command. However if you break down your analysis into do files the same thing can be achieved.
- Allows an quick overall view of the analysis
- Easier to debug smaller do files than large do files
- Some do files can be run (no output to the screen) and others can you can do (output to the screen). This is easier then using the Stata's quiet command
Disadvantages:
To learn more see: Stata 9 Users guide [U] 16.2 and [U]16.6.2
Max. depth of nested do files, in Stata type help limits
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.
See also the user written command: tr
To install this command: ssc install tr
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
by using the regular expression:
gen day=regexs(1) if regexm(date, "(^[0-9]+)")
|
date
|
day
|
|
12jan2003
|
12
|
|
1april1995
|
1
|
|
17may1977
|
17
|
|
02september2000
|
02
|
For a FAQ on regular expression go
here
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 7
1 7
1 7
1 7
1 7
1 7
1 8
1 8
2 8
2 8
2 1
bysort id: gen running _tot=sum(var2[_n]!=var2[_n-1])
further 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.
|