我有自动数据集,想要创建几个条形图:
sysuse auto, clear
local mpg "22 20 17"
local titles "Title1 Title2 Title3"
local path "twentytwo twenty seventeen"
foreach x of local mpg {
foreach y of local titles {
foreach z of local path {
keep if mpg==`x' & foreign==0
egen hv_rank=rank(price)
# delimit ;
graph bar price,
over (make, sort(hv_rank) reverse label(labsize(vsmall)))
ytitle("")
horizontal title("`y'", size(medium))
;
# delimit cr
graph save "$dir_gphs\mpg`z'f0-bal.gph", replace
drop hv_rank
sysuse auto, clear
}
}
}
我不想为我的3个当地人的“价值”的每个可能组合创建一个条形图,而是我想拥有if x=22
,然后是y=Title1
然后{{1 }}。同样地z=twentytwo
然后if x=20
和y=Title2
。
这一定是一个简单的问题。我猜我到目前为止的搜索并没有给我带来任何有用的结果,因为我不知道问题的正确词汇。
答案 0 :(得分:2)
以下是我如何处理这个问题。
. local mpg 22 20 17
. local titles `" "Title 1" "Title 2" "Title 3" "'
. local path twentytwo twenty seventeen
.
. forvalues i = 1/3 {
2. local x : word `i' of `mpg'
3. local y : word `i' of `titles'
4. local z : word `i' of `path'
5. display `" `x' --- `y' --- `z' "'
6. }
22 --- Title 1 --- twentytwo
20 --- Title 2 --- twenty
17 --- Title 3 --- seventeen
或者
. local set1 22 "Title 1" twentytwo
. local set2 20 "Title 2" twenty
. local set3 17 "Title 3" seventeen
. forvalues i = 1/3 {
2. local x : word 1 of `set`i''
3. local y : word 2 of `set`i''
4. local z : word 3 of `set`i''
5. display `" `x' --- `y' --- `z' "'
6. }
22 --- Title 1 --- twentytwo
20 --- Title 2 --- twenty
17 --- Title 3 --- seventeen
答案 1 :(得分:1)
正如你所说,你真的想要一个循环。意识到这取决于经验而不是找到一些文档。
我无法对此进行测试,因为它取决于您的本地目录结构和未定义的全局宏,因此您的示例不可重现。我做了一些偶然的简化。
如果您的各个元素包含空格,则需要双引号来绑定。
sysuse auto, clear
forval j = 1/3
local x : word `j' of 22 20 17
local title: word `j' of Title1 Title2 Title3
local path: word `j' of twentytwo twenty seventeen
graph bar price if mpg==`x' & foreign==0 ///
over(make, sort(1) reverse label(labsize(vsmall))) ///
ytitle("") horizontal title("`title'", size(medium))
graph save "$dir_gphs\mpg`path'f0-bal.gph", replace
}