Announcement

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

  • Categorizing values within a single variable via loop

    Good evening,

    I am new to Stata and have been researching and consulting the help command on a problem with no luck.

    I am trying to create a new variable "hour_groups" that categorizes the "hour" variable into 4 groups, e.g. 0 to 6 (a.m.) is assigned the number 1, 7 to 12 is assigned 2 and so on. I'm trying the foreach and and forvalues command. Any help is appreciated. Is it possible to generate a new variable while categorizing in the same command?

  • #2
    Shaun:
    welcome to this forum.
    No loop needed here:
    Code:
    . set obs 24
    number of observations (_N) was 0, now 24
    
    . g hours=_n
    
    . g hours_group=1 if hours<=6 & hours!=.
    (18 missing values generated)
    
    . replace hours_group=2 if hours<=12 & hours_group==. & hours!=.
    (6 real changes made)
    
    . replace hours_group=3 if hours<=18 & hours_group==. & hours!=.
    (6 real changes made)
    
    . replace hours_group=4 if hours_group==. & hours!=.
    (6 real changes made)
    
    . list
    
         +------------------+
         | hours   hours_~p |
         |------------------|
      1. |     1          1 |
      2. |     2          1 |
      3. |     3          1 |
      4. |     4          1 |
      5. |     5          1 |
         |------------------|
      6. |     6          1 |
      7. |     7          2 |
      8. |     8          2 |
      9. |     9          2 |
     10. |    10          2 |
         |------------------|
     11. |    11          2 |
     12. |    12          2 |
     13. |    13          3 |
     14. |    14          3 |
     15. |    15          3 |
         |------------------|
     16. |    16          3 |
     17. |    17          3 |
     18. |    18          3 |
     19. |    19          4 |
     20. |    20          4 |
         |------------------|
     21. |    21          4 |
     22. |    22          4 |
     23. |    23          4 |
     24. |    24          4 |
         +------------------+
    
    .
    See also -help label if you want to take the matter further.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      No loop necessary I reckon. You can do it all in one command with recode, see help recode.

      Comment


      • #4
        The scheme you ask for is not consistent as 0 to 6 is 7 hours, 7 to 12 is 6 hours, etc. Nevertheless the main point is that absolutely no loop is needed here. If how to change this to get exactly what you want is not clear to you, then please do as we ask and give a data example (FAQ Advice #12).

        Code:
        clear
        set obs 24
        gen hours = _n
        gen wanted = ceil(hours/6)
        list, sepby(wanted)
        
            +----------------+
             | hours   wanted |
             |----------------|
          1. |     1        1 |
          2. |     2        1 |
          3. |     3        1 |
          4. |     4        1 |
          5. |     5        1 |
          6. |     6        1 |
             |----------------|
          7. |     7        2 |
          8. |     8        2 |
          9. |     9        2 |
         10. |    10        2 |
         11. |    11        2 |
         12. |    12        2 |
             |----------------|
         13. |    13        3 |
         14. |    14        3 |
         15. |    15        3 |
         16. |    16        3 |
         17. |    17        3 |
         18. |    18        3 |
             |----------------|
         19. |    19        4 |
         20. |    20        4 |
         21. |    21        4 |
         22. |    22        4 |
         23. |    23        4 |
         24. |    24        4 |
             +----------------+

        Comment

        Working...
        X