**************************************
***** Stata Self-Learning Course *****
****** University of Goettingen ******
**************************************
********* Applied Programming ********
****** Exercise: More practice *******
**************************************
*** Task 1 ***
/*
For an analysis, you need the mean and the median of population groups from the
census.dta, so you coded:
*/
sysuse census, clear
qui su poplt5, d
di "The median of poplt5 is `r(p50)', the mean is `r(mean)'"
qui su pop5_17, d
di "The median of pop5_17 is `r(p50)', the mean is `r(mean)'"
qui su pop18p, d
di "The median of pop18p is `r(p50)', the mean is `r(mean)'"
qui su pop65p, d
di "The median of pop65p is `r(p50)', the mean is `r(mean)'"
/*
Now, you realize that this would be a nice command in general, so you decide
to transform this code into a program.
*/
* a) Write a program which takes numeric variables as input and then displays
* their median and mean.
* b) Extend the program such that it stores the median and mean in the return
* list. Be aware that you need different names for each variable.
*** Task 2 ***
/*
Your friend had a similar problem, but decided to put the values into matrices:
*/
sysuse census, clear
mat A = J(4,2,.)
qui su poplt5, d
mat A[1,1] = `r(p50)'
mat A[1,2] = `r(mean)'
qui su pop5_17, d
mat A[2,1] = `r(p50)'
mat A[2,2] = `r(mean)'
qui su pop18p, d
mat A[3,1] = `r(p50)'
mat A[3,2] = `r(mean)'
qui su pop65p, d
mat A[4,1] = `r(p50)'
mat A[4,2] = `r(mean)'
mat colnames A = "Median" "Mean"
mat rownames A = "poplt5" "pop5_17" "pop18p" "pop65p"
mat list A
/*
You like this code and decide to transform it into a program
*/
* a) Write a program which takes numeric variables as input and then displays
* their median and mean in a matrix.
* b) Instead of displaying the variable names in the matrix, let the program
* display their labels.