Search code examples
stata

creating tempfiles in a forvalue loop


I have a dataset where I want to get the sample size for each race category by year and sector (occupation). My race variable name is wbho and the values are White (1), Black(2), Hispanic(3), Others(3). I wrote a forval loop to do this, but I can't save them as different tempfiles inside the loop (since the race variable name is the same for each collapse result). In the end, I want to append the 4 tempfiles. How can I do that? Here is my loop:

forval i=1/4 {
use ${data}wbho.dta, clear
gcollapse (count) wbho if wbho==`i', by (year docc03)
}

After each tempfile is saved, I was going to append like this:

 use `part1', clear 
append using `part2'
append using `part3'
append using `part4'
tempfile overall
save `overall'


Solution

  • I don't follow why you're looping in the first place.

    use ${data}wbho.dta, clear
    contract wbho year docc03