<>
*******
clear
input id str20 item amount
1 material 1000
1 "material includes:A" 550
1 "material includes:B" 300
1 labor 400
1 manufacturing 200
2 material 800
2 labor 500
2 "labor includes:a" 300
2 "labor includes:b" 200
3 labor 600
3 material 700
end
bys id: gen order=_n
split item
bys id item1 (order): egen subtotal=total((_n>1)*amount)
bys id item1:gen byte keepobs=_N==1
bys id item1: replace keepobs=_n==1 & amount!=subtotal
bys id item1 (order): gen byte first=amount[1]==subtotal[1]
bys id item1 (order): gen byte dummy=(_n!=1) & (first)
keep if keepobs | dummy
sort id order
drop item1 item2 subtotal keepobs first dummy order
l, noo sepby(id)
*******
HTH
Martin
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of [email protected]
Sent: Mittwoch, 23. Dezember 2009 06:48
To: statalist
Subject: st: a specific data management problem
ear statalists,
I encountered a data management problem. Let take a exerpt of my data to
clarify my problem.
clear
input id str20 item amount
1 material 1000
1 "material includes:A" 550
1 "material includes:B" 300
1 labor 400
1 manufacturing 200
2 material 800
2 labor 500
2 "labor includes:a" 300
2 "labor includes:b" 200
3 labor 600
3 material 700
end
The characteristic of the data is that in every id the item(s) for which
there are details is(are) variational.
What I expect is as follows. By id, if the sum of the detailed item equals
the related total,drop the total observation and keep the detailed ones.
Otherwise,keep the total observation and drop the detailed ones.
Specifically, the result of the above data is
1 material 1000
1 labor 400
1 manufacturing 200
2 material 800
2 labor includes:a 300
2 labor includes:b 200
3 labor 600
3 material 700
Could anyone help me ? Thank you very much.
Best regards,
Rose.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/