Announcement

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

  • Generate mean depending on variable specification

    Dear folks,

    I am currently working on a dataset that contains sales data of items in stores across the country. Each row represents a purchase and contains information on the amount of product name, product brand, units sold, unit price, date, store name, store brand, state of store, etc. For the sake of price analysis, I am currently trying to create a mean price across the whole observational period. However, as prices obviously differ across brands, store types and time, one has to create several individual means that control for each of these variable specifications.
    More precisely:

    I want Stata to create individual mean prices for each of all observations that share:

    The same product (variable 1)
    The same store type (variable 2)
    The same state of store (variable 3)
    The same week of purchase (variable 4)
    The same year of purchase (variable 5)

    I have some working knowledge with creating new variables, means, etc. in Stata. However, I haven't managed to do get it done if the mean depends on more than one variable as in the case above.

    Many thanks in advance!

  • #2
    Jason:
    some approaches to accomplish what you're after:
    Code:
    . sysuse auto.dta
    (1978 Automobile Data)
    
    . sum price if foreign==1 & rep78==3
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          3    4828.667    1285.613       3895       6295
    
    . egen foreign_rep78=group(foreign rep78)
    (5 missing values generated)
    
    . bysort foreign_rep78 : sum price
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = 1
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          2      4564.5    522.5519       4195       4934
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = 2
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          8    5967.625    3579.357       3667      14500
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = 3
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |         27    6607.074    3661.267       3291      15906
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = 4
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          9    5881.556    1592.019       3829       8814
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = 5
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          2      4204.5    311.8341       3984       4425
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = 6
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          3    4828.667    1285.613       3895       6295
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = 7
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          9    6261.444    1896.092       3995       9735
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = 8
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          9    6292.667    2765.629       3748      11995
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> foreign_rep78 = .
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |          5      6430.4    3804.322       3799      12990
    
    
    .
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Dear Carlo,

      that solves all my issues!

      Many thanks

      Comment

      Working...
      X