Hi everybody, I started learning stata recently and I cannot overcome this error.
I am trying to create N portfolios (373) where in each portfolio I have insert the sum of different variables (varlist from month_au to month_us) following two conditions:
if the sum of the past 12months is positive (this sum is stored in the variable returnsmonth_*), then I will have to add the specific variable
if the sum of the past 12months is negative, then I will have to substract the specific variable
I tried to use the following code;
forvalues i = 1(1)373 {
gen portfolio`i' = 0
foreach var of varlist month_au - month_us {
replace portfolio`i' = portfolio`i' + `var' if returns`var'[Nobs -1] > 0
replace portfolio`i' = portfolio`i' - '`var' if returns`var'[Nobs -1] < 0
}
}
however, it reports an error saying "month_au invalid name"
I also tried to use a local variables giving to y the values from 1 to 373 but it does not store any results.
foreach i of local y {
gen portfolio`i' = 0
foreach var of varlist month_au - month_us {
replace portfolio`i' = portfolio`i' + `var' if returns`var'[Nobs -1] > 0
replace portfolio`i' = portfolio`i' - '`var' if returns`var'[Nobs -1] < 0
}
}
Hoping the problem is clear, any help will be more that appreciate,
Thanks in advace!
I am trying to create N portfolios (373) where in each portfolio I have insert the sum of different variables (varlist from month_au to month_us) following two conditions:
if the sum of the past 12months is positive (this sum is stored in the variable returnsmonth_*), then I will have to add the specific variable
if the sum of the past 12months is negative, then I will have to substract the specific variable
I tried to use the following code;
forvalues i = 1(1)373 {
gen portfolio`i' = 0
foreach var of varlist month_au - month_us {
replace portfolio`i' = portfolio`i' + `var' if returns`var'[Nobs -1] > 0
replace portfolio`i' = portfolio`i' - '`var' if returns`var'[Nobs -1] < 0
}
}
however, it reports an error saying "month_au invalid name"
I also tried to use a local variables giving to y the values from 1 to 373 but it does not store any results.
foreach i of local y {
gen portfolio`i' = 0
foreach var of varlist month_au - month_us {
replace portfolio`i' = portfolio`i' + `var' if returns`var'[Nobs -1] > 0
replace portfolio`i' = portfolio`i' - '`var' if returns`var'[Nobs -1] < 0
}
}
Hoping the problem is clear, any help will be more that appreciate,
Thanks in advace!
Comment