Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: obtaining locals


From   Lars Korsholm <[email protected]>
To   David E Moore <[email protected]>
Subject   st: RE: obtaining locals
Date   Thu, 25 Sep 2003 01:13:08 +0200 (MEST)

thanks to david.


On Wed, 24 Sep 2003, David E Moore wrote:

> I believe that the latest version of Stata has a way to accomplish what you want
> that is different from my approach, but I'm using version 6.  At any rate, you
> can get the value labels for any variable by: (1) finding the label definition,
> and (2) extracting the appropriate label.  Thus, it is a two step process.  The
> trick is to use the extended functions for local macros.  Here's an example:
> 
> local vlname: value label `varname'
> local yyy: label `vlname' `x'
> 
> The first statement obtains the name of the value labels assigned to variable
> `varname' and assigns it to the local macro, "vlname."  The second statement
> grabs the actual label associated with the value `x' defined for the labels
> assigned to variable `varname'.  This assumes that `x' is a valid value for the
> labels and that value labels have been assigned to `varname'.  If either of
> these conditions is false, then the extended functions return empty strings.
> Also, I'm not sure how you are getting the coded values of your variables.  When
> I was writing generic code to process value labels I used a crude but effect
> indirect method.  The idea was to obtain the coded values for every category
> from the -tabulate- command.  Here's a code fragment that cycles through every
> coded value of a categorical variable and displays its label:
> 
> tempname rval                                   /* matrix to store coded label
> value*/
> quietly tab `varname', matrow(`rval')           /* get the coded values
> */
> local rvals = rowsof(`rval')                    /* how many values?
> */
> local vlname: val label `varname'               /* get the name of the value
> labels */
> if "`vlname'" != "" {                           /* if varname has value labels
> */
>    local i 1
>    while `i' <= `rvals' {                       /* cycle through all the values
> */
>       local ival = `rval'[`i',1]                /* get a category code
> */
>       local vlab: label `vlname' `ival'         /* get the associated label
> */
>       display "label `i'(`ival'): `vlab'"       /* show the code's label
> */
>       local i = `i' + 1                         /* go get the next one
> */
>    }
> }
> 
> Hope this helps.
> 
> Dave
> 
> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]]On Behalf Of Lars Korsholm
> > Sent: Wednesday, September 24, 2003 3:11 PM
> > To: [email protected]
> > Subject: st: obtaining locals
> >
> >
> > Dear listers
> >
> > I am sure that someone know a smart way to do what I want. :-)
> >
> > the sort version:
> > Is it possible to obtain the label associated to a given value?
> >
> > The long verson:
> > I am writing a small tabulation program. The user (also me) specify some
> > of the levels that a kategorical (integer) veriabel has. and for each of
> > these chosen levels the program  produce one line of output.
> >
> > I would like the line to start with the label associated with the level
> > and not the integer in my loop.
> >
> > i.e. something like
> >
> > program define mytab
> >  syntax varname, ni(numlist ..) ..
> >  foreach x  in numlist `ni' {
> >     count if `varname'=�=`x'  & ..
> >     di "level yyy:" `r(N)' ...
> >     }
> > end
> >
> > where yyy is the contens of the label associated with varname
> > that corespond to the value `x'
> > If no label is assigned, then `x' must do.
> >
> >
> > Hope someone can help me on this one.
> >
> > Best Lars
> >
> >
> > ............................................
> > : Lars Korsholm Ph.D.,                     :
> > :  Department of Statistics                :
> > :  University of Southern Denmark          :
> > :  Sdr. Boulevard 23A, 2.                  :
> > :  DK-5000 Odense C                        :
> > : Phone   +45 6550 3608                    :
> > : Mobil   +45 3033 3617                    :
> > : Fax     +45 6595 7766                    :
> > : Mail    [email protected]              :
> > : Web     http://www.isd.sdu.dk/~korsholm  :
> > :..........................................:
> >
> >
> >
> > *
> > *   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/
> >
> >
> 

............................................
: Lars Korsholm Ph.D.,                     :
:  Department of Statistics                :
:  University of Southern Denmark          :
:  Sdr. Boulevard 23A, 2.                  :
:  DK-5000 Odense C                        :
: Phone   +45 6550 3608                    :
: Mobil   +45 3033 3617                    :
: Fax     +45 6595 7766                    :
: Mail    [email protected]              :
: Web     http://www.isd.sdu.dk/~korsholm  :
:..........................................:



*
*   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