Stata 在ado程序中使用语法varlist传递逗号

Stata 在ado程序中使用语法varlist传递逗号,stata,Stata,我有一个简短的.ado文件来标准化来自多个源的字符串变量。程序接受一个字符串变量,用后缀重命名它,然后用标准化名称替换原始变量 但是语法不能正确解析字符串变量后的逗号。也就是说,它不是传递country而是传递country,并抛出一个错误 varname和,之间是否需要空格?或者我对应该如何使用语法和变量列表有误解吗 clear set obs 10 generate country = "United States of America" * runs fine without `suff

我有一个简短的.ado文件来标准化来自多个源的字符串变量。程序接受一个字符串变量,用后缀重命名它,然后用标准化名称替换原始变量

但是
语法
不能正确解析字符串变量后的逗号。也就是说,它不是传递
country
而是传递
country,
并抛出一个错误

varname
之间是否需要空格?或者我对应该如何使用
语法
变量列表
有误解吗

clear
set obs 10
generate country = "United States of America"

* runs fine without `suffix()` options
country_names country
list

/*
. list

     +------------------------------------------+
     |                country_0         country |
     |------------------------------------------|
  1. | United States of America   United States |
  2. | United States of America   United States |
  3. | United States of America   United States |
  4. | United States of America   United States |
  5. | United States of America   United States |
     |------------------------------------------|
  6. | United States of America   United States |
  7. | United States of America   United States |
  8. | United States of America   United States |
  9. | United States of America   United States |
 10. | United States of America   United States |
     +------------------------------------------+
*/

* but gives error with `suffix()` 
country_names country, suffix(_orig)

/*
. country_names country, suffix(_orig)
, invalid name
r(198);
*/

* `set trace on` reveals comma passed as part of `varlist`
set trace on
country_names country, suffix(_orig)

/*
. country_names country, suffix(_orig)
  ---------------------------------------------------------------------------------------------- begin country_names ---
  - version 11.2
  - syntax varname(string) [, Suffix(string) ]
  - quietly {
  - if "`suffix'" == "" local suffix "_0"
  = if "_orig" == "" local suffix "_0"
  - rename `1' `1'`suffix'
  = rename country, country,_orig
, invalid name
    generate `1' = proper(`1'`suffix')
    replace `1' = "United States" if inlist(`1', "United States Of America")
    local name: variable label `1'`suffix'
    label variable `1' "`name'"
    label variable `1'`suffix' "`name' (orig)"
    }
  ------------------------------------------------------------------------------------------------ end country_names ---
r(198);
*/

* if I leave space before comma, then program works
country_names country , suffix(_orig)
list

/*
. list

     +----------------------------------------------------------+
     |                country_0    country_orig         country |
     |----------------------------------------------------------|
  1. | United States of America   United States   United States |
  2. | United States of America   United States   United States |
  3. | United States of America   United States   United States |
  4. | United States of America   United States   United States |
  5. | United States of America   United States   United States |
     |----------------------------------------------------------|
  6. | United States of America   United States   United States |
  7. | United States of America   United States   United States |
  8. | United States of America   United States   United States |
  9. | United States of America   United States   United States |
 10. | United States of America   United States   United States |
     +----------------------------------------------------------+
*/
这是.ado文件

*! 0.1 Richard Herron 2/11/2014

/* use to standardize country names across several data sources */

program country_names
    version 11.2
    syntax varname(string) [, Suffix(string) ]

    quietly {

        /* default suffix */
        if "`suffix'" == "" local suffix "_0"

        /* save original as new variable w/ suffix */
        rename `1' `1'`suffix'

        /* first standardize capitalization */
        generate `1' = proper(`1'`suffix')

        /* -if- picks bad names from several sources */
        replace `1' = "United States" ///
            if inlist(`1', "United States Of America")

        /* fix labels */ 
        local name: variable label `1'`suffix'
        label variable `1' "`name'"
        label variable `1'`suffix' "`name' (orig)"

    }

    end

当且仅当您提供有效的变量名时,您的
语法
语句才会生成本地宏
varlist

你的程序的问题是你没有使用本地的

而是使用名为
1
的本地宏。无论
语法如何
,默认情况下,本地宏
0
是在命令名后键入的整个命令行,本地宏
1
2
等是命令行中的第一个、第二个等“标记”

在这个上下文中,在决定什么是令牌时,这里的重要细节是Stata在空间上解析。因此,在您的示例中,标记1是
国家、
(包括逗号)等等(假定
后缀是
\u orig

被解释为

rename country, country,_orig 
如果你
设置trace on
,我预测你会看到这一行,这就是让你扫兴的地方。就
rename
而言,
country
(一个有效的变量名)后面是逗号,它不是一个有效的变量名

简短的总结很贴切:您在应该使用
varlist
的任何地方都使用了对
1
的引用

注意:尽管您的
语法
语句确实指定了
varname
,但您键入的变量名仍然放在本地宏
varlist

注意:您可以通过总是在变量名后加空格来解决这个问题,但我强烈建议不要这样做


注意:
语法因此在这里是无可指责的

谢谢你的解释。或者我可以添加
标记化“varlist”
,对吗?下面的解释正确吗?
varlist
已经是
tokenize
d,但是在空格上而不是在有效的变量名上,因此
`1'
country,
。但是如果我
标记化
变量列表
(再次),它会删除后面的逗号。不客气。如果将程序中的
tokenize
应用于
varlist
,则本地宏
1
将包含您指定的唯一变量名。但为什么要这样做呢?您只需使用已包含该名称的
varlist
。否则就意味着不必要的语句(和奇怪的程序风格)。
rename country, country,_orig