Reasonable Easy commands

12 views
Skip to first unread message

Linfeng Li

unread,
May 13, 2013, 6:07:47 PM5/13/13
to income-dynam...@googlegroups.com
1. keep id2 q50 

    This will dump all the rest variables;

2. cd "The_address_for_your_local_folder_keeping_datas_to_be_used"

    Copy paste your directory and put this on top of the do file. ==> can just call data file by its name ^.^
 
 cd "C:\Users\lenovo\Desktop\UMich\RA_Econ\Stata\text\hh99"
 
use "Deck 15", clear

And press ctrl+shift+E to get to the following "coding area" in Google group.
Enter code here...


Raj Arunachalam

unread,
May 23, 2013, 3:45:01 PM5/23/13
to income-dynam...@googlegroups.com
Hi all,
Great work today!  Here are some Stata tips that came to me as you were giving your Stata presentations.  Not all are based on the code you used, but I hope they will be helpful. Cheers!
--raj


1. Avoid putting spaces in directory names and file names.  Use underscore instead.  This avoids having to deal with quotes and even sometimes what Stata calls compound double quotes, which can be very confusing.

2a. -, replace- is handy, when saving datasets, tables, graphs. 

2b. However, careful when using in combination with -tempfile-.  -tempfile- creates a local telling Stata files to drop when the program ends.  This is useful especially when you need to create thousands of datasets.  But, be careful not to -save , replace- a tempfile.  For instance:

tempfile blah
save `blah' , replace

is fine, but if you accidentally mistype:
save `blha', replace

then Stata will evaluate `blha' to empty and read:
save , replace

and then write over your dataset.  Really, really bad.

3. It's hard to imagine -merge m:m- ever being useful.

4. Careful with using arithmetic operations instead of -egen blah=rowtotal()-.  Arithmetic operations will yield missings if any variable is missing, which is not necessarily desirable, particularly when summing different forms of income/wealth/etc.

5. Use -set more off- so you don't need to keep pressing spacebar as the program runs.

6.     -save blah, replace-
    -clear-
    -use blah-
is unnecessary.  This is one of those extraneous bits of code that seems totally harmless, but unfortunately it's not always, as you can get r(603) errors sometimes when opening and closing files.

7. Use /// or /*
*/ to make all lines of code visible in the do file.  (But, this is a matter of taste.)

8. Comment out -ssc install- after one run of the do file.  This can slow everything down considerably, depending on the internet connection.

9.     -keep x y-
    -collapse (mean) x, by(y)-
is unnecessary, since collapse will only keep those variables that you mention in the command. 

10. Due to Stata's multiple missing codes, use -if missing(var)- and -if !missing(var)- rather than -if var==.- and -if var!=.- 


Ajay Shenoy

unread,
May 24, 2013, 2:18:53 PM5/24/13
to income-dynam...@googlegroups.com
Hey, all,

On point 8: if you really want the code to be general so it runs on anyone's computer but doesn't slow things down if the needed program isn't installed, you can do the following (I'll use esttab as an example):

cap esttab,//...whatever your first esttab command is
if _rc!=0{
 ssc install estout
}


This will try to run esttab, and if Stata gives an error it will install it.  Otherwise it'll carry on without trying to install anything.
-@

Reply all
Reply to author
Forward
0 new messages