Dear Statalist
I want to generate a variable that contains the maximum beginning market values of firms in the same decile, year and month and traded in a certain stock exchange (abbreviated as N)
*First I have created my deciles for firms that are traded in N (New York Stcok Exchange), the variable for the exchange identifier is primexch. Deciles are created on the basis of beginning market values, crmv is the firm market value:
gen l_crmv=l.crmv
egen sizedecile = xtile(crmv) if primexch=="N", by(yr mth) p(1/9)
* Now I wanted to create a variable that will contain the maximum beginning market value for each decile in a given year and month. If I do not use a loop, I would think that I should do the following:
sum l_crmv if sizedecile==1 & yr==1990 & mth==1
scalar max1=r(max)
sum l_crmv if sizedecile==1 & yr==1990 & mth==2
scalar max2=r(max)
.....
....
....
sum l_crmv if sizedecile==2 & yr==1990 & mth==1
**Obviously that is really time consuming especially when I have over 20 years in the sample and I want to do that for each decile in each month of each year !!!
I tried to create a loop:
forval j = 1/N {
gen max`j'=.
}
levelsof sizedecile, local(levels)
foreach x of local levels {
foreach z of numlist 1990/2012 {
foreach y of numlist 1/12 {
sum l_crmv if sizedecile== `x' & yr==`z' & mth==`y'
forval j = 1/N {
scalar max`j' = r(max)
}
}
}
}
The loop is not correct, as the loop in loop only summarizes the beginning market values for each decile in a given year and month but can't generate the variable I look for. Also first loop is inappropriate !
I will appreciate you suggestions ! I am sure there should be a simple way, but it is out of my head !
Many thanks in advance
Ahmed
I want to generate a variable that contains the maximum beginning market values of firms in the same decile, year and month and traded in a certain stock exchange (abbreviated as N)
*First I have created my deciles for firms that are traded in N (New York Stcok Exchange), the variable for the exchange identifier is primexch. Deciles are created on the basis of beginning market values, crmv is the firm market value:
gen l_crmv=l.crmv
egen sizedecile = xtile(crmv) if primexch=="N", by(yr mth) p(1/9)
* Now I wanted to create a variable that will contain the maximum beginning market value for each decile in a given year and month. If I do not use a loop, I would think that I should do the following:
sum l_crmv if sizedecile==1 & yr==1990 & mth==1
scalar max1=r(max)
sum l_crmv if sizedecile==1 & yr==1990 & mth==2
scalar max2=r(max)
.....
....
....
sum l_crmv if sizedecile==2 & yr==1990 & mth==1
**Obviously that is really time consuming especially when I have over 20 years in the sample and I want to do that for each decile in each month of each year !!!
I tried to create a loop:
forval j = 1/N {
gen max`j'=.
}
levelsof sizedecile, local(levels)
foreach x of local levels {
foreach z of numlist 1990/2012 {
foreach y of numlist 1/12 {
sum l_crmv if sizedecile== `x' & yr==`z' & mth==`y'
forval j = 1/N {
scalar max`j' = r(max)
}
}
}
}
The loop is not correct, as the loop in loop only summarizes the beginning market values for each decile in a given year and month but can't generate the variable I look for. Also first loop is inappropriate !
I will appreciate you suggestions ! I am sure there should be a simple way, but it is out of my head !
Many thanks in advance
Ahmed
Comment