Announcement

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

  • Defining a label in foreach

    I would like to define a label in a foreach because I have a lot of different possible values for the variable, as the table below shows.
    For example, I would like to label 102 as "2 hours", 103 as "3 hours" and so on. The same for the number of days, months and years. I tried the command below but I didn't get the lables for any of the foreach commands. What is wrong and how can I do this without having to type all the possible values?


    Code:
    /*jan96+: IDADE.CNV
        267  Ignorado                                           000-999
          1  < 1 hora                                           001-100
          2  1-23 horas                                         101-123
         25  < 1 dia, horas ign                                 200
         26  1-29 dias                                          201-229
         55  < 1 mes, dias ign                                  300
         56  1-11 meses                                         301-311
         67  menor de 1 ano ign                                 400
         68  1-99 anos                                          401-499
        167  100 anos                                           500
        168  101-199 anos                                       501-599 */
    
    label define idd 0 "Ignorado" 1/100 "< 1 hora" 101 "1 hora" 200 "< 1 dia" 201 "1 dia" 300 "< 1 mes" 301 "1 mes" 400 "< 1 ano" 401 "1 ano" 500 "100 anos" 501 "101 anos"
    foreach t in 2/9 {
    label define idd 10`t' "`t' horas" 20`t' "`t' dias" 30`t' "`t' meses" 40`t' "`t' anos" 50`t' "1`t' anos", add 
    }
    foreach i in 10/23 {
    label define idd 1`i' "`i' horas", add
    }
    foreach i in 10/29 {
    label define idd 2`i' "`i' dias", add
    }
    foreach i in 10/11 {
    label define idd 3`i' "`i' meses", add
    }
    foreach i in 10/99 {
    label define idd 4`i' "`i' anos" 5`i' "1`i' anos", add
    }
    label values idade idd

    The results I got:
    Code:
          idade |      Freq.     Percent        Cum.
    ------------+-----------------------------------
       Ignorado |          1        0.00        0.00
              / |         33        0.07        0.07
              2 |          9        0.02        0.09
              3 |          4        0.01        0.10
              5 |         14        0.03        0.13
              6 |          1        0.00        0.13
              7 |          1        0.00        0.13
             10 |         28        0.06        0.19
             14 |          1        0.00        0.19
             15 |         18        0.04        0.23
             18 |          1        0.00        0.23
             20 |         24        0.05        0.28
             22 |          1        0.00        0.28
    10/23 horas |          2        0.00        0.28
             25 |          4        0.01        0.29
             28 |          1        0.00        0.29
             30 |         26        0.05        0.35
             35 |          2        0.00        0.35
             37 |          1        0.00        0.35
             40 |         10        0.02        0.37
             42 |          1        0.00        0.38
             45 |          6        0.01        0.39
             50 |          7        0.01        0.40
             51 |          1        0.00        0.40
             55 |          2        0.00        0.41
             56 |          1        0.00        0.41
             57 |          2        0.00        0.42
       < 1 hora |          6        0.01        0.43
         1 hora |        223        0.46        0.89
              / |        134        0.28        1.16
            103 |         72        0.15        1.31
            104 |         66        0.14        1.45
            105 |         43        0.09        1.53
            106 |         52        0.11        1.64
            107 |         31        0.06        1.70
            108 |         27        0.06        1.76

  • #2
    I'm guessing that when your write "
    foreach t in 2/9 {" you actually want this repeated for each integer from 2-9, inclusive, correct? if yes, you want to use either:
    Code:
     forval t=2/9 {  or  foreach t of numlist 2/9 {
    Stata will not recognize foreach t in 2/9 as a numlist

    Comment


    • #3
      An alternative (better, because less confusing?) approach would be to recode the actual values of your variable and attach labels to these values, as in

      Code:
      recode idade (1/100 = 1 "< 1 hora") (101/200 = 2 "< 2 horas") ... ,generate(idade2) label(idd)
      label define idd 0 "Ignorado" ... ,add
      Best
      Daniel

      Comment


      • #4
        Oh yeh, thank you Rich!
        Still a question: why the numbers 1 to 100 haven't been labeled as "< 1 hora", as I had defined in the label as below:

        Code:
        label define idd 0 "Ignorado" 1/100 "< 1 hora" 101 "1 hora" 200 "< 1 dia" 201 "1 dia" 300 "< 1 mes" 301 "1 mes" 400 "< 1 ano" 401 "1 ano" 500 "100 anos" 501 "101 anos"

        Code:
             idade |      Freq.     Percent        Cum.
        ------------+-----------------------------------
           Ignorado |          1        0.00        0.00
                  / |         33        0.07        0.07
                  2 |          9        0.02        0.09
                  3 |          4        0.01        0.10
                  5 |         14        0.03        0.13
                  6 |          1        0.00        0.13
                  7 |          1        0.00        0.13
                 10 |         28        0.06        0.19
                 14 |          1        0.00        0.19
                 15 |         18        0.04        0.23
                 18 |          1        0.00        0.23
                 20 |         24        0.05        0.28
                 22 |          1        0.00        0.28
                 23 |          2        0.00        0.28
                 25 |          4        0.01        0.29
                 28 |          1        0.00        0.29
                 30 |         26        0.05        0.35
                 35 |          2        0.00        0.35
                 37 |          1        0.00        0.35
                 40 |         10        0.02        0.37
                 42 |          1        0.00        0.38
                 45 |          6        0.01        0.39
                 50 |          7        0.01        0.40
                 51 |          1        0.00        0.40
                 55 |          2        0.00        0.41
                 56 |          1        0.00        0.41
                 57 |          2        0.00        0.42
           < 1 hora |          6        0.01        0.43
             1 hora |        223        0.46        0.89
            2 horas |        134        0.28        1.16
            3 horas |         72        0.15        1.31
            4 horas |         66        0.14        1.45
            5 horas |         43        0.09        1.53
            6 horas |         52        0.11        1.64
            7 horas |         31        0.06        1.70
            8 horas |         27        0.06        1.76
            9 horas |         25        0.05        1.81
           10 horas |         42        0.09        1.90
           11 horas |         17        0.03        1.93
           12 horas |         20        0.04        1.97
           13 horas |          8        0.02        1.99
           14 horas |          8        0.02        2.01
           15 horas |         13        0.03        2.03
           16 horas |         14        0.03        2.06
           17 horas |         12        0.02        2.09
           18 horas |          9        0.02        2.11
           19 horas |          7        0.01        2.12
           20 horas |         12        0.02        2.14
           21 horas |          6        0.01        2.16
           22 horas |          8        0.02        2.17
           23 horas |         12        0.02        2.20
            < 1 dia |         95        0.20        2.39

        Comment


        • #5
          Experiment to find out what happened:


          Code:
          . label def whatever 1/100 "whatever"
          
          . label li
          whatever:
                     1 /
                   100 whatever
          Nothing in the documentation for label indicates that you can define several labels simultaneously in that way, and you can't. So why did it work at all? Because Stata found a way to make sense of the instruction, but not as you wished.

          Comment

          Working...
          X