Kit's solution only works if you have a single labeled numeric by
variable taking values 1,2,3,... with no missing values (as is the case
with -rep78-).
A more generic solution would be as follows...
program define fred, sortpreserve byable(recall)
version 9.2
marksample touse
if _by() {
foreach bv of local _byvars {
gsort -`touse'
local bval=`bv'[1]
cap confirm string var `bv'
if _rc {
local blab: value label `bv'
if "`blab'"=="" {
local bf: format `bv'
di "`bv' = " `bf' `bval'
}
else {
local blabval: label `blab' `bval'
di "`bv' = `blabval'"
}
}
else {
di "`bv' = `bval'"
}
}
}
end
David
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Kit Baum
Sent: 24 April 2006 20:36
To: [email protected]
Subject: st: byindex variables
Fred said
I have a program that is byable(recall). Suppose my byvariable is sex,
taking on 3 levels: 0 female, 1 male, . missing. I can track in the
program
the iteration, 1, 2 or 3. However, what I want to do is to pick up the
value of -sex- when each of the 3 iterations is being processed so
that I
can pull off the label and pass it to another programs. Is there a
way to
do this?
sysuse auto
capt label def rep 1 "Awful" 2 "Poor" 3 "OK" 4 "Good" 5 "Great"
label values rep78 rep
capt program drop fred
program define fred,rclass sortpreserve byable(recall)
version 9.2
marksample touse
su `_byindex' if `touse', meanonly
local blab: value label `_byvars'
su `_byindex' if `touse', meanonly
local blabval: label `blab' `r(min)'
di "category `blabval'"
end
Kit Baum, Boston College Economics
http://ideas.repec.org/e/pba1.html
*
* 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/
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
*
* 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/