Dear Statalist community,
I am trying to transform a data from wide into long format. The problem is that my data has thousands of variables, and I do not want to write all of them into the reshape command. To avoid writing all the variables, I am using the "ds" command combined with local to obtain the list of variables that I want to reshape. The problem is that those variables name finish with the year, in particular variables are called XXXXX_2018. What I want to do is rename the variables XXX so that I can then write: reshape varlist, i(PUBID) j(year) and I do not need to write every single variable. However, I am having problems renaming those variables. The code is the following:
1. Is there a more efficient way to do a reshape without having to write thousands of variables names?
2. How can I fix my code so that I can efficiently eliminate the suffix "_2018" from my variables names?
I attach the code that is producing the following error:
I am trying to transform a data from wide into long format. The problem is that my data has thousands of variables, and I do not want to write all of them into the reshape command. To avoid writing all the variables, I am using the "ds" command combined with local to obtain the list of variables that I want to reshape. The problem is that those variables name finish with the year, in particular variables are called XXXXX_2018. What I want to do is rename the variables XXX so that I can then write: reshape varlist, i(PUBID) j(year) and I do not need to write every single variable. However, I am having problems renaming those variables. The code is the following:
Code:
ds PUBID KEY_SEX KEY_BDATE_M KEY_BDATE_Y CV_SAMPLE_TYPE KEY_RACE_ETHNICITY,not
local allvar r(varlist)
di `allvar'
foreach vari in `allvar' {
local aa = subinstr("`vari'","_2018","",.)
rename `vari' `aa'
}
2. How can I fix my code so that I can efficiently eliminate the suffix "_2018" from my variables names?
I attach the code that is producing the following error:
Code:
. ds PUBID KEY_SEX KEY_BDATE_M KEY_BDATE_Y CV_SAMPLE_TYPE KEY_RACE_ETHNICITY,not
~1_01_M_1998 ~1_05_Y_1998 ~2_05_M_1998 ~3_04_Y_1998 YSCH_21300.. YSCH_21300.. YSCH_21800.. YSCH_21800.. NE~E_05_1998 NE~T_02_1998
~1_01_Y_1998 ~2_01_M_1998 ~2_05_Y_1998 ~3_05_M_1998 YSCH_21300.. YSCH_21300.. YSCH_21800.. YSCH_21800.. NE~E_06_1998 NE~T_03_1998
~1_02_M_1998 ~2_01_Y_1998 ~3_01_M_1998 ~3_05_Y_1998 YSCH_21300.. YSCH_21300.. YSCH_21800.. YSCH_21800.. NE~D_01_1998 NE~T_04_1998
~1_02_Y_1998 ~2_02_M_1998 ~3_01_Y_1998 ~4_01_M_1998 YSCH_21300.. YSCH_21300.. YSCH_21800.. YSCH_21800.. NE~D_02_1998 NE~T_05_1998
~1_03_M_1998 ~2_02_Y_1998 ~3_02_M_1998 ~4_01_Y_1998 YSCH_21300.. YSCH_21300.. YSCH_21800.. YSCH_21800.. NE~D_03_1998 NE~T_06_1998
~1_03_Y_1998 ~2_03_M_1998 ~3_02_Y_1998 ~5_01_M_1998 YSCH_21300.. YSCH_21300.. YSCH_21800.. NE~E_01_1998 NE~D_04_1998
~1_04_M_1998 ~2_03_Y_1998 ~3_03_M_1998 ~5_01_Y_1998 YSCH_21300.. YSCH_21800.. YSCH_21800.. NE~E_02_1998 NE~D_05_1998
~1_04_Y_1998 ~2_04_M_1998 ~3_03_Y_1998 YSCH_21300.. YSCH_21300.. YSCH_21800.. YSCH_21800.. NE~E_03_1998 NE~D_06_1998
~1_05_M_1998 ~2_04_Y_1998 ~3_04_M_1998 YSCH_21300.. YSCH_21300.. YSCH_21800.. YSCH_21800.. NE~E_04_1998 NE~T_01_1998
. local allvar r(varlist)
. di `allvar'
YSCH_20400_01_01_M_1998 YSCH_20400_01_01_Y_1998 YSCH_20400_01_02_M_1998 YSCH_20400_01_02_Y_1998 YSCH_20400_01_03_M_1998 YSCH_20400_01_03_Y_1998 YSCH_2
> 0400_01_04_M_1998 YSCH_20400_01_04_Y_1998 YSCH_20400_01_05_M_1998 YSCH_20400_01_05_Y_1998 YSCH_20400_02_01_M_1998 YSCH_20400_02_01_Y_1998 YSCH_20400
> _02_02_M_1998 YSCH_20400_02_02_Y_1998 YSCH_20400_02_03_M_1998 YSCH_20400_02_03_Y_1998 YSCH_20400_02_04_M_1998 YSCH_20400_02_04_Y_1998 YSCH_20400_02_
> 05_M_1998 YSCH_20400_02_05_Y_1998 YSCH_20400_03_01_M_1998 YSCH_20400_03_01_Y_1998 YSCH_20400_03_02_M_1998 YSCH_20400_03_02_Y_1998 YSCH_20400_03_03_M
> _1998 YSCH_20400_03_03_Y_1998 YSCH_20400_03_04_M_1998 YSCH_20400_03_04_Y_1998 YSCH_20400_03_05_M_1998 YSCH_20400_03_05_Y_1998 YSCH_20400_04_01_M_199
> 8 YSCH_20400_04_01_Y_1998 YSCH_20400_05_01_M_1998 YSCH_20400_05_01_Y_1998 YSCH_21300_01_01_1998 YSCH_21300_01_02_1998 YSCH_21300_01_03_1998 YSCH_213
> 00_01_04_1998 YSCH_21300_01_05_1998 YSCH_21300_02_01_1998 YSCH_21300_02_02_1998 YSCH_21300_02_03_1998 YSCH_21300_02_04_1998 YSCH_21300_02_05_1998 YS
> CH_21300_03_01_1998 YSCH_21300_03_02_1998 YSCH_21300_03_03_1998 YSCH_21300_03_04_1998 YSCH_21300_03_05_1998 YSCH_21300_04_01_1998 YSCH_21300_05_01_1
> 998 YSCH_21800_01_01_1998 YSCH_21800_01_02_1998 YSCH_21800_01_03_1998 YSCH_21800_01_04_1998 YSCH_21800_01_05_1998 YSCH_21800_02_01_1998 YSCH_21800_0
> 2_02_1998 YSCH_21800_02_03_1998 YSCH_21800_02_04_1998 YSCH_21800_02_05_1998 YSCH_21800_03_01_1998 YSCH_21800_03_02_1998 YSCH_21800_03_03_1998 YSCH_2
> 1800_03_04_1998 YSCH_21800_03_05_1998 YSCH_21800_04_01_1998 YSCH_21800_05_01_1998 NEWSCHOOL_SCHCODE_01_1998 NEWSCHOOL_SCHCODE_02_1998 NEWSCHOOL_SCHC
> ODE_03_1998 NEWSCHOOL_SCHCODE_04_1998 NEWSCHOOL_SCHCODE_05_1998 NEWSCHOOL_SCHCODE_06_1998 NEWSCHOOL_PUBID_01_1998 NEWSCHOOL_PUBID_02_1998 NEWSCHOOL_
> PUBID_03_1998 NEWSCHOOL_PUBID_04_1998 NEWSCHOOL_PUBID_05_1998 NEWSCHOOL_PUBID_06_1998 NEWSCHOOL_LEFT_01_1998 NEWSCHOOL_LEFT_02_1998 NEWSCHOOL_LEFT_0
> 3_1998 NEWSCHOOL_LEFT_04_1998 NEWSCHOOL_LEFT_05_1998 NEWSCHOOL_LEFT_06_1998
.
. foreach vari in `allvar' {
2. local aa = subinstr("`vari'","_2018","",.)
3. rename `vari' `aa'
4. }
parentheses unbalanced

Comment