Announcement

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

  • How to replace the value with the first value within groups in a column in Stata?

    I have a dataset like this.
    I want to replace the value of project within number with the first value of the project column if the number are repeated. Otherwise, we don't need to repeat.
    Thank you!

    clear
    input str70 number str380 project
    "F-000073" "Meadowcreek Pavilion"
    "F-000073" "000 4502 - Parks Consolidated Construction Fund"
    "F-000509" "Environmental Projects"
    "F-000509" "000 4502 - Parks Consolidated Construction Fund"
    "F-000675" "Design Services for Various Parks"
    "F-000675" "000 4035 - Parks & Recreation Dedication Fund"
    "F-000703" "Swimming Pool Upgrades"
    "F-000703" "000 4502 - Parks Consolidated Construction Fund"
    "F-000706" "Busby Park Redevelopment"
    "F-000703" "Wright-Bembry Park"
    "F-000709" "000 4035 - Parks & Recreation Dedication Fund"
    "F-000713" "Metropolitan Multi-Service Center"
    "F-000713" "000 4502 - Parks Consolidated Construction Fund"
    "D-000180" "Alief MSC,Community Center and Library"
    end
    Last edited by smith Jason; 24 May 2022, 14:02.

  • #2
    Code:
    sort number, stable
    by number: replace project = project[1]

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Code:
      sort number, stable
      by number: replace project = project[1]
      Thank you for your response! I don't know why I need to use stable function.
      I know when I dealt with this dataset, the order seems change.

      Comment


      • #4
        The stable option is required because the variable number does not uniquely identify observations in the data set. If you were to just -sort number- Stata could scramble the order of the observations within the groups defined by number. The -stable- option prevents that from happening, thereby assuring that the correct value of project gets selected.

        Comment


        • #5
          Thank you!

          Comment

          Working...
          X