Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Trying to run a loop


From   "Hugh Colaco" <[email protected]>
To   statalist <[email protected]>
Subject   st: Trying to run a loop
Date   Tue, 17 Jun 2008 15:29:33 -0400

Hi Statalisters,

I am trying to obtain columns data31, data61 and data81 in the table
below. The values that I want to obtain for the 3 columns are shown
below. As an example, when code=1 and baseyear=yeara=1993, then the 4
values under variable data31 that I want to obtain (as shown in the
table) are:-

ln(((23.09-18.49)/68.78)+1) = 0.064738

ln(((35.56-18.49)/68.78)+1) = 0.221689

ln(((39.32-18.49)/68.78)+1) = 0.264554

ln(((46.58-18.49)/68.78)+1) = 0.342457


I tried the following code, but am "obviously" only getting the last
year's values (i.e. for data31, data61, and data81; .342457,
1.209815	and .738831 respectively (after rounding)).

I also want to generate a counter (1,2,3 and 4) for the 4 values that
I get above for each code. I need this because I need to -reshape- the
data to wide after I obtain the values.

Note that there is only one match of yeara and baseyear.


Code that I tried:-


foreach var of varlist data3 data6 data8 {

	local i=1

	gen `var'1 = .

	while `i'<5 {

	by code: replace `var'1 = ln(((`var'[_n+`i']-`var'[_n])/data6)+1) if
yeara==baseyear

	local i=`i'+1

	}

}




code	baseyear	yeara	data3	data6	data8	    data31	
data61	                   data81
1		1991						
1	1993	1992						
1	1993	1993	18.49	68.78	25.97	0.064738414	0.331036479	0.078005158
1	1993	1994	23.09	95.77	31.55	0.221688582	0.826987482	0.462451192
1	1993	1995	35.56	157.26	66.41	0.264553916	0.905473494	0.540703389
1	1993	1996	39.32	170.1	75.3	0.342456868	1.209814967	0.738830848
1	1993	1997	46.58	230.61	101.18			
1	1993	1998	51.97	295.34	141.87			
1	1993	1999	73.63	453.82	228.09			
1	1992	2000	101.47	474.12	236.94			
1	1992	2001						
1	1992	2002						
1	1992	2003						
1	1992	2004						
1	1992	2005						
1	1992	2006						
2		1991						
2	1994	1992						
2	1993	1993	0	11.74	1.3	0	0.831723323	0.045784109
2	1993	1994	0	26.97	1.85	0	1.021617175	0.081744836
2	1993	1995	0	32.61	2.3	0	2.848083691	0.75507369
2	1993	1996	0	202.57	14.54	0	2.939765654	1.180618733
2	1993	1997	0	222.02	27.79			
2	1993	1998	0	273.19	47.71			
2	1993	1999						
2		2000						
2		2001						
2		2002						
2		2003						
2		2004						
2		2005						
2		2006



Thanks in advance,


-- 
Hugh
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index