Announcement

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

  • Assigning set of values/names to every 1-10 observation

    Hello,
    I need to assign a set of names in one particular order to the first 10 observations. I want to follow the same pattern for the remaining observation. I have around 700 observations in the data. Here is a code example for 3 observation set:

    Code:
    gen xx = _n : 
    
    gen name = "nick" if xx == 1 
    replace name = "tina" if xx == 2 
    replace name = "pam" if xx == 3
    I want to repeat these names for the next 4-6th observations, and then the 9th-12 observations, till the 700th observation. Please help with this.


    Thanks,
    Smriti

  • #2
    Several ways to do it. Here's one:

    Code:
    . clear
    
    . set obs 30
    number of observations (_N) was 0, now 30
    
    . egen seq = seq(), to(3)
    
    . gen wanted = word("nick tina pam", seq)
    
    
    . list , sep(3)
    
         +--------------+
         | seq   wanted |
         |--------------|
      1. |   1     nick |
      2. |   2     tina |
      3. |   3      pam |
         |--------------|
      4. |   1     nick |
      5. |   2     tina |
      6. |   3      pam |
         |--------------|
      7. |   1     nick |
      8. |   2     tina |
      9. |   3      pam |
         |--------------|
     10. |   1     nick |
     11. |   2     tina |
     12. |   3      pam |
         |--------------|
     13. |   1     nick |
     14. |   2     tina |
     15. |   3      pam |
         |--------------|
     16. |   1     nick |
     17. |   2     tina |
     18. |   3      pam |
         |--------------|
     19. |   1     nick |
     20. |   2     tina |
     21. |   3      pam |
         |--------------|
     22. |   1     nick |
     23. |   2     tina |
     24. |   3      pam |
         |--------------|
     25. |   1     nick |
     26. |   2     tina |
     27. |   3      pam |
         |--------------|
     28. |   1     nick |
     29. |   2     tina |
     30. |   3      pam |
         +--------------+

    Comment


    • #3
      Hi Nick,

      Thanks a lot for the code. I should have specified the exact names I needed earlier. So the names are "nick_ten" "tina_john".
      When I run the code you provided, with these names, Stata gives the error of invalid name. The underscore here is creating the problem but I need it in the names.
      I read up on 'seq' but could not figure out how to work with the special character i.e. underscore.

      Please help with this.

      Comment


      • #4
        Please help me to help you by giving a concrete example. Underscores won’t undermine the logic of my earlier code.

        Comment


        • #5
          Hi Nick,
          I had made an error in the code which I just spotted. It works perfectly now. Apologies!

          Comment

          Working...
          X