sewind_tj 发表于 2013-1-21 16:47 
请问楼主:
egen varm=concat(var units scale), p(;)
token `"`v'"',p("' `")
concat 即concatenate,连接的意思,该函数属egen系列,
就是说,如果想详细了解的话,可以help concat,然后egen -> ctrl+f -> concat,即可找到相应的help file
即 concat(varlist) [, format(%fmt) decode maxlength(#) punct(pchars)] may not be combined with by. It concatenates varlist to produce a string variable. Values of string variables are unchanged. Values of numeric variables are
converted to string, as is, or are converted using a numeric format under the format(%fmt) option or decoded under the decode option, in which case maxlength() may
also be used to control the maximum label length used. By default, variables are added end to end: punct(pchars) may be used to specify punctuation, such as a
space, punct(" "), or a comma, punct(,).
功能就是合并(连接)几个变量,
比如有变量var, vardescription, units,scale
var | vardescription | units | scale |
NGDP_D | Gross domestic product, current prices | national currency | billions |
concat的option里有个 punct(pchars), 这个的作用就是设定连接几个变量所用的连接符,我用的是分号;
所以其结果就是
varm的在观测值上的值为Gross domestic product, current prices;national currency;billions
之所以要连接,是因为每个国家的变量描述vardescription里有重复的,而他们之间的区别在于度量单位units不同,或是规模/级别scale不同,如果用unique函数显示var中的不同值,有46个,但是vardescription 只有 30多个,
那么在重新给变量加标签的时候,就不足了,但是通过concat,合并vardescription,units,scale后,新生成的(用来给新变量v1-v46贴标签的)变量就有46个不同值啦。
所以这都是给后面加标签做铺垫。
以下是token的help -file:
[P] tokenize -- Divide strings into tokens 分隔字串为若干子字串
tokenize [[`]"][string]["[']] [, parse("pchars") ]
Description
tokenize divides string into tokens, storing the result in `1', `2', ... (the positional local macros). Tokens are determined based on the parsing characters pchars,
which default to a space if not specified.
Option
parse("pchars") specifies the parsing characters. If parse() is not specified, parse(" ") is assumed, and string is split into words.
之前在levelsof varm, local(v) 是把所有varm里的不同标签值存在v这个local macro 里(一整条长字串)
所以token `"`v'"',p("' `") 就是把 v这个local macro里的那一个长字串,根据parse设定的分隔符(' `)判定[默认分隔识别符为空格], 断开拆成一个个子字串(46个),并存在positional位置型local macro 1-46里面。看实例:
. levelsof varlab,l(v)
`"Current account balance;Percent of GDP;"' `"Current account balance;U.S. dollars;Billions"'
`"Employment;Persons;Millions"'
只截取部分,所以看到 每个断点 特征是 ` ' (即右单引号+空格+左单引号)
如果一起execute两条命令(因为是local macro)
levelsof varlab,l(v)
macro list
可以看到macro list里的v:
_v: `"Current account balance;Percent of GDP;"' `"Current account balance;U.S. dollars;Billions"'
`"Employment;Persons;Millions"' (截取部分)……
v里面是个长字串(由46个子字串构成,分隔判定是` ')
连续执行3条命令:
levelsof varlab,l(v)
tokenize `"`v'"',p("' `")
display "1=|`1'|, 2=|`2'|, 3=|`3'|"
得到:
1=|Current account balance;Percent of GDP;|, 2=|Current account balance;U.S. dollars;Billions|, 3=|Employment;Persons;Millions|
最后,
forvalues i=1/46{
label variable y`i' `"``i''"'
}
forvalues: loop over consecutive values
label variable varname ["label"]
那几个引用符就是为了
["label"] 这些个label两边要加双引号"label"的
` " ` `i' ' " '
所以一共4层:
1. `i' 提取label
2. `' 引用label这个字串
3. "" 加双引号
4. `' 引用整体(加完双引号的label字串)
另外用levelsof + token这个办法要么用于贴标签,要么用于换名字,
换名字的话:
指令为:(当然之前几步也有所变化)
forvalues i=1/46{
rename y`i' ``i''
}
不能兼顾,原因有以下几个:
1、token只存1-46一次[不能给这种positional local macro更名],第二次就覆盖掉了;
2、不过问题不在token,那个可以在后面forvalues i=1/4{local lab`i'=``i'' }然后加标签的时候
forvalues i=1/46{label variable y`i' `"``lab`i'''"'},问题在于levelsof,它存变量独立值的时候,自动用升序排序,所以如果标签和变量名的升序不一致的话,就乱了;
3、如果第二次reshape 的时候带着var[即i(...)里有var]做varlab的标签 +var对y1-y46的重命名,
那就会造成冗余项过多[即便是duplicates drop之后])
目前还没想到更好的办法实现标签+命名(标签是变量的详细描述,所以较之命名更重要些,所以我是先标签,后手动命名的)