I have a program which does some calculation and saves some result matrices. I would like to use a sub-program which gets passed on some arguments from the main programm to name the columns and rows of the result matrices. In the best case, the value labels of the original variable in my dataset should be used for the row names of the matrices. However, I cannot figure out how to get the value labels from the original variable when I am passing on the variable. In the main program, I use syntax varname, rowvar(varname)
. Here is an example code:
*** Sub-program name matrix rows and cols ***
program namemat
version 6.0
args rowvar
tempname rowlab tmp_min tmp_max tmp_rowlab
mat def exmat = J(13,3,0)
qui sum `rowvar'
local tmp_min = r(min)
local tmp_max = r(max)
foreach i of numlist `tmp_min' / `tmp_max' {
local tmp_rowlab: label (`rowvar') `i'
local rowlab = `"`rowlab'"' + `""`tmp_rowlab'" "'
}
matrix colnames exmat = "col 1" "col 2" "col 3"
matrix rownames exmat = `rowlab'
mat list exmat
end
*** Use subprogram ***
sysuse nlsw88, clear
namemat occupation
How can I get the original value labels from the 13 occupations as rownames? In the next coding step, I would save value labels which are too long in an additional scalar which I would then store along with the matrices as rclass results.
答案 0 :(得分:1)
这对我有用:
*** Sub-program name matrix rows and cols ***
program namemat
version 6.0
args rowvar
mat def exmat = J(13,3,0)
sum `rowvar', meanonly
forval i = `r(min)'/`r(max)' {
local rowlab `"`rowlab' "`: label (`rowvar') `i''" "'
}
matrix colnames exmat = "col 1" "col 2" "col 3"
matrix rownames exmat = `rowlab'
mat list exmat
end
*** Use subprogram ***
sysuse nlsw88, clear
namemat occupation
编辑:您的问题是使用tempname rowlab
,这意味着本地宏rowlab
在生活中作为临时名称开始,如__000000
道德:不要使用{{1当你定义一个本地宏时。