1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
*! Example Stata script showing syntax highlighting
* This is a single-line comment
* This is a single-line comment
// This is also a single-line comment
gen foo = 3*3 // This is an in-line comment
/*
This is a multi-line
comment
*/
* Preprocessor command
#delimit ;
* Valid names
gen foo = 3
gen foo1 = 3
gen _myvar = 3
* Invalid names
gen 0foo = 3
gen ^foo = 3
gen foo% = 3
* Numbers and formats
di 12345678901
di 123456.78901
di 123e3
di 123E3
di 1.23e-3
di 1.23e3
di %5.3fc 32.32146
format foo %12.3gc
* Functions
gen foo = cos(bar)
gen foo = tan(bar)
gen foo = runiform()
gen foo = e(sample)
* Types and macros
gen byte foo = 4
gen int foo = 3
gen long foo = 3
gen float bar = 3.2
gen double bar = 3.2
gen str foo = "bar"
gen str132 foo = "bar"
gen str2045 foo = "bar"
gen strL foo = "bar"
confirm numeric 3
confirm string "foo"
confirm integer number 3
scalar foo = 3
matrix foo = 3
local foo = 3
global foo = 3
gen bar1 = `foo'
gen bar2 = $foo
* Invalid types
gen str0 foo = "bar"
gen str2046 foo = "bar"
* Control structures
if (`x'==3) {
display "foo"
}
else if (`x'==4) {
display "bar"
}
else {
display "foobar"
}
foreach i in "foo" "bar" {
di "`i'"
}
forval i = 1/5 {
di `i'
}
forval i = 1 5 to 10 {
di `i'
}
local i = 1
while (`i' < 5) {
local i = `i'+1
if (`i'<3) continue
if (`i' > 3) break
}
foreach v of varlist mpg weight-turn {
di "`v'"
}
foreach v of newlist newvar1 newvar2 {
di "`v'"
}
foreach v of numlist 1/5 12 {
di `v'
}
* Programs
program define myprog
di "`0'"
end
* Indexing
matrix input foo = (1,2,3,4\5,6,7,8\9,10,11,12)
matrix bar = foo[1..., 2..4]/2
matrix symmetric = (2,1\1,2)
matrix foobar = cholesky(0.1*I(rowsof(symmetric)) + 0.9*symmetric)
* Operators
di 2 - 2
di 2 + 2
di 2 * 2
di 2 / 2
di 2^2
di 1 & 0
di 1 | 0
di !1
di ~1
di 2 < 2
di 2 > 2
di 2 >= 2
di 2 <= 2
di 2 == 2
di 2 != 2
di 2 ~= 2
di 2 + 2 == 4
di 1 + (2 + 3)
reg y i.foo#c.bar
reg foo io(2 3 4).bar
gen foo = L3.bar
gen foo = L(1/3).bar
* Strings
gen foo = "hello, world!"
gen foo = `"hello, world!"'
gen foo = `"hello," world!"'
gen foo = "`mylocal'"
gen foo = `"`mylocal'"'
* Estimation and data manipulation commands
sysuse auto, clear
foreach v of varlist * {
cap confirm numeric var `v'
if _rc continue
gen imp_`v' = mi(`v')
label var imp_`v' "Imputed value for `v'"
summ `v', detail
replace `v' = r(p50) if mi(`v')
}
compress
foreach rhs in "mpg" "mpg weight" {
reg price `rhs' if foreign=="Domestic":origin, robust
reg price `rhs' if foreign=="Foreign":origin, robust
}
** EOF
|