Announcement

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

  • Summarizing numerical variable by a categorical variable for each available year

    I am working with three variables: return, type, and year. How can I obtain summary statistics of return by type for each available year? Put another away, I want something like
    tabstat return, by( type ) for each year.

    Thank you.

  • #2
    The direct solution is:
    Code:
    levelsof year, local(years)
    foreach y of local years {
        display `y'
        tabstat return if year == `y', by(type)
    }
    Another approach that gives the results in a layout with years crossed with type would be:

    Code:
    table year type, c(mean return)
    If you want the data in memory to have these results, rather than outputing them in a table or tables

    Code:
    by type year, sort: egen mean_return = mean(return)
    Note: In the future, please show examples of your data when asking for code. While the above solutions are fairly generic and will work with most data sets that would meet your description, the correct code often depends on details of the data that are not adequately described in words. The helpful and simple way to show example data is to use the -dataex- command. Please read Forum FAQ #12 for more information about this topic.

    Comment


    • #3
      Codes work with my data. Thanks a lot Clyde Schechter! I will keep the advice in mind.

      Comment


      • #4
        This should also work:

        Code:
        egen group = group(year type), label
        tabstat return, by(group)

        Comment

        Working...
        X