Announcement

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

  • Collapse command and finding min and max values

    Hello,
    I am working with a longitudinal dataset which contains the following variables:
    a.) temperature (varname: temp) collected at b.)various weather stations (varname: stationID) during c.)year XXX (varname: year) and in d.) county XX (varname: countyID).

    For each year and for each county, I want to do the following: 1.) assign the mean temperature data from all of the weather stations within the county, 2.) find and assign to each county the minimum temp measured at a weather station in that county, and 3.) find and assign to each county the maximum temperature measured at a weather station in that county.

    To find the mean temp, I used this command:

    line 1: gen one=1
    line 2: collapse (count) n_station=one (first) countyID ///
    line 3: (mean) mean_temp=temp, by(countyID year)

    but, similar code to find the minimum (line4) and maximum (line 5)don't work:

    line 1: gen one=1
    line 2: collapse (count) n_station=one (first) countyID ///
    line 3: (mean) mean_temp=temp, by(countyID year)
    line 4: (min) min_temp=temp, by(countyID year)
    line 5: (max) max_temp=temp, by(countyID year)

    Any suggestions on how I can fix this code?
    Thank you

  • #2
    Since you edited in line 1:, line2:, etc. the rest of us are left to guess what you actually typed. And "don't work" could mean dozens of different things, so you are really stretching everyone's telepathy skills with your post.

    I'll make a low-confidence guess: I'm guessing that what you ran is:

    Code:
    collapse (count) n_station=one (first) countyID ///
    (mean) mean_temp=temp, by(countyID year)
    (min) min_temp=temp, by(countyID year)
    (max) max_temp=temp, by(countyID year)
    There are several things wrong with that.

    1. You can't have more than one -by()- specified in a single -collapse- command.
    2. You didn't put in a continuation marker (///) at the end of any but the first line.. You need to have it on each line except the last.
    3. You can't have (first) countyID and also have countyID in the -by()- option. Stata won't permit it, but even if it were allowed, it's pointless since Stata will preserve the variables specified in the -by()- option.

    So I think what you need is:

    Code:
    collapse (count) n_station=one ///
    (mean) mean_temp=temp ///
    (min) min_temp=temp ///
    (max) max_temp=temp, by(countyID year)
    Please read the Forum FAQ for excellent advice on how to write posts in ways that maximize your chances of getting a helpful and timely response. In particular:

    1. Show exactly the code you ran with no edits at all. There are no unimportant details.
    2. Show exactly the response that you got from Stata--any output or error messages--also with no edits.
    3. Never say "didn't work"--it provides no information.
    4. If it isn't obvious, explain how what you got from Stata differed from what you were hoping for.

    While you're reading the FAQ, also pay special attention to #12 for instructions on showing code and output bound in code delimiters, and using the -dataex- command when showing data examples.

    Comment

    Working...
    X