【问题标题】:How to iterate plot over different variables?如何迭代不同变量的绘图?
【发布时间】:2019-07-18 17:19:57
【问题描述】:

我有以下代码:

hist house1 if house1 >0 & house1 <200000, bin(25) fraction by(Year) 

graph export house1.png, replace

我想用car1bed1 替换house1 来迭代它,而无需复制粘贴代码和替换,或者至少类似:

var = "house1"

hist var if house1 >0 & house1 <200000, bin(25) fraction by(Year) 

graph export var.png, replace

这样我就可以只更改分配给var 的值。

【问题讨论】:

  • 欢迎来到 Stack Overflow。请阅读Stata tag wiki,了解如何在此处提出 Stata 相关问题的建议。
  • 我们不知道如何从字面上看你的例子,但你暗示相同的限制适用于完全不同的变量。 @PearlySpencer 直接以你的例子为例,而不是试图猜测你真正想要什么,这是非常合理的。

标签: iteration stata stata-macros


【解决方案1】:

一个简单的foreach 循环将起作用:

foreach x in house1 car1 bed1 {
    display "hist `x' if `x' >0 & `x' <200000, bin(25) fraction by(Year)"
    display "graph export `x'.png, replace"
}

hist house1 if house1 >0 & house1 <200000, bin(25)fraction by(Year)
graph export house1.png, replace
hist car1 if car1 >0 & car1 <200000, bin(25)fraction by(Year)
graph export car1.png, replace
hist bed1 if bed1 >0 & bed1 <200000, bin(25)fraction by(Year)
graph export bed1.png, replace

这里的x 是一个本地宏,它获取foreach 中指定的值。

请注意,display 命令用于说明,并非必需。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多