Announcement

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

  • Forlooping Variables with Condition

    Hi,

    I have a data set with 5 variables, with a 'slicer'. This is a sample from a larger dataset. I would like to loop each variable for analysis, namely tabstat.

    I tried:

    foreach x in sample_v sample_v_n sample_v_n2{
    tabstat data if sample_v ==1, by(slicer) stat(n mean SD)
    }

    However, it reports x not found. Do I need to do anything different?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(sample_v sample_v_n sample_v_n2 sample_v_n3 sample_v_n4 data slicer)
    1 1 0 0 1 14520 1
    0 1 0 1 1  8875 0
    0 1 0 0 0  4564 1
    1 1 0 1 0   452 0
    0 0 1 0 1     0 1
    1 0 1 1 1  1111 0
    end
    Thank you for your help!

  • #2
    I do not think you are accurately reporting what you have done, or perhaps you are not accurately reporting what results you got. The code you show, with the example data you show, runs with no error messages on my setup.
    Code:
    . * Example generated by -dataex-. For more info, type help dataex
    . clear
    
    . input float(sample_v sample_v_n sample_v_n2 sample_v_n3 sample_v_n4 data slicer)
    
          sample_v  sample_~n  sample_~2  sample_~3  sample_~4       data     slicer
      1. 1 1 0 0 1 14520 1
      2. 0 1 0 1 1  8875 0
      3. 0 1 0 0 0  4564 1
      4. 1 1 0 1 0   452 0
      5. 0 0 1 0 1     0 1
      6. 1 0 1 1 1  1111 0
      7. end
    
    .
    . foreach x in sample_v sample_v_n sample_v_n2{
      2.         tabstat data if sample_v ==1, by(slicer) stat(n mean SD)
      3. }
    
    Summary for variables: data
    Group variable: slicer
    
      slicer |         N      Mean        SD
    ---------+------------------------------
           0 |         2     781.5  465.9834
           1 |         1     14520         .
    ---------+------------------------------
       Total |         3      5361  7938.768
    ----------------------------------------
    
    Summary for variables: data
    Group variable: slicer
    
      slicer |         N      Mean        SD
    ---------+------------------------------
           0 |         2     781.5  465.9834
           1 |         1     14520         .
    ---------+------------------------------
       Total |         3      5361  7938.768
    ----------------------------------------
    
    Summary for variables: data
    Group variable: slicer
    
      slicer |         N      Mean        SD
    ---------+------------------------------
           0 |         2     781.5  465.9834
           1 |         1     14520         .
    ---------+------------------------------
       Total |         3      5361  7938.768
    ----------------------------------------
    
    .
    end of do-file

    What it does is almost certainly not what you want, because it produces the exact same table three times--which is, of course, exactly what will happen when you have a loop with an iterator called x but then x is never used anywhere inside the loop.

    Since I don't know what you really did and got. I also can't figure out what you want to do: I don't know what you mean by "loop each variable for analysis, namely tabstat." Which part of the tabstat command do you want to change with each iteration of the loop? (If you couldn't write a loop but just had to write out five separate tabstat commands, one for each of the five variables, what would those commands look like?) So, under the circumstances, I can't help.

    If you fill in those gaps, perhaps a helpful response will be possible.

    Comment


    • #3
      My apologies! I must have mistyped my code for this troubleshooting. My original intention was:
      tabstat data if 'x' ==1, by(slicer) stat(n mean SD)

      My original thought was to convert these code into a shorter version:

      tabstat data if sample_v ==1, by(slicer) stat(n mean SD)
      tabstat data if sample_v_n ==1, by(slicer) stat(n mean SD)
      tabstat data if sample_v_n2 ==1, by(slicer) stat(n mean SD)
      ...

      Thank you for your response!

      Comment


      • #4
        You could replace three lines with three lines.

        Code:
        foreach v in sample_v sample_v_n sample_v_n2 { 
              tabstat data if `v' ==1, by(slicer) stat(n mean SD)
        }
        Similarly you could loop over _v _v_n _v_n2 and so forth. This is just picking up on Clyde Schechter 's key point

        Which part of the tabstat command do you want to change with each iteration of the loop?

        Comment


        • #5
          Thank you! I think I must have mistyped something in my code.

          I have another question - Is it possible to combine all tabstat results into a condensed version as the loop proceeds? With the same row of data (Slicer 0 and 1), sample_v sample_v_n sample_v_n2 would appear as a larger row for each of the columns (n mean SD).

          Thank you!

          Comment


          • #6
            It could be done, but I advise working out how to this directly with the new table command.

            Comment

            Working...
            X