Montag, 31. Oktober 2011

Remove the last word in Stata

You have a dataset that you want to aggregate over several levels in a loop. At every loop you want to drop the last group.
The regexm command can drop the last word of a macro in every loop:

regexm("`agglevel'", "([a-z A-Z]+)[ ]([a-zA-Z]+)")

an nice and short introduction for regexm can be found here.

That is the code snippet I use:

(works in STATA11)

foreach var of varlist saledate mofd wofd year{
preserve

local agglevel total company store category supplier pricelevel
local facts (sum) items pricesold pricein pricelabel
local words=wordcount("`agglevel'")

forvalues v=1/`words'{
local agglevel_file=subinstr("`agglevel'"," ","_",.)
di "`facts' ,by(`agglevel' `var')"
qui collapse `facts' ,by(`agglevel' `var')
qui my_popagg
qui add_mofd
save `sample'_agg_`agglevel_file',replace
qui gen x=""
di regexm("`agglevel'", "([a-z A-Z]+)[ ]([a-zA-Z]+)")
*will produce an error on the last level -ignore safely
cap local agglevel=regexs(1)
local agglevel="`agglevel'"
}
restore
}




Keine Kommentare:

Kommentar veröffentlichen