Announcement

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

  • Duplicating obs based on Unique ID in appended dataset

    long ID str100 org_name str16 datasource float budet_category

    1234456 Helping Hand Survey1 2
    1234456 Helping Hand Survey2
    1234456 Helping Hand Survey3
    9089081 Save Children Survey1 5
    9089081 Save Children Survey2
    9089081 Save Children Survey3

    I need to duplicate the data for the same organization with the same unique ID for the different sources. For instance, ID 1234456 organization name Helping Hand budget_category is 2 and I want to duplicate the same for rows 2 and 3 (the same ID and same org but different datasource (survey2 and survey3).
    Thanks so much in advance!
    Abhishek
    Last edited by abhishek bhati; 01 Jun 2021, 11:11.

  • #2
    Please note the use of the dataex command to present your example data in an immediately usable format, ready to copy and past into Stata.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long ID str100 org_name str16 datasource float budet_category
    1234456 "Helping Hand"  "Survey1" 2
    1234456 "Helping Hand"  "Survey2" .
    1234456 "Helping Hand"  "Survey3" .
    9089081 "Save Children" "Survey1" 5
    9089081 "Save Children" "Survey2" .
    9089081 "Save Children" "Survey3" .
    end
    by ID (budet_category), sort: replace budet_category = budet_category[1] if missing(budet_category)
    sort ID datasource
    list, abbreviate(20) sepby(ID)
    Code:
    . list, abbreviate(20) sepby(ID)
    
         +-------------------------------------------------------+
         |      ID        org_name   datasource   budet_category |
         |-------------------------------------------------------|
      1. | 1234456    Helping Hand      Survey1                2 |
      2. | 1234456    Helping Hand      Survey2                2 |
      3. | 1234456    Helping Hand      Survey3                2 |
         |-------------------------------------------------------|
      4. | 9089081   Save Children      Survey1                5 |
      5. | 9089081   Save Children      Survey2                5 |
      6. | 9089081   Save Children      Survey3                5 |
         +-------------------------------------------------------+

    Comment


    • #3
      Thanks, @William! I am sorry, I didn't format the data command well into the text (though I did use dataex). I really appreciate your help and others @Statalist.

      Comment

      Working...
      X