Do you mean something like this?
clear
input str4 id4 year var
1111 72 2
1112 72 1
1113 72 4
1121 72 3
1111 73 1
1112 73 2
1113 73 3
1121 73 4
end
gen id2 = substr(id4,1,2)
gen id3 = substr(id4,1,3)
stack id4 var year id2 var year id3 var year, into(id var year) clear
bysort year id: gen var1 =sum(var)
by year id: keep if _n ==_N
format id %-4s
drop _st var
l, sepby(year)
Scott
> -----Original Message-----
> From: [email protected] [mailto:owner-
> [email protected]] On Behalf Of [email protected]
> Sent: Wednesday, November 22, 2006 5:14 PM
> To: stata
> Subject: st: (st) Automatical aggregation from 4 digit codes to 3,2 digit
> codes?
>
> Hello. Stata Users.
> I need your help.
> I have panel set containing ID(4 digit), year and Var1, for simple case.
> I just want to add 2 or 3 digit level Var1 using aggregating up 4-digit
> level
> coded. For example, Currently my dataset looks like (from sort by year id)
>
> ID year var1
> 1111 72 2
> 1112 72 1
> 1113 72 4
> 1121 72 3
> . . .
> .. .. ..
>
> 1111 73 1
> 1112 73 2
> 1113 73 3
> 1121 72 4
>
>
> I would like to add var1 as follows:
>
> ID year var1
> 11 72 ???(7+??+....)
> 111 72 7 (2+1+3)
> 1111 72 2
> 1112 72 1
> 1113 72 4
> 112 72 ?? (3+?+?...)
> 1121 72 3
> . . .
> .. .. ..
> 11 73 ?? (6+?)
> 111 73 6 (1+2+3)
> 1111 73 1
> 1112 73 2
> 1113 73 3
> 112 73 ? (4+..)
> 1121 72 4
>
>
> As you see, the aggregating values should be added by year and ID.
> Since I have lot of codes, it would take forever to generate each 3 and 2
> digit
> level manually. It is possible way to create automatically 3 and 2 digit
> level
> code and following new var1 in the system?
> Thanks. Any comments and helpful remarks should be appreciated
> WT
*
* 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/