I am trying to create a programme that will do "something" on numeric variables and "something else" on factor variables. Ideally, the programme would check whether the variable was numeric or a factor. If it is numeric, then it would summarise the variable. If it was a factor variable, then it would create dummy variables for each of its levels and then perform summarise. But this requires the programme to identify whether the variable is numeric or factor (from the user specifying the "i." prefix before the variable name).
For the factor variables, the programme would ideally create dummy variables and 'summarise' would be performed on each of the dummy variables.
I know there is a way to use the "s(fvops)" macro but I am not sure it is the best approach in this case?
In the above, "mbsmoke" and "mage" would be numeric variables and so should be summarised as is. But as "i.prenatal" is a factor variable the programme should create dummy variables and then perform summarise on each of the dummy variables.
Any help is much appreciated!
For the factor variables, the programme would ideally create dummy variables and 'summarise' would be performed on each of the dummy variables.
I know there is a way to use the "s(fvops)" macro but I am not sure it is the best approach in this case?
Code:
* Load the data
use "http://www.stata-press.com/data/r14/cattaneo2.dta", clear
* Define the programme
capture program drop myprogramme
program define myprogramme
syntax varlist(numeric fv)
local fvops = "`s(fvops)'" == "true"
if `fvops' {
tab variable, gen(_variable)
local k = [number of levels of factor variable]
forval in 1/k {
summarize
}
}
if fvops = "`s(fvops)'" == "false" {
sum `varlist'
}
end
* Run the programme
myprogramme mbsmoke mage i.prenatal
In the above, "mbsmoke" and "mage" would be numeric variables and so should be summarised as is. But as "i.prenatal" is a factor variable the programme should create dummy variables and then perform summarise on each of the dummy variables.
Any help is much appreciated!

Comment