Hi All,
In an earlier question I could not make it so clear what I need.
I used this code below to get the maximum values I have:
The results were new variables equal to
My question is about the "`r(max)'" , how can I use it in the code below:
which will give me something like this:
----------------------- copy starting from the next line -----------------------
when I run this code:
or
the results are different:
----------------------- copy starting from the next line -----------------------
------------------ copy up to and including the previous line ------------------
Please let me know if I should change something in my code above .
Thanks, Max
In an earlier question I could not make it so clear what I need.
I used this code below to get the maximum values I have:
Code:
bysort id_com year: gen all=sum(v_local!=v_local[_n-1])
su all, meanonly
forval j = 1/`r(max)'{
egen v_local`j'=mean(cond(all==`j', v_local, .)), by (id_com year)
}
Code:
v_local1, v_local2, v_local3, and v_local4
Code:
gen trade_var =1 if domestic == v_local1 | domestic == v_local2 | domestic == v_local3|domestic == v_local4 replace trade_var==0 if trade_var==.
----------------------- copy starting from the next line -----------------------
Code:
* Example generated by -dataex-. For more info, type help dataex clear input int year byte id_com str1 department byte(v_local domestic) float(all v_local1 v_local2 v_local3 v_local4 trade_var) 2005 1 "A" 11 11 1 11 12 14 11 1 2005 1 "A" 11 12 1 11 12 14 11 1 2005 1 "A" 11 13 1 11 12 14 11 0 2005 1 "A" 11 14 1 11 12 14 11 1 2005 1 "B" 12 11 2 11 12 14 11 1 2005 1 "B" 12 12 2 11 12 14 11 1 2005 1 "B" 12 13 2 11 12 14 11 0 2005 1 "B" 12 14 2 11 12 14 11 1 2005 1 "C" 14 11 3 11 12 14 11 1 2005 1 "C" 14 12 3 11 12 14 11 1 2005 1 "C" 14 13 3 11 12 14 11 0 2005 1 "C" 14 14 3 11 12 14 11 1 2005 1 "D" 14 11 3 11 12 14 11 1 2005 1 "D" 14 12 3 11 12 14 11 1 2005 1 "D" 14 13 3 11 12 14 11 0 2005 1 "D" 14 14 3 11 12 14 11 1 2005 1 "E" 11 11 4 11 12 14 11 1 2005 1 "E" 11 12 4 11 12 14 11 1 2005 1 "E" 11 13 4 11 12 14 11 0 2005 1 "E" 11 14 4 11 12 14 11 1 end
when I run this code:
Code:
bysort id_com year: gen all=sum(v_local!=v_local[_n-1])
su all, meanonly
forval j = 1/`r(max)'{
egen v_local`j'=mean(cond(all==`j', v_local, .)), by (id_com year)
}
scalar max = r(max)
local newvars `newvars' v_local`j'
gen trade_var=1 if domestic == v_local`j'
replace trade_var==0 if trade_var==.
Code:
gen trade_var=1 if domestic == `newvars' replace trade_var==0 if trade_var==.
----------------------- copy starting from the next line -----------------------
Code:
* Example generated by -dataex-. For more info, type help dataex clear input int year byte id_com str1 department byte(v_local domestic) float(all v_local1 v_local2 v_local3 v_local4 trade_var) 2005 1 "A" 11 11 1 11 12 14 11 1 2005 1 "A" 11 12 1 11 12 14 11 0 2005 1 "A" 11 13 1 11 12 14 11 0 2005 1 "A" 11 14 1 11 12 14 11 0 2005 1 "B" 12 11 2 11 12 14 11 0 2005 1 "B" 12 12 2 11 12 14 11 1 2005 1 "B" 12 13 2 11 12 14 11 0 2005 1 "B" 12 14 2 11 12 14 11 0 2005 1 "C" 14 11 3 11 12 14 11 0 2005 1 "C" 14 12 3 11 12 14 11 0 2005 1 "C" 14 13 3 11 12 14 11 0 2005 1 "C" 14 14 3 11 12 14 11 1 2005 1 "D" 14 11 3 11 12 14 11 0 2005 1 "D" 14 12 3 11 12 14 11 0 2005 1 "D" 14 13 3 11 12 14 11 0 2005 1 "D" 14 14 3 11 12 14 11 1 2005 1 "E" 11 11 4 11 12 14 11 1 2005 1 "E" 11 12 4 11 12 14 11 0 2005 1 "E" 11 13 4 11 12 14 11 0 2005 1 "E" 11 14 4 11 12 14 11 0 end
Please let me know if I should change something in my code above .
Thanks, Max

Comment