gpt4 book ai didi

syntax-error - reshape 长多个变量时出错

转载 作者:行者123 更新时间:2023-12-03 08:26:40 26 4
gpt4 key购买 nike

我必须 reshape我的数据集从宽到长。我有 500 个变量,范围从 2016 年到 2007 年,记录为 abcd2016等等。我需要一个允许我 reshape 的程序不写所有变量的名称,我运行:

unab vars : *2016 
local stubs16 : subinstr local vars "2016" "", all
unab vars : *2015
local stubs15 : subinstr local vars "2015" "", all

依此类推,然后:
reshape long `stubs16' `stubs15' `stubs14' `stubs13' `stubs12' `stubs11' `stubs10' `stubs09' `stubs08' `stubs07', i(id) j(year)

但我得到了错误
invalid syntax
r(198);

为什么?你能帮我修一下吗?

最佳答案

这个想法是在整形为长格式时只指定 stub 。为此,您需要从变量名称中删除年份部分并将唯一 stub 存储在可以传递给 reshape 的本地中:

/* (1) Fake Data */
clear
set obs 100
gen id = _n
foreach s in stub stump head {
forvalues t = 2008(1)2018 {
gen `s'`t' = rnormal()
}
}

/* (2) Get a list of stubs and reshape */
/* Get a list of variables that contain 20, which is stored in r(varlist) */
ds *20*
/* remove the year part */
local prefixes = ustrregexra("`r(varlist)'","20[0-9][0-9]","")
/* remove duplicates from list */
local prefixes: list uniq prefixes
reshape long `prefixes', i(id) j(t)

这会将数字后缀存储在名为 t 的变量中。

关于syntax-error - reshape 长多个变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51711379/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com