Announcement

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

  • Generate duplicates?

    Hi Statalists,

    I have encountered a seemingly simple question while managing data with Stata 15.
    Basically I want var2 repeatingas long as var1 is the same.

    Thanks for any help in advance!
    Yingyi

    data I have:
    var1 var2
    1 a
    1 .
    1 .
    1 .
    2 b
    2 .
    2 .
    3 c
    3 .

    data I want:
    var1 var2
    1 a
    1 a
    1 a
    1 a
    2 b
    2 b
    2 b
    3 c
    3 c

  • #2
    Code:
    by var1 (var2), sort: replace var2 = var2[1]
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thanks for the quick reply and nice notice Clyde. The solution worked!

      Thanks again,
      Yingyi

      Comment

      Working...
      X