Looping over multiple variables

#1
Hi! I am trying to write a code for a number of variables within a dataset and I cannot seem to make it work. I tried to make a macro that included all of the variables I wanted to use. Then I tried a foreach code to transpose each variable from string format to a dummy variable. When I run this in stata, no errors occur however nothing happens. No new variables are created. Any assistance you can provide is appreciated. Thanks!

local sectormonth mainsectorsworkedinlastmonthcrop mainsectorsworkedinlastmonthlive mainsectorsworkedinlastmonthfore mainsectorsworkedinlastmonthfish mainsectorsworkedinlastmonthchar mainsectorsworkedinlastmonthmini mainsectorsworkedinlastmonthmanu mainsectorsworkedinlastmonthwate mainsectorsworkedinlastmonthcons mainsectorsworkedinlastmonthmoto mainsectorsworkedinlastmonthtran mainsectorsworkedinlastmonthacco mainsectorsworkedinlastmonthit mainsectorsworkedinlastmonthfina mainsectorsworkedinlastmontharts mainsectorsworkedinlastmonthhuma mainsectorsworkedinlastmontheduc mainsectorsworkedinlastmonthothe mainsectorsworkedinlastmonthspla mainsectorsworkedinlastmonthgove mainsectorsworkedinlastmonthadmi mainsectorsworkedinlastmonthprof mainsectorsworkedinlastmonthreal

foreach mainsectorsworkedinlastmonth in `sectormonth' {
gen sector_1mo_`sectormonth'=0
replace sector_1mo_`sectormonth'=1 if `sectormonth'=="TRUE"
label variable sector_1mo_`sectormonth' "`sectormonth'"
label value sector_1mo_`sectormonth' binary_variable
}
 
#2
From a quick glance, I believe you should be using
`mainsectorsworkedinlastmonth' where you have `sectormonth'
inside your loop.

Without some toy data, I can't offer much more.
 
#3
mmm, if what you have written in your code is really what you have posted here
local sectormonth mainsectorsworkedinlastmonthcrop mainsectorsworkedinlastmonthlive mainsectorsworkedinlastmonthfore mainsectorsworkedinlastmonthfish mainsectorsworkedinlastmonthchar mainsectorsworkedinlastmonthmini mainsectorsworkedinlastmonthmanu mainsectorsworkedinlastmonthwate mainsectorsworkedinlastmonthcons mainsectorsworkedinlastmonthmoto mainsectorsworkedinlastmonthtran mainsectorsworkedinlastmonthacco mainsectorsworkedinlastmonthit mainsectorsworkedinlastmonthfina mainsectorsworkedinlastmontharts mainsectorsworkedinlastmonthhuma mainsectorsworkedinlastmontheduc mainsectorsworkedinlastmonthothe mainsectorsworkedinlastmonthspla mainsectorsworkedinlastmonthgove mainsectorsworkedinlastmonthadmi mainsectorsworkedinlastmonthprof mainsectorsworkedinlastmonthreal
it seems like it is showing nothing because you have not defined what the local variable should contain.
That is, you should have written:
local sectormonth "and all the other variables"

If you try again to run your original piece of code typing first set trace on you will easily see whether it was a problem of not having specified appropriately the variables.