Learning Stata

Collecting stata commands that are very useful but not used frequently, namely, those I can not remember well. It will get very long ...
*This post was editted by * Maxico (a Markdown *based editor for * Evernote *) first, then copied here. * The Maxico version looks much better.

  • V - Commands regarding Variables
  • M - Macros, i.e., local and global
  • F - Read, import, or export Files
  • SSC - Commands from SSC
  • F - Useful Functions

V

Variable Labels

Transfer a variable label to another

  • Copy a variable label to a local
    • local lbl: variable label oldvar
    • Pass to another: label var newvar "`lbl'"
  • An old version: _crcslbl newvar oldvar
    • This one only copies the variable label
  • A new vewsion: copydesc oldvar newvar
    • This one copies the descriptive properties too

-- By Nicholas J. Cox

Copy variable labels before collapse

A systematic way to do this is with a foreach loop.

foreach v of var * {
  local l`v' : variable label `v'
    if `"`l`v''"' == "" {
        local l`v' "`v'"
        }
    }

Then after reshape check all variable labels and replace blanks by any saved labels:

foreach v of var * { 
    local L`v' : variable label `v' 
        if `"`L`v''"' == "" & `"`l`v''"' != "" { 
            label var `v' `"`l`v''"' 
    }
} 

-- By Nicholas J. Cox

M

Use of a global

  • Defining a global is just as defining a local global gl, but accessing to it one has to put a **** in front of it, i.e., ```gl ```
  • When a global is used with a local, "{ }" must be used. For example, let local i be 6, and gl be "var", the code di "$gl`i'" will not return var6 but 6 instead. One has to type
di "${gl`i'}"

F

Import data from other sources (New in STATA 2013)

Import is a new command in State 13. It can import -excel- file directly. It also replace the insheet using command.

Import from Excel file

This needs some special notes because it is really a great update for STATA 13.0.

  • Command: import excel using
    • cellrange([start]:[end]): There should be no blank after colon
    • firstrow should always be specified because it is neat! It does not have to be the first row in the sheet, but the first row in the range defined in the command. When firstrow has been specified, blanks will be removed for naming a variable, and will be kept for variable labels automatically.

Import from a delimited text file

  • Command: import delim using: rowrange, colrange, nummericc are all useful

SSC commands

sec family; ssc hot

  • unique: module to report number of unique values in variable(s)
  • labutil: modules for managing value and variable labels
  • tabout: module to export publication quality cross-tabulations

F

String Functions

destring

  • It is a command actually, but more used to replace real().
  • It is quite safe because string variables will not be transferred into numeric if destring detects any non-numeric records. Records of "." will be treated directly into missing values though. Neat!
  • Use option ignore() to deal with the delimiter ``,''. Note this option can handle more than one arguments, e.g., ignore("," "N.A." " ").

John Mullahy發(fā)推征集到的五個(gè)最有用的stata命令

_n subscripting
assert
byhist: makes nice categorical histograms in a single panel.

capture
catplot
cmp
char
clonevar
cluster
collapse
contract
coefplot
cond()
contract

distinct (# of unique cases)

egen flag = tag(group)
even
estout
esttab
eststo/esttab

fasterxtile
factor notation: i.[varname] -dummy coding is a breeze
fre
ftools
fvvarlist

glm
gr_edit
gtools
gcollapse
greshape

heatplot (I need to break my Ben Jann theme)

inlist (tag values within a variable)
isid

khb
kdensity & akdensity

labmask:is great for replacing strings with labeled intergers for i. or reducing file sizes.
levelsof is great more making lists of values of string variables.
lookfor

margins
mat def & nullmat()
mats
mata
mdesc
merge
meglm
metan
missing
mlab
motivate
mod(), and all the extended_fcn macros!
mvdecode

numlabel, add

over / by

pause (if you teach)
post
program
putexcel: is great for making custom reproducible tables in excel (plus excel to latex extension for latex).

recast strL
reghdfe
rifreg (& now generalised to rifhdreg

set more off
set scheme X, where X is anything but the default baby blue border abomination.

shell python
shell matlab
shell R
shell cp
shell

strpos
strmatch (string function)
svy: commands
set scheme
"set trace on"(When combined with the classic green font, black background, it makes everything look like The Matrix. Fine, I guess it also helps with debugging code by showing exactly where things go wrong)
svyset
stset
stcox

tabulate
tabout2
tex/webdoc (sometimes markstat instead)
texdoc
tempfile/tempvar
tab1
teffects
tokenize
tw
tuples - all combinations of a list

vplot
xtnbreg

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末嗅辣,一起剝皮案震驚了整個(gè)濱河市胆屿,隨后出現(xiàn)的幾起案子饲齐,更是在濱河造成了極大的恐慌兰伤,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,723評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件放棒,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)悉默,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)溺健,“玉大人麦牺,你說(shuō)我怎么就攤上這事”掮裕” “怎么了剖膳?”我有些...
    開(kāi)封第一講書人閱讀 152,998評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)岭辣。 經(jīng)常有香客問(wèn)我吱晒,道長(zhǎng),這世上最難降的妖魔是什么沦童? 我笑而不...
    開(kāi)封第一講書人閱讀 55,323評(píng)論 1 279
  • 正文 為了忘掉前任仑濒,我火速辦了婚禮,結(jié)果婚禮上偷遗,老公的妹妹穿的比我還像新娘墩瞳。我一直安慰自己,他們只是感情好氏豌,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,355評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布喉酌。 她就那樣靜靜地躺著,像睡著了一般泵喘。 火紅的嫁衣襯著肌膚如雪泪电。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 49,079評(píng)論 1 285
  • 那天纪铺,我揣著相機(jī)與錄音相速,去河邊找鬼。 笑死鲜锚,一個(gè)胖子當(dāng)著我的面吹牛突诬,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播芜繁,決...
    沈念sama閱讀 38,389評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼攒霹,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了浆洗?” 一聲冷哼從身側(cè)響起催束,我...
    開(kāi)封第一講書人閱讀 37,019評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎伏社,沒(méi)想到半個(gè)月后抠刺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體塔淤,經(jīng)...
    沈念sama閱讀 43,519評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,971評(píng)論 2 325
  • 正文 我和宋清朗相戀三年速妖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了高蜂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,100評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡罕容,死狀恐怖备恤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情锦秒,我是刑警寧澤露泊,帶...
    沈念sama閱讀 33,738評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站旅择,受9級(jí)特大地震影響惭笑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜生真,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,293評(píng)論 3 307
  • 文/蒙蒙 一沉噩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧柱蟀,春花似錦川蒙、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,289評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至痰哨,卻和暖如春胶果,著一層夾襖步出監(jiān)牢的瞬間匾嘱,已是汗流浹背斤斧。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,517評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留霎烙,地道東北人撬讽。 一個(gè)月前我還...
    沈念sama閱讀 45,547評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像悬垃,于是被迫代替她去往敵國(guó)和親游昼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,834評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容