gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:01:11 26 4
gpt4 key购买 nike

我有一个简短的 .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

最佳答案

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

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

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

在决定什么是标记时,在这种情况下,这里的重要细节是 Stata 在空间上进行解析。因此,在您的示例中,标记 1 是 country,(包括逗号)等(假设 suffix_orig)

rename `1' `1'`suffix'

被解释为

rename country, country,_orig 

如果您set trace on 我预计您会看到这一行,这就是让您失望的原因。就 rename 而言,country(一个有效的变量名)后面的是逗号,它不是一个有效的变量名。

简短的总结很贴心:您在应该使用 varlist 的地方使用了对 1 的引用。

注意:虽然你的syntax语句确实指定了varname,一个单一的变量名,你键入的变量名仍然放在局部宏varlist

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

注意:syntax 在这里是无可指责的。

关于stata - 逗号在ado程序中通过语法varlist传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21709933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com