Announcement

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

  • Group by in stata

    Hi everyone, Im trying to use a panel dataset in stata but it does not have the right format yet. (see below for an example)
    Firm_code Date Quantity Price
    1 24Dec2004 17:00:00 205 10
    1 24Dec2004 17:00:00 300 5.3
    1 24Dec2004 17:00:00 305 8
    1 24Dec2004 19:00:00 67 8.2
    2 24Dec2004 17:00:00 728 8
    2 24Dec2004 19:00:00 72 3
    3 25Dec2004 17:00:00 32 12
    .. .. .. ..
    I want to join get the average price for every similar date per firm but the cumulative quantity for each firm per date.

    I have search everywhere but cannot find a solution to this problem...

    The aim of this is to be able to use this dataset as a panel in stata using: "xtset firmcode date" but as there is repetitive date for some individuals (firm) it doesn't work.

    Does someone know how to do this or if it is possible?

    In advance, I thank you for your help

    Best regards,

    Sybille
    Last edited by Sybille Roemer; 07 Dec 2021, 10:31.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte firm_code float date int quantity float price
    1 1.4195268e+12 205  10
    1 1.4195268e+12 300 5.3
    1 1.4195268e+12 305   8
    1  1.419534e+12  67 8.2
    2 1.4195268e+12 728   8
    2  1.419534e+12  72   3
    3  1.419613e+12  32  12
    end
    format %tc date
    
    collapse (sum) quantity (mean) price, by(firm_code date)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. 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.

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

    Comment

    Working...
    X