Announcement

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

  • Difficulties with loop in a loop, feeding a matrix values through a loop

    Hello, I am looking to fill a 4 by 5 matrix with the descriptive statistics (e.g. number of obs, max, min, sd, mean) of each variable. The objective is to fill each column with each variables statistics and utilize each row as a way to differentiate each variable. I would believe my code to be correct but every time I attempt to run it an "unexpected end of file" error occurs. If someone could help me out it would be excellent. The code is below:

    I attempt to sum each variable separately through the tokenize, following this I attempt to utilize another foreach to extract scalars from the "sum" return and insert these values in the matrix, nevertheless, although I can extract the desired statistics the second loop, where I attempt to extract each statistic and insert it fails.

    //

    global areauso "area_uso_12 area_uso_14 area_uso_16 area_uso_17"

    global numeroaños: word count $areauso

    global colnames mean sd max min N

    matrix stats = J($numeroaños,5,.)
    matrix list stats

    tokenize $areauso

    foreach x of numlist 1/$numeroaños {
    local j=0
    sum ``x''

    foreach y of global colnames {
    local j=`j'+1
    matrix stats[`n',`j']=r(`y')
    }
    }

    //

  • #2
    Make sure you have a blank line after the last } in your dofile. An unexpected end of file usually indicates unmatched { and }. It looks like you have the right number but if there isn't a line break after the last one Stata may not process it.

    Comment


    • #3
      Thanks, although when I do that an "Invalid Syntax" error starts to appear, may there be any mistake in the code?

      Comment


      • #4
        You can use the command set trace on before your loop to see where the error is.
        It looks like you have not defined the local n that is used in your last line. Did you mean for that to be `x' , maybe?

        Comment


        • #5
          You can use tabstat in any case for your purpose. Transpose the resulting matrix if you prefer.

          Code:
          * sandbox dataset
          clear
          
          set obs 5
          set seed 2803
          
          forval j = 12/17 {
              gen area_uso_`j' = runiformint(1, 10)
          }
          
          list
          
               +-----------------------------------------------------------------+
               | area_~12   area_~13   area_~14   area_~15   area_~16   area_~17 |
               |-----------------------------------------------------------------|
            1. |        6          7          5          3          8          2 |
            2. |        2          7          2          8          3          5 |
            3. |        6          2          2          7          5          1 |
            4. |        1          1         10          6          2          3 |
            5. |        3          2          4          7          8          3 |
               +-----------------------------------------------------------------+
          
          
          tabstat area_uso_*, s(n min max mean sd) c(s) save
          
              variable |         N       min       max      mean        sd
          -------------+--------------------------------------------------
           area_uso_12 |         5         1         6       3.6  2.302173
           area_uso_13 |         5         1         7       3.8  2.949576
           area_uso_14 |         5         2        10       4.6  3.286335
           area_uso_15 |         5         3         8       6.2  1.923538
           area_uso_16 |         5         2         8       5.2  2.774887
           area_uso_17 |         5         1         5       2.8   1.48324
          ----------------------------------------------------------------
          
          matrix li r(StatTotal)
          
          r(StatTotal)[5,6]
                area_uso_12  area_uso_13  area_uso_14  area_uso_15  area_uso_16  area_uso_17
             N            5            5            5            5            5            5
           min            1            1            2            3            2            1
           max            6            7           10            8            8            5
          mean          3.6          3.8          4.6          6.2          5.2          2.8
            sd    2.3021729    2.9495762    3.2863353    1.9235384    2.7748874    1.4832397

          Comment

          Working...
          X