Announcement

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

  • Keep previous sort order when using "by x: egen"

    Hello,

    I am trying to mark duplicate values of a variable, var1, using the following code:

    by var1: gen dup = cond(_N==1,0,_n)

    yet, i do not want the ordering of what ends up being dup=1, dup=2, .... to be arbitrary.

    So I thought of presorting the data on another variable, var2, to remove the randomness, such that the complete code would look like:

    gsort var1, -var2
    by var1: gen dup = cond(_N==1,0,_n)

    But when I do this, it appears that the second command reshuffles the observations in an arbitrary manner.

    How can I get around this?

    Thank you.


  • #2
    The behavior you describe is puzzling. But I am quite certain that what you are showing is not the actual code you tried. I am certain of that because -gsort var1, -var2- is illegal syntax and you would have gotten an error message, and the subsequent command would never have been executed. So rather than me or anybody else wasting time trying to figure out what is going on based on inaccurate information, please help us help you by:

    1. Post an example data set that exhibits this problem. Use the -dataex- command to do that. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- 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.

    2. Post the exact code that you are using, and show exactly any output that Stata gives you as a result. Do this by copying to your clipboard from your do file or the results window or log file and then pasting here in the forum between code delimiters. Do not edit this in any way!!! There is no such thing as a "minor" change in code.

    Comment

    Working...
    X