Dear Stata users,
I have poor programming skills and I have benefited from the Statalist treasures for many years. The time has come for me to post since I could not find any answer, even after spending a rather shocking amount of time on what is probably a simple question for many of you.
I have a dataset with 12 variables (month1-12). They take the value 0 (no activity) to 3 (high activity) for small businesses.
I want to put a condition inside a loop that could be written literally: "all the previous month variables are equal to 0"
For now the code looks like this:
forvalues i=1/12{
replace outcome_max=$outcome if month`i'=1 & month1-month`=`i' - 1'==0
replace oucome_min=outcome_max/ratio if month`i'=1 & month1-month`=`i' - 1'==0
replace outcome_avg=(outcome_max+oucome_min)/2 if month`i'=1 & month1-month`=`i' - 1'==0
}
Of course, the condition "month1-month`=`i' - 1'==0 " does not work (invalid syntax), since it is but a naive translation of the sentence "all previous month =0"
How can I make a flexible reference to all previous month ?
In other words, for month 3 the condition would be month1==0 & month2==0, and so on.
It is probably some simple local macro that I cannot think of for the moment, which would allow nesting all the "==0".
Many thanks in advance for your time & undoubtedly powerful skills
Axel
I have poor programming skills and I have benefited from the Statalist treasures for many years. The time has come for me to post since I could not find any answer, even after spending a rather shocking amount of time on what is probably a simple question for many of you.
I have a dataset with 12 variables (month1-12). They take the value 0 (no activity) to 3 (high activity) for small businesses.
I want to put a condition inside a loop that could be written literally: "all the previous month variables are equal to 0"
For now the code looks like this:
forvalues i=1/12{
replace outcome_max=$outcome if month`i'=1 & month1-month`=`i' - 1'==0
replace oucome_min=outcome_max/ratio if month`i'=1 & month1-month`=`i' - 1'==0
replace outcome_avg=(outcome_max+oucome_min)/2 if month`i'=1 & month1-month`=`i' - 1'==0
}
Of course, the condition "month1-month`=`i' - 1'==0 " does not work (invalid syntax), since it is but a naive translation of the sentence "all previous month =0"
How can I make a flexible reference to all previous month ?
In other words, for month 3 the condition would be month1==0 & month2==0, and so on.
It is probably some simple local macro that I cannot think of for the moment, which would allow nesting all the "==0".
Many thanks in advance for your time & undoubtedly powerful skills
Axel
Comment