2014-10-29 33 views
0

我想創建多個圖形並使用循環進行組合。我用下面的代碼:將多個圖形與循環結合使用

local var Connecticut Delaware Minnesota Missouri Rhode Island Tennessee Vermont Wisconsin Hawaii 
local n: word count `var' 
forvalues i=1/`n'{ 
local a: word `i' of `var' 
line prop_report_agencies modate if statename=="`a'" , ytitle(proportion_agency reports for `a') saving(gg`a',replace) 
local gg `gg' "gg`a'" 
} 
local gg: subinstr local gg "gg`a'" `""gg`a'""' 

gr combine `gg' 
graph drop _all 

當我這樣做,我得到這個錯誤

ggConnecticut is not a memory graph 

代碼的第一部分似乎工作:代碼創建單獨的圖形,並將其存儲;但是,由於錯誤,它不能combine

回答

1

首先,讓我們來糾正代碼,同時也從一個蜿蜒流收縮到一個直線:

local S Connecticut Delaware Minnesota Missouri "Rhode Island" Tennessee Vermont Wisconsin Hawaii 
foreach s of local S { 
    line prop_report_agencies modate if statename=="`s'", ytitle(proportion_agency reports for `s') saving(gg`s',replace) 
    local gg `"`gg' "gg`s'""' 
} 

gr combine `gg' 

順帶一提,請注意,您有一個bug爲Rhode Island是一種狀態,而是兩個話。

但是,我認爲,您的主要問題是" "作爲字符串分隔符的作用與您需要它們作爲combine調用中的文字字符之間存在衝突。要停止" "被剝離,您需要使用複合雙引號。你的代碼顯示你知道他們。複合雙引號是

`" "' 

這個例子是dopey,但Stata的用戶使用任何最新版本的Stata都可以重現。

sysuse census, clear 

local S Alabama "Rhode Island" 
foreach s of local S { 
    histogram medage if state == "`s'", saving("gg`s'", replace) 
    local gg `"`gg' "gg`s'""' 
} 

graph combine `gg' 

今後,使用

macro list 

在您的調試,看看是什麼問題。我的猜測是," "Connecticut附近丟失,但目前爲其他國家。

要看到問題的心臟,考慮

local foo "bar" 
mac li 

當地的宏foo結果不包括引號,因爲他們已經被剝奪。堅持使用複合雙引號保護它們。

local foo `""bar"' 
mac li 
+0

謝謝。我發現問題是複合引號 – rrodrigorn0 2014-10-29 17:51:03

+1

不; **解決方案**是複合報價! – 2014-10-29 17:55:21