Hi, everyone. Lately I ran into a code writing problem. I am not very sure what is the difference between forvalues and foreach, and exactly how to use it in a loop.
I have a variable items which is a string variable, and I hope to get a corresponding average of p, for each unique value of this items, e.g for product aaa, bbb, ccc, etc.
The code I previously wrote could do the job to get the result, if I specify items == "bbb". But since the items actually contains hundreds of products, and I don't want to write manually the codes hundreds of times, so I tried to write a loop which looks like below. Obviously, my understanding of foreach and forvalues is still very clumsy after reading the stata manuals. As I click my do.file (below), the stata runs, gives no warnings in red, BUT there is no wanted result coming out. If I run the command -edit-, the ABC.dta looks still as it is. No observations deleted, no new variable is generated, and importantly, no wanted graphs are created.
use ABC.dta
levelsof items
local items
foreach l of local items{
if items == `l'
list if items == `l'
keep if items == `l'
egen avg_p = mean(p), by(country)
}
foreach l of local levels {
sort country
quietly by country: gen dup = cond(_N==1,0,_n)
drop if dup>1
save "`items`l''", replace
}
foreach l of local levels {
clear
use "`items`l''".dta
edit
merge m:1 country using income.dta
scatter p income, mlabel(country)|| lfit p income ||, title("items"`l'"and income")
graph save Graph "D:\STATA\items"`l'"and income.gph"
}
I wonder if anyone could help me check out where lies the problem. It is very hard for me to get any clue because stata runs the codes but produces no results and no warnings.
I would like to thank you for your advice in advance!
PS: how could I put the codes into a codes format which I often see in the forum? it looks very neat, but I don't know where is the icon for this function. can anyone help too?
Best,
Melanie
I have a variable items which is a string variable, and I hope to get a corresponding average of p, for each unique value of this items, e.g for product aaa, bbb, ccc, etc.
The code I previously wrote could do the job to get the result, if I specify items == "bbb". But since the items actually contains hundreds of products, and I don't want to write manually the codes hundreds of times, so I tried to write a loop which looks like below. Obviously, my understanding of foreach and forvalues is still very clumsy after reading the stata manuals. As I click my do.file (below), the stata runs, gives no warnings in red, BUT there is no wanted result coming out. If I run the command -edit-, the ABC.dta looks still as it is. No observations deleted, no new variable is generated, and importantly, no wanted graphs are created.
use ABC.dta
levelsof items
local items
foreach l of local items{
if items == `l'
list if items == `l'
keep if items == `l'
egen avg_p = mean(p), by(country)
}
foreach l of local levels {
sort country
quietly by country: gen dup = cond(_N==1,0,_n)
drop if dup>1
save "`items`l''", replace
}
foreach l of local levels {
clear
use "`items`l''".dta
edit
merge m:1 country using income.dta
scatter p income, mlabel(country)|| lfit p income ||, title("items"`l'"and income")
graph save Graph "D:\STATA\items"`l'"and income.gph"
}
I wonder if anyone could help me check out where lies the problem. It is very hard for me to get any clue because stata runs the codes but produces no results and no warnings.
I would like to thank you for your advice in advance!
PS: how could I put the codes into a codes format which I often see in the forum? it looks very neat, but I don't know where is the icon for this function. can anyone help too?
Best,
Melanie
Comment