Announcement

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

  • Copying values within a cluster

    Hello statalist,

    I want to copy values within a cluster as below.
    ID Var1 Var2 Var3
    123 1 ... ...
    123 0 ... ...
    123 0 ... ...
    456 2 ... ...
    456 0 ... ...
    In this data, the values of Var1 are different, and I want to make it the same in one cluster like this.

    ID Var1 Var2 Var3
    123 1 ... ...
    123 1 ... ...
    123 1 ... ...
    456 2 ... ...
    456 2 ... ...

    Thanks in advance.

    HS JANG


  • #2
    Var1 is a kind of dummy variables.

    Comment


    • #3
      Please use -dataex- to share example/excerpt of your data with the list. Thanks.
      That said:
      Code:
      . input ID Var1
      
                  ID       Var1
        1. 123 1
        2. 123 0
        3. 123 0
        4. 456 2
        5. 456 0
        6. end
      
      . bysort ID: replace Var1=Var1[1]
      
      . list
      
           +------------+
           |  ID   Var1 |
           |------------|
        1. | 123      1 |
        2. | 123      1 |
        3. | 123      1 |
        4. | 456      2 |
        5. | 456      2 |
           +------------+
      
      .
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Thank you, Carlo!!

        Comment


        • #5
          Dear Carlo,

          What if the dataset are like this?

          In this case, unlike before, if there is a value in the second and third ... values, I want to copy the value in same ID.
          Thank you in advance.

          ID Var
          123 1
          123 0
          123 0
          456 0
          456 2
          789 0
          789 0
          789 3

          ->

          ID Var
          123 1
          123 1
          123 1
          456 2
          456 2
          789 3
          789 3
          789 3
          Last edited by HS JANG; 27 Jan 2019, 23:42.

          Comment


          • #6
            Please use -dataex- to share example/excerpt of your data with the list. Thanks.
            That said:
            Code:
            . bysort ID: egen max=max( Var)
            
            . replace Var=max
            
            . drop max
            
            . list
            
                 +-----------+
                 |  ID   Var |
                 |-----------|
              1. | 123     1 |
              2. | 123     1 |
              3. | 123     1 |
              4. | 456     2 |
              5. | 456     2 |
                 |-----------|
              6. | 789     3 |
              7. | 789     3 |
              8. | 789     3 |
                 +-----------+
            
            .
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Dear Carlo,

              I appreciate for your reply and kindness.
              Thank you!

              Comment

              Working...
              X