Announcement

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

  • Is there any difference between "Sort X Y" versus. "Bysort X Y: gen A=1"

    I know in Bysort you have to generate something. But other than that will using Sort X Y vs. Bysort X Y make any changes to the structure of my data?

    For example, if I wanted to arrange my dataset in the following way:

    1. First by CompanyID in an ascending order.

    2. Within the company ID, I want the data to be arranged by Year next.

    From what I understand "Sort CompID Year" will arrange my dataset in that order. Will "Bysort CompID Year: gen A=1" do the same under all circumstances?
    Last edited by Tahseen Hasan; 14 Dec 2018, 08:16.

  • #2
    After typing - help bysort - in the command window, we get this information:

    . sort rep78
    . by rep78: tabulate foreign

    Equivalent to above two commands
    . by rep78, sort: tabulate foreign

    Equivalent to above command
    . bysort rep78: tabulate foreign
    Best regards,

    Marcos

    Comment


    • #3
      No; you don't have to generate anything with bysort. In principle you may do many other things with it, sensible or silly. But a command starting

      Code:
      bysort X Y: 
      will sort your data, regardless of whether the following command works or is even legal.

      Comment


      • #4
        @Nick Cox: So just to confirm, the following commands will structure my data in the same exact way?

        1. Sort ID Date

        2. Bysort ID Date

        Comment


        • #5
          That is so. This also follows from Marcos Almeida's examples.

          Comment


          • #6
            Understood. Thank you so much for the help

            Comment

            Working...
            X