逗号在ado程序中通过语法varlist传递

时间:2014-02-11 18:20:28

标签: stata

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

但是syntax在字符串变量之后没有正确解析逗号。也就是说,它不是传递country而是传递country,而是抛出错误。

是否需要varname,之间的空格?或者我是否误解了我应该如何使用syntaxvarlist

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

1 个答案:

答案 0 :(得分:3)

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

您的程序存在的问题是您不使用本地程序。

相反,您使用名为1的本地宏。无论syntax如何,默认情况下,本地宏0是在命令名称后键入的整个命令行,而本地宏12等是第一个,第二,等命令行中的“令牌”。

在决定什么是令牌时,在这个上下文中,重要的细节是Stata解析空间。因此,在您的示例中,令牌1为country,(包括逗号)等等(假设suffix_orig

rename `1' `1'`suffix'

被解释为

rename country, country,_orig 

如果你set trace on我预测你会看到这一行,这就是抛出你的东西。就rename而言,country(有效变量名)后面的是逗号,它不是有效的变量名。

简短摘要很简单:您正在使用1的引用,无论您应该使用varlist

注意:虽然您的syntax语句确实指定了varname,但是您输入的变量名称仍然位于本地宏varlist中。

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

注意:syntax因此无可指责。