循环记录年份

时间:2018-08-09 14:12:09

标签: loops stata stata-macros

我想使用Year循环重新编码名为for的变量,并在Stata中创建一个名为year的新变量。

我正在寻找的应该缩短以下代码:

recode Year (5 = 1960) (6 = 1961)(7 = 1962)(8 = 1963)(9 = 1964) (10 = 1965) ///
(11 = 1966) (12 = 1967) (13 = 1968) (14 = 1969) (15 = 1970) (16 = 1971) ///
(17 = 1972) (18 = 1973) (19 = 1974) (20 = 1975) (21 = 1976) (22 = 1977) ///
, gen(year)

1 个答案:

答案 0 :(得分:2)

for循环不会缩短您的代码,也没有其他方法可以更简洁地编写代码。

尽管如此,以下对我有用:

local counter1 = 4
local counter2 = 59

forvalues i = 1 / 18 {
    local counter1 = `counter1' + 1
    local counter2 = `counter2' + 1
    local foo `foo' (`counter1' = 19`counter2')
}

recode Year `foo', generate(year)

编辑:

正如@NickCox指出的(并且我同意),而不是循环,您可以执行以下操作:

generate year = Year + 1955