I am currently analyzing survey data where I have data on e.g. the expected unemployment rate and other things like the respondents' state and the period (monthly) they participated in the survey. I am now trying to run a foreach loop over variables like the expected unemployment rate to create a new variable where I can store theaverage expected unemployment rate per period per month, so that I can continue working on the state level instead of the individual level.
Variables
state - String of respondents' state (abbreviated), locally stored as stat
period - Integer that ranges from 1-27 (I recorded months from multiple years), locally stored as per
unemp_rate - Expected unemployment rate
unemp_month - Variable where I want to store the means
My Concern
Why I am posting here is because that code worked well yesterday but when I used it today it does not compute. I locally store state and period and then directly after I enter the loop but nothing happens; the loop does not compute and no error message is displayed. I would appreciate it if anyone could help me out with what might be the reason for that.
Code:
levelsof state, local(stat) levelsof period, local(per) foreach state of local stat{ foreach period of local per{ quietly sum unemp_rate if state==`"`state'"' & period==`period' replace unemp_month = r(mean) if state==`"`state'"' & period==`period' } }
Stata Version 16.1
0 Response to Foreach loop does not compute
Post a Comment