Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop over values not working

    Dear all,

    I have a panel dataset (panelvar is id and timevar is year). I have been trying to generate a group of new variables from tssmooth exponential funciton using forvalues. Here is my sample data.

    Code:
    *To install: 
    ssc install dataex, replace
    clear 
    input int year int id float roi
    2015 1 0.069
    2013 1 0.091
    2012 1 0.158
    2010 1 0.126
    2011 1 0.162
    2014 1 0.063
    2014 2 -0.019
    2015 2 -0.074
    2015 3 0.256
    2012 3 0.199
    2013 3 0.229
    2014 3 0.254
    2010 3 0.188
    2011 3 0.114
    2014 4 0.07
    2012 4 0.184
    2013 4 0.117
    2015 4 -0.033
    2011 4 0.184
    2010 4 0.148
    2012 5 -0.007
    2014 5 0.076
    2010 5 1.004
    2015 5 -0.021
    2011 5 -0.034
    2013 5 0.076
    2011 6 0.129
    2013 6 0.09
    2015 6 0.163
    2012 6 0.133
    2010 6 0.132
    2014 6 0.091
    2013 7 0.048
    2012 7 0.067
    2010 7 0.019
    2014 7 0.062
    2011 7 0.093
    2015 7 0.066
    2015 8 0.125
    2014 8 0.112
    2011 8 0.186
    2010 8 0.198
    2012 8 0.131
    2013 8 0.12
    
    end
    Here is my code:
    Code:
    xtset id year
    forvalues x = 0.1(0.05)1 {
        generate `i'= 20 * `x'-1
        tssmooth exponential ewma_roi_`i' = roi, parms(`x')
    }
    I want to generate ewma_roi_1, ewma_roi_2, ewma_roi_3, ...., and ewma_roi_19. Each new variable corresponds to its own smoothing parameter. However, I keep receiving the error message saying "too few variables specified" (see below). To me, everything looks correct. I am not sure what the real problem is and how to fix the error of two-few-variables-specified. Any help would be greatly appreciated!
    Click image for larger version

Name:	too few variables.PNG
Views:	1
Size:	1.1 KB
ID:	1546311


    Thanks,
    David

  • #2
    Perhaps this does what you want.
    Code:
    xtset id year
    forvalues i=1/19 {
        local x = `i'/20
        tssmooth exponential ewma_roi_`i' = roi, parms(`x')
    }
    Last edited by William Lisowski; 12 Apr 2020, 09:55.

    Comment


    • #3
      Thank you very much, William! Your code works like magic!

      Best,
      David

      Comment

      Working...
      X