input
我在Stata的命令中发现了这种奇怪的行为。
当您将本地宏作为一个变量或多个变量的参数传递时,该input
命令会给出以下错误:
'`' 不能读取为数字
以下是两个给出相同错误的示例:
clear
local nums 1 1 1
input a b c
`nums'
end
clear
local num 1
input a b c
1 1 `num'
end
有没有办法将宏传递给input
命令?
input
我在Stata的命令中发现了这种奇怪的行为。
当您将本地宏作为一个变量或多个变量的参数传递时,该input
命令会给出以下错误:
'`' 不能读取为数字
以下是两个给出相同错误的示例:
clear
local nums 1 1 1
input a b c
`nums'
end
clear
local num 1
input a b c
1 1 `num'
end
有没有办法将宏传递给input
命令?
这在本质上主要是对 Aaron Wolf 答案的评论,但是代码使它太笨拙,无法放入物理评论中。
给定本地的东西,另一种方法是
clear
local num "1 1 1"
set obs 1
foreach v in a b c {
gettoken this num : num
gen `v' = `this'
}
自然,有很多方法可以将 1 1 1 转换为三个变量。
这本身不会将宏传递给输入命令,但它确实达到了您想要的结果,所以也许这可以帮助您尝试做什么?
大体思路是将一个变量的值设置为一个局部,然后分割局部(类似于Excel中的文本到列按钮)。
clear
local nums "1 1 1"
foreach n of local nums {
if "`nums_2'" == "" local nums_2 "`n'"
else local nums_2 = "`nums_2'/`n'"
}
set obs 1
gen a = "`nums_2'"
split a, parse("/") gen(b) destring
drop a