讓我編輯我的問題來澄清。下面是Python代碼:如何在python中創建一個stata本地宏?
decade_begin = 1970
while decade_begin <= 1994:
for x in range(0, 10):
a= decade_begin + x
decade_begin = a+1
decade_end=a
print "year_",decade_begin,"s" #start year
最後一行的輸出是:
year_1980s
year_1990s
我希望能夠創建一個變量,使得:
year_1990s = [a]
在Stata這很容易,使用本地宏'x'。但在Python中,我認爲混合字符串和int在變量名是不允許的。
forval i = 1979/1994 {
whatever if year == `i'
save year_`i'.dta, replace
}
有關如何在python中實現的任何想法?
對於那些熟悉Python但不熟悉STATA的人,你能解釋一下'x'是什麼嗎?你的意思是像[格式文字](http://stackoverflow.com/documentation/python/1019/string-formatting/4021/format-literals#t=201611212058243848285)? –
我認爲它在STATA被稱爲本地宏。我找到的最簡單的解釋是:https://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm – Carla
它**被稱爲本地宏。這在源碼http://www.stata.com/help.cgi?foreach上是非常明確的但我懷疑Stata [NB]術語是否有助於回答python問題,這是一個如何遍歷列表的問題。 –