Announcement

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

  • foreach value label throwing error

    I was trying to capture variable and value labels as a local macro to use for a K-M plot in Stata 18. The foreach loops runs well independently but when tacked as an include file with other include files, it seems to fail with an error. What could be the glitch? Furthermore, the code deletes the existing var and value labels and switches them to 0 and 1.


    Error
    Code:
    . foreach L of local levels {
      2.    local vl`L': label `vlname' `L'
      3. }
    0 invalid name
    r(198);
    r(198);

    // vlabgrabber.doh file below
    Code:
    * `blab' and level labels macro names are `vl0' `vl1'
    local blab : variable label `b'
    levelsof `b', local(levels)
    local vlname: value label `b'
    foreach L of local levels {
       local vl`L': label `vlname' `L'
    }
    Code into which above is nestled in.
    Code:
    local b cllbtk
            drop if missing(`b')
            include ".\gv_includes\vlabgrabber.doh"            // Grab var nanme and level labels for graph
            include ".\gv_includes\kmopt_bin.doh"            // KM options for graph
            sts test `b', logrank
            local p = chi2tail(`r(df)',`r(chi2)')
            include ".\gv_includes\pvalkm.do"                // Format outputted p-values
            gvkm2 `b' 5 years                                                         // run program for 24 mo K-M plot
    
    * add option texts below
    *------------------------
    local title     "`blab' adversely impact OS24"                                    
    local note         ""
    local caption     "*Standard errors adjusted for correlation between three centers in HR estimation"
    
    *....do not tamper below************************
    include ".\gv_includes\kmplotter.do"
    drop km a0 a1
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte cllbtk
    1
    0
    0
    1
    0
    1
    1
    0
    0
    0
    1
    1
    0
    0
    0
    0
    1
    1
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    1
    0
    1
    0
    1
    0
    1
    0
    1
    1
    1
    1
    0
    0
    0
    0
    1
    1
    0
    0
    0
    0
    0
    1
    0
    1
    0
    0
    0
    0
    1
    1
    1
    1
    0
    0
    0
    0
    0
    0
    end
    label values cllbtk yesno
    label def yesno 0 "NO", modify
    label def yesno 1 "YES", modify

  • #2
    Well, neither your data example nor your code provides the complete information needed to fully replicate your problem. The file kmopt_bin.doh is not shown, and the data example contains no variable other than cllbtk, thereby precluding doing any testing of how this all plays out in the K-M plot.

    But I ran a stripped down version of your problem, and it runs with no error messages and produces what I take to be the intended results:
    Code:
    . * Example generated by -dataex-. For more info, type help dataex
    . clear*
    
    . input byte cllbtk
    
           cllbtk
      1. 1
      2. 0
      3. 0
      4. 1
      5. 0
      6. 1
      7. 1
      8. 0
      9. 0
     10. 0
     11. 1
     12. 1
     13. 0
     14. 0
     15. 0
     16. 0
     17. 1
     18. 1
     19. 0
     20. 0
     21. 0
     22. 0
     23. 0
     24. 0
     25. 0
     26. 0
     27. 0
     28. 0
     29. 0
     30. 0
     31. 0
     32. 0
     33. 0
     34. 0
     35. 0
     36. 0
     37. 0
     38. 0
     39. 0
     40. 0
     41. 0
     42. 0
     43. 0
     44. 0
     45. 0
     46. 0
     47. 0
     48. 0
     49. 0
     50. 0
     51. 0
     52. 0
     53. 0
     54. 0
     55. 0
     56. 0
     57. 0
     58. 0
     59. 0
     60. 1
     61. 1
     62. 0
     63. 1
     64. 0
     65. 1
     66. 0
     67. 1
     68. 0
     69. 1
     70. 1
     71. 1
     72. 1
     73. 0
     74. 0
     75. 0
     76. 0
     77. 1
     78. 1
     79. 0
     80. 0
     81. 0
     82. 0
     83. 0
     84. 1
     85. 0
     86. 1
     87. 0
     88. 0
     89. 0
     90. 0
     91. 1
     92. 1
     93. 1
     94. 1
     95. 0
     96. 0
     97. 0
     98. 0
     99. 0
    100. 0
    101. end
    
    . label values cllbtk yesno
    
    . label def yesno 0 "NO", modify
    
    . label def yesno 1 "YES", modify
    
    .
    . local b cllbtk
    
    .
    . /* THIS MATERIAL SAVED AS vlabgrabber.doh
    > * `blab' and level labels macro names are `vl0' `vl1'
    > local blab : variable label `b'
    > levelsof `b', local(levels)
    > local vlname: value label `b'
    > foreach L of local levels {
    >    local vl`L': label `vlname' `L'
    > }
    > */
    .
    . include vlabgrabber.doh
    
    . * `blab' and level labels macro names are `vl0' `vl1'
    . local blab : variable label `b'
    
    . levelsof `b', local(levels)
    0 1
    
    . local vlname: value label `b'
    
    . foreach L of local levels {
      2.    local vl`L': label `vlname' `L'
      3. }
    
    .
    .
    . macro dir
    S_level:        95
    F1:             help advice;
    F2:             describe;
    F7:             save
    F8:             use
    S_ADO:          BASE;SITE;.;PERSONAL;PLUS;OLDPLACE
    S_StataMP:      MP
    S_StataSE:      SE
    S_OS:           Windows
    S_OSDTL:        64-bit
    S_MACH:         PC (64-bit x86-64)
    _vl1:           YES
    _vl0:           NO
    _vlname:        yesno
    _levels:        0 1
    _b:             cllbtk
    
    .

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Well, neither your data example nor your code provides the complete information needed to fully replicate your problem. The file kmopt_bin.doh is not shown, and the data example contains no variable other than cllbtk, thereby precluding doing any testing of how this all plays out in the K-M plot.

      But I ran a stripped down version of your problem, and it runs with no error messages and produces what I take to be the intended results:
      Code:
      . * Example generated by -dataex-. For more info, type help dataex
      . clear*
      
      . input byte cllbtk
      
      cllbtk
      1. 1
      2. 0
      3. 0
      4. 1
      5. 0
      6. 1
      7. 1
      8. 0
      9. 0
      10. 0
      11. 1
      12. 1
      13. 0
      14. 0
      15. 0
      16. 0
      17. 1
      18. 1
      19. 0
      20. 0
      21. 0
      22. 0
      23. 0
      24. 0
      25. 0
      26. 0
      27. 0
      28. 0
      29. 0
      30. 0
      31. 0
      32. 0
      33. 0
      34. 0
      35. 0
      36. 0
      37. 0
      38. 0
      39. 0
      40. 0
      41. 0
      42. 0
      43. 0
      44. 0
      45. 0
      46. 0
      47. 0
      48. 0
      49. 0
      50. 0
      51. 0
      52. 0
      53. 0
      54. 0
      55. 0
      56. 0
      57. 0
      58. 0
      59. 0
      60. 1
      61. 1
      62. 0
      63. 1
      64. 0
      65. 1
      66. 0
      67. 1
      68. 0
      69. 1
      70. 1
      71. 1
      72. 1
      73. 0
      74. 0
      75. 0
      76. 0
      77. 1
      78. 1
      79. 0
      80. 0
      81. 0
      82. 0
      83. 0
      84. 1
      85. 0
      86. 1
      87. 0
      88. 0
      89. 0
      90. 0
      91. 1
      92. 1
      93. 1
      94. 1
      95. 0
      96. 0
      97. 0
      98. 0
      99. 0
      100. 0
      101. end
      
      . label values cllbtk yesno
      
      . label def yesno 0 "NO", modify
      
      . label def yesno 1 "YES", modify
      
      .
      . local b cllbtk
      
      .
      . /* THIS MATERIAL SAVED AS vlabgrabber.doh
      > * `blab' and level labels macro names are `vl0' `vl1'
      > local blab : variable label `b'
      > levelsof `b', local(levels)
      > local vlname: value label `b'
      > foreach L of local levels {
      > local vl`L': label `vlname' `L'
      > }
      > */
      .
      . include vlabgrabber.doh
      
      . * `blab' and level labels macro names are `vl0' `vl1'
      . local blab : variable label `b'
      
      . levelsof `b', local(levels)
      0 1
      
      . local vlname: value label `b'
      
      . foreach L of local levels {
      2. local vl`L': label `vlname' `L'
      3. }
      
      .
      .
      . macro dir
      S_level: 95
      F1: help advice;
      F2: describe;
      F7: save
      F8: use
      S_ADO: BASE;SITE;.;PERSONAL;PLUS;OLDPLACE
      S_StataMP: MP
      S_StataSE: SE
      S_OS: Windows
      S_OSDTL: 64-bit
      S_MACH: PC (64-bit x86-64)
      _vl1: YES
      _vl0: NO
      _vlname: yesno
      _levels: 0 1
      _b: cllbtk
      
      .
      Apologies for the passing half code that could not be entirely recapitulated. But yes, realized that the dummy variable cllbtk initially had no value labels assigned which explained why the loop failed. Assigning them in the same session and re-running the programs (with the loop) made the loop strip the assigned value labels and again throw the error. I had to clear everything from memory and label the values before it would run. Did not quite understand the logic of why it would do that. But I am working on making sure I have all value and variable labels assigned without missing values.Thanks for double checking and showing me the macro dir command.

      Comment

      Working...
      X