📄 _mkvec.ado
字号:
*! version 1.1.3 10jan1999
program define _mkvec, sclass
/* Syntax:
_mkvec matname [, FROM(this:that=# that=# /this=# matrix #, COPY SKIP)
UPdate COLnames(string) ERRor(string) FIRST ]
*/
version 6
/* Parse. */
gettoken matout 0 : 0, parse(", ")
if "`matout'"=="" | "`matout'"=="," {
error 198
}
syntax [, FROM(str) UPdate COLnames(str) ERRor(str) FIRST ]
if "`update'"!="" {
if "`colname'"!="" {
di in red "update and colnames() cannot both be " /*
*/ "specified"
exit 198
}
capture local colname : colfullnames `matout'
if _rc {
di in red "matrix `matout' not found"
exit 111
}
if rowsof(matrix(`matout')) != 1 {
di in red "`matout' matrix to be updated must be " /*
*/ "a row vector"
exit 503
}
local matin `matout'
}
if `"`error'"'!="" {
local error `"`error': "'
}
tempname mat
if `"`from'"'!="" {
/* Split off [, COPY SKIP ] options from -from(..., copy skip)-. */
From `from'
local copy `s(copy)'
local skip `s(skip)'
/* Copy vector. */
if "`copy'"!="" {
CopyMat `mat' `"`error'"' `s(from)'
if `"`colname'"'!="" {
local dim : word count `colname'
if `dim' != colsof(matrix(`mat')) {
di in red `"`error'matrix must "' /*
*/ `"be dimension "' `dim'
exit 503
}
mat colnames `mat' = `colname'
}
}
/* Or build up vector. */
else {
BuildMat `mat' `"`error'"' "`skip'" `s(from)'
if `"`colname'"'!="" {
ChkNames `mat' "`matin'" `"`colname'"' /*
*/ `"`error'"' "`skip'" "`first'"
local fill `s(k_fill)'
}
}
}
else {
/* If here, there is no -from()-. */
local fill 0
if "`update'"!="" {
mat `mat' = `matin'
}
else if `"`colname'"'!="" {
local dim : word count `colname'
mat `mat' = J(1,`dim',0)
mat colnames `mat' = `colname'
}
else {
di in red "from() must be specified"
exit 198
}
}
sret clear
sret local copy `copy'
sret local skip `skip'
sret local k = colsof(matrix(`mat'))
if "`fill'"!="" {
sret local k_fill `fill'
}
else sret local k_fill = colsof(matrix(`mat'))
matrix rename `mat' `matout', replace
end
program define From, sclass /* split off COPY SKIP options */
while `"`0'"'!="" & "`token'"!="," {
gettoken token 0 : 0, parse(" ,")
if "`token'"!="," {
local from `from' `token'
}
}
sret clear
sret local from `from'
if `"`0'"'!="" {
local 0 , `0'
syntax [, COPY SKIP ]
}
sret local copy `copy'
sret local skip `skip'
end
program define CopyMat /* matname { matrix | # } ... */
args matall error
tempname matadd
local i 3
while "``i''"!="" {
capture di matrix(``i''[1,1])
if _rc == 0 {
ChkMat `matadd' ``i'' `"`error'"'
}
else {
capture confirm number ``i''
if _rc {
di in red `"`error'copy option requires "' /*
*/ "either a matrix or a list of numbers"
exit 198
}
mat `matadd' = J(1,1,``i'')
}
mat `matall' = nullmat(`matall') , `matadd'
local i = `i' + 1
}
end
program define BuildMat /* matname this:that=# that=# /this=# matrix */
args matall error skip
macro shift 3
tokenize `"`*'"', parse(" =")
tempname matadd
local i 1
while "``i''"!="" {
local k = `i' + 1
if "``k''"=="=" {
local j = `i' + 2
MkMat `matadd' ``i'' ``j'' `skip'
local i = `i' + 3
}
else {
ChkMat `matadd' ``i'' `"`error'"'
local i = `i' + 1
}
mat `matall' = nullmat(`matall') , `matadd'
}
end
program define ChkMat /* matname matrix */
args matout matin error
capture di matrix(`matin'[1,1])
if _rc {
capture confirm number `matin'
if _rc {
di in red `"`error'matrix `matin' not found"'
exit 111
}
di in red `"`error'list of numbers requires copy option"'
exit 198
}
if matrix(rowsof(`matin')!=1 & colsof(`matin')!=1) {
di in red "`matin' " matrix(rowsof(`matin')) /*
*/ " x " matrix(colsof(`matin')) " is not a vector"
exit 503
}
if matrix(rowsof(`matin')) == 1 {
mat `matout' = `matin'
}
else mat `matout' = `matin''
end
program define MkMat /* matname {[eq:]name | /name} # */
args mat el_name z skip
confirm number `z'
tokenize "`el_name'", parse(" :/")
if "`2'"==":" {
local eqname "`1'"
local colname "`3'"
}
else if "`1'"=="/" {
local eqname "`2'"
local colname _cons
}
else local colname "`el_name'"
if "`colname'" != "_cons" { /* must allow non-vars, but also unab */
if "`skip'"=="" {
local colname : tsnorm `colname', varname
cap tsunab colname : `colname', max(1)
}
else {
cap local colname : tsnorm `colname', varname
cap tsunab colname : `colname', max(1)
}
}
mat `mat' = J(1,1,`z')
if "`eqname'"!="" {
mat coleq `mat' = `eqname'
}
mat colnames `mat' = `colname'
end
program define ChkNames, sclass
args mat matin names error skip first
local dim = colsof(matrix(`mat'))
if "`first'"!="" { /* get first equation name */
local name : word 1 of `names'
local eq1 = substr(`"`name'"',1,index(`"`name'"',":"))
}
tempname new fill x
local k : word count `names'
if "`matin'"!="" {
mat `new' = `matin'
}
else {
mat `new' = J(1,`k',0)
mat colnames `new' = `names'
}
mat `fill' = J(1,`k',0)
local i 1
while `i' <= `dim' {
mat `x' = `mat'[1,`i'..`i']
local eq : coleq `x'
local name : colname `x'
local j = colnumb(`new',`"`eq':`name'"')
if `j'==. & `"`eq'"'=="_" & `"`eq1'"'!="" {
local j = colnumb(`new',`"`eq1'`name'"')
}
if `j'!=. {
if `fill'[1,`j'] {
if "`eq'"=="_" {
di in red `"`error'duplicate "' /*
*/ `"entries for `name' found"'
}
else di in red `"`error'"' /*
*/ `"duplicate entries for `eq':`name' found"'
exit 507
}
else mat `fill'[1,`j'] = 1
mat `new'[1,`j'] = `mat'[1,`i']
}
else if "`skip'"=="" {
if "`eq'"=="_" {
di in red `"`error'extra parameter `name' "' /*
*/ "found" _n "specify skip option if necessary"
}
else di in red `"`error'extra parameter "' /*
*/ `"`eq':`name' found"' _n /*
*/ "specify skip option if necessary"
exit 111
}
local i = `i' + 1
}
mat `fill' = `fill'*`fill''
sret clear
sret local k_fill = `fill'[1,1]
mat `mat' = `new'
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -