Dear Stata users,

My following question may be easy but I could not find a direct command to achieve it. We all know there are two command -- contract & collapse -- that are powerful in creating frequencies and summary statistics. What I want is a way to create frequencies of some variables, and in the same time, to create summary statistics of another certain variable. It would be in someway a combination of contract and collapse. The result will like this:
Code:
sysuse bplong
preserve
contract sex agegrp when
restore
preserve
collapse bp, by( sex agegrp when )
restore
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte(sex agegroup when freq) float bp
0 1 1 20 153.45
0 1 2 20 146.45
0 2 1 20 159.05
0 2 2 20 157.25
0 3 1 20  165.3
0 3 2 20 162.85
1 1 1 20  149.9
1 1 2 20  142.2
1 2 1 20 151.15
1 2 2 20  144.3
1 3 1 20 159.85
1 3 2 20  155.1
end
label values sex sex
label def sex 0 "Male", modify
label def sex 1 "Female", modify
label values agegroup agegroup
label def agegroup 1 "30-45", modify
label def agegroup 2 "46-59", modify
label def agegroup 3 "60+", modify
label values when when
label def when 1 "Before", modify
label def when 2 "After", modify