Scott Merryman has given you code for a calculate,
put in locals and print style.
Another approach is to get results into variables.
You might even want to show graphs, or use the
results downstream. The main trick is to copy
non-missing values over missing values, so that
you can calculate ratios in place:
bysort rep78 (foreign) : egen f_mean = mean(price) if foreign
by rep78 : egen d_mean = mean(price) if !foreign
bysort rep78 (f_mean) : replace f_mean = f_mean[1]
bysort rep78 (d_mean) : replace d_mean = d_mean[1]
gen ratio = d_mean / f_mean
tabdisp rep78, c(ratio)
Nick
[email protected]
Christer Thrane
> Okay, a second try, then.
>
> Using the auto data, I wish to calculate the foreign/domestic
> price ratioes
> for three different levels of repair record.
>
> use c:\data\auto.dta, clear
> (1978 Automobile Data)
>
> .
> . recode rep 1/3=3
> (rep78: 10 changes made)
>
> . tab rep
>
> Repair |
> Record 1978 | Freq. Percent Cum.
> ------------+-----------------------------------
> 3 | 40 57.97 57.97
> 4 | 18 26.09 84.06
> 5 | 11 15.94 100.00
> ------------+-----------------------------------
> Total | 69 100.00
>
> . drop if rep==.
> (5 observations deleted)
>
> . sort rep
>
> . by rep: oneway price foreign, t
>
> --------------------------------------------------------------
> ------------------------------------------------
> -> rep78 = 3
>
> | Summary of Price
> Car type | Mean Std. Dev. Freq.
> ------------+------------------------------------
> Domestic | 6,358.405 3,526.92 37
> Foreign | 4,828.667 1,285.613 3
> ------------+------------------------------------
> Total | 6,243.675 3,425.43 40
>
> rest of output omitted
> --------------------------------------------------------------
> ------------------------------------------------
> -> rep78 = 4
>
> | Summary of Price
> Car type | Mean Std. Dev. Freq.
> ------------+------------------------------------
> Domestic | 5,881.556 1,592.019 9
> Foreign | 6,261.444 1,896.092 9
> ------------+------------------------------------
> Total | 6,071.5 1,709.608 18
>
> rest of output omitted
> --------------------------------------------------------------
> ------------------------------------------------
> -> rep78 = 5
>
> | Summary of Price
> Car type | Mean Std. Dev. Freq.
> ------------+------------------------------------
> Domestic | 4,204.5 311.83409 2
> Foreign | 6,292.667 2,765.629 9
> ------------+------------------------------------
> Total | 5,913 2,615.763 11
>
> rest of output omitted
>
> .
> end of do-file
>
>
> ... and here I must stop the do-file and obtain the results
> above in the
> following calcualtions:
>
> . dis 6358/4828
> 1.3169014
>
> . dis 5881/6261
> .93930682
>
> . dis 4204/6292
> .66815003
>
> Question: Is it possible to do this in one operation?
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/