如何在Stata中的宏中调用宏

发布于 2025-02-09 04:02:45 字数 194 浏览 1 评论 0原文

这是我的代码:

local father_yes "c2[_n+`i']==1"

foreach i of numlist 1/15{
   replace withfa=1 if "`hz_father_yes'"
}

然后,我发现在我定义的rath_yes宏中无法识别循环的本地宏`i'。我该如何确定它?

Here is my code:

local father_yes "c2[_n+`i']==1"

foreach i of numlist 1/15{
   replace withfa=1 if "`hz_father_yes'"
}

Then I found the local macro `i' of the loop cannot be identified within the father_yes macro I defined. How can I get it identified?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖风昔人 2025-02-16 04:02:46

这里的主要问题是,当您定义第一个宏时,本地宏尚未定义,因此定义是

local father_yes "c2[_n+]==1"

在那时并非违法的,而是您想要的。

foreach在那里是合法的,但是我会重写这一点

forval i = 1/15 {
    replace withfa = 1 if c2[_n + `i'] == 1 

}

,因为在您的代码

  1. 中,本地宏authod> auth_yes如果定义为定义,即使更正不必要的

  2. 本地宏hz_father_yes未定义

  3. 表单的比较,如果“ string ” 是非法的,因为字符串不被视为true或true或false。

The main problem here is that when you define the first macro, local macro i is not defined, so the definition is then

local father_yes "c2[_n+]==1"

which is not illegal at that point, but is not what you want.

foreach is legal there, but I would rewrite this as

forval i = 1/15 {
    replace withfa = 1 if c2[_n + `i'] == 1 

}

noting that in your code

  1. the local macro father_yes would be illegal if used as defined, and even if corrected remains unnecessary

  2. the local macro hz_father_yes is not defined

  3. comparisons of the form if "string" are illegal, as strings are not regarded as true or false.

山川志 2025-02-16 04:02:45

我不确定您要做什么,但似乎问题在于您在定义它之前引用`i''

除此之外,您可能还会在使用`HZ_FATHER_YES'作为字符串中,将其放入字符串引号“”时,您可能还会遇到错误。

这项工作会做:

foreach i of numlist 1/15 {
  local father_yes "c2[_n+`i']==1"
  replace withfa=1 if `hz_father_yes'
}

但是我认为这样做会更干净:

foreach i of numlist 1/15 {
  replace withfa = 1 if c2[_n+`i']==1
}

如果这不适合您,请告诉我们您会遇到什么错误。

I am not sure what you are trying to do but it seems as if the issue is that you are referencing `i' before you define it.

Besides that you are probably also getting errors as you are using `hz_father_yes' as a string by putting it inside string quotes "".

Would this work:

foreach i of numlist 1/15 {
  local father_yes "c2[_n+`i']==1"
  replace withfa=1 if `hz_father_yes'
}

But then I think it would be cleaner to do:

foreach i of numlist 1/15 {
  replace withfa = 1 if c2[_n+`i']==1
}

Let us know what error you get if this does not work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文