简体   繁体   English

重塑长变量时出错

[英]Error in reshape long multiple variables

I have to reshape my dataset from wide to long. 我必须将数据集从宽到长reshape I have 500 variables that range from 2016 to 2007 and are recorded as abcd2016 and so on. 我有500个变量,范围从2016年到2007年,并记录为abcd2016等。 I needed a procedure that allowed me to reshape without writing all the variables' names and I run: 我需要一个程序,该程序无需编写所有变量的名称即可reshape ,然后运行:

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

and so on, then: 依此类推,然后:

reshape long `stubs16' `stubs15' `stubs14' `stubs13' `stubs12' `stubs11' `stubs10' `stubs09' `stubs08' `stubs07', i(id) j(year)

but I get the error 但我得到了错误

invalid syntax
r(198);

Why? 为什么? Can you help me to fix it? 你能帮我解决吗?

The idea is to just specify the stub when reshaping to long format. 想法是在重整形为长格式时仅指定存根。 To that end, you need to remove the year part from the variable name and store unique stubs in a local that you can pass to 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)

This will store the numeric suffix in a variable called t. 这会将数字后缀存储在名为t的变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM