Announcement

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

  • Collapse over two variables

    Hi I am trying to create a graph with time(by month and year) on the x and pricegal on the y. My goal is to average pricegal from the different locations, so that for each month of each year I have only one data entry(so I can make a linegraph)

    Attached is part of the data

    any advice is appreciated
    Attached Files

  • #2
    Well, you actually answered your own question in the title of your post
    Code:
    collapse (pricegal), by(YEAR MONTH)
    will get you one observation for each calendar month, containing the average of pricegal across all locations.

    But to graph it, you need a real time variable. The question is not how to collapse over two variables, it's how to convert an unworkable pair of variables into a single variable for the month and year:

    Code:
    gen mdate = ym(YEAR, MONTH)
    format mdate %tm
    Then you can
    Code:
    graph twoway connect pricegal mdate, sort
    Here's some more important advice. Read the Forum FAQ for great advice on the most effective ways to post here. Pay particular attention to #12 where you will learn that screenshots are probably the least useful way to show data. It worked out OK here because although many turn out unreadable, yours was easy to read, the solution didn't rely on information that couldn't be gleaned from a screenshot, and the code involved was simple enough that there was no need to test it on example data. In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 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.

    Comment

    Working...
    X