There is no exercise with collapse, even though it is mentioned in the "Overview" and "Key points" sections as well.
It could be included in the "Aggregate" section, after
egen average_census_year = mean(censusyear), by(incomegroup)
in the following way:
preserve
collapse (mean) average_census_year=censusyear, by(incomegroup)
save "data/derived/censusyear.dta", replace
restore
This gives the same result as the egen command before, with the difference that it saves the aggregate statistics to a separate dta file. Students can load the generated datafile and check to see. This is a typical example of the collapse as it is often used together with preserve and restore. With this exercise, we can also show the importance of preserve and restore, and highlight that save should be used together with the option replace.
What is the problem?
There is no exercise with
collapse
, even though it is mentioned in the "Overview" and "Key points" sections as well. It could be included in the "Aggregate" section, afteregen average_census_year = mean(censusyear), by(incomegroup)
in the following way: preserve collapse (mean) average_census_year=censusyear, by(incomegroup) save "data/derived/censusyear.dta", replace restoreThis gives the same result as the
egen
command before, with the difference that it saves the aggregate statistics to a separatedta
file. Students can load the generated datafile and check to see. This is a typical example of thecollapse
as it is often used together withpreserve
andrestore
. With this exercise, we can also show the importance ofpreserve
andrestore
, and highlight thatsave
should be used together with the optionreplace
.Location of problem (optional)
Episode 3