STATA为什么返回未知功能+ inrange()”?

发布于 2025-02-01 14:12:07 字数 722 浏览 5 评论 0原文

我正在研究Stata编程的书籍 STATA编程简介,第二版

在第4章中,有代码生成一个测试其他一些变量是否满足逻辑条件的变量,代码就像:

foreach v of varlist child1-child12{
        local n_school "`n_school' + inrange(`v', 1, 5)"
    }
    gen n_school = `n_school'

当我更改此代码以适合自己的数据时,

foreach v of varlist qp605_s_1-qp605_s_5 {
        local n_med  "`n_med' + inrange(`v', 1, 5)"
    }
    gen n_med = `n_med'

其中qp605_s_1 < /code>的值范围从1到17,然后stata返回:

. foreach v of varlist qp605_s_1-qp605_s_5 {
  2.         local n_med "`n_med' + inrange(`v', 1, 5)"
  3. }

. gen n_med = `n_med'
unknown function +inrange()
r(133);

此代码有什么想法?

I am studying Stata programming with the book An Introduction to Stata Programming, Second Edition.

In chapter 4 there is code to generate a variable that tests whether some other variables satisfy a logical condition, the code is like:

foreach v of varlist child1-child12{
        local n_school "`n_school' + inrange(`v', 1, 5)"
    }
    gen n_school = `n_school'

When I change this code to suit my own data,

foreach v of varlist qp605_s_1-qp605_s_5 {
        local n_med  "`n_med' + inrange(`v', 1, 5)"
    }
    gen n_med = `n_med'

where qp605_s_1's values range from 1 to 17, then Stata returns:

. foreach v of varlist qp605_s_1-qp605_s_5 {
  2.         local n_med "`n_med' + inrange(`v', 1, 5)"
  3. }

. gen n_med = `n_med'
unknown function +inrange()
r(133);

Any ideas what is wrong with this code?

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

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

发布评论

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

评论(2

·深蓝 2025-02-08 14:12:07

我看到我在哪里误解了

本地n_med+开始,因此我将其更改为:

local n_med 0
foreach v of varlist qp605_s_1-qp605_s_5{
    local n_med "`n_med' + inrange(`v', 1, 5)"
}
gen n_med = `n_med',after(qp605_s_5)

它可以使用!

顺便说一句,根据的stata编程简介,此方法比您首先生成一个均为零的变量,然后replot> replace>替换它通过循环,因为替换命令比生成要慢,因此最好避免替换

I see where I was wrong

The local n_med begins with +, so I change it to:

local n_med 0
foreach v of varlist qp605_s_1-qp605_s_5{
    local n_med "`n_med' + inrange(`v', 1, 5)"
}
gen n_med = `n_med',after(qp605_s_5)

and it works!

BTW, according to An Introduction to Stata Programming, this method is faster than if you first generate a variable which is all zero and then replace it by a loop, because the replace command is slower than generate, so it is better to avoid replace.

水溶 2025-02-08 14:12:07

这是另一种方法。

* Example generated by -dataex-. For more info, type help dataex
clear
input float(var1 var2)
1 5
2 6
3 7
4 8
end

gen wanted = .

mata :

data = st_data(., "var*")

st_store(., "wanted", rowsum(data :>= 1 :& data :<= 5))

end

list

     +----------------------+
     | var1   var2   wanted |
     |----------------------|
  1. |    1      5        2 |
  2. |    2      6        1 |
  3. |    3      7        1 |
  4. |    4      8        1 |
     +----------------------+

Here is another approach.

* Example generated by -dataex-. For more info, type help dataex
clear
input float(var1 var2)
1 5
2 6
3 7
4 8
end

gen wanted = .

mata :

data = st_data(., "var*")

st_store(., "wanted", rowsum(data :>= 1 :& data :<= 5))

end

list

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