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

st: string vars


From   TEWODAJ MOGUES <[email protected]>
To   [email protected], Stata _ <[email protected]>
Subject   st: string vars
Date   Wed, 10 Nov 2004 20:48:30 -0500

Nick, Scott, Ronan,
These are good tips. The " " was really straightforward, strange that 
that didn't occur to me. The encode and inlist syntax will be very 
useful for what I'm doing.
Tewodaj
_______________________________________________________________________
_

Message: 10        
   Date: Wed, 10 Nov 2004 16:45:28 +0000
   From: Ron�n Conroy <[email protected]>
Subject: Re: st: RE: Dealing with string variables

Nick Cox wrote:

>drop if grain == "wheat" 
>
>value labels for string variables: 
>not allowed 
>
>what you want is better done by 
>
>gen Crop = "crop" if inlist(crop, "wheat", "barley") 
>replace Crop = "root" if inlist
(crop, "potato", "cassava", "yams", "beet") 
>... 
>
>  
>
using -encode-

. label define crop 1 "wheat" 2 "barley" 3 "root", modify
. encode crop, gen(Crop) label(crop)
. replace Crop=3 if inlist(crop, "potato", "cassava", "yams", "beet")
. replace Crop=. if Crop > 3

Logic:
1. start by defining a label that will assign numeric codes to the 
strings in a logical order. In a do-file, always follow this by -
modify- 
in case the label is already defined in some way.
2. use -encode- to generate a new variable, using the predefined 
mappings. Strings that are not found in the predefined value label 
will 
be added to the value label, using unassigned numbers. For this 
reason,  
nothing will be assigned the number 3 because we reserved this for a 
new 
category, "root".
3. Nick's nice piece of code that checks for root crops. You could use 
the -index- function for a single string, but -inlist- is more 
general. 
-index- has some neat uses; this isn't one of them.
4. Any remaining values are ones that we don't need (strings like "not 
applicable" or other crops that don't interest us). These will have 
been 
assigned value labels and numeric codes that can be inspected easily 
by 
typing

. lab list crop
 


~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
Tewodaj Mogues
Dept. of Agricultural and Applied Economics
University of Wisconsin - Madison
427 Lorch St. #317, Taylor Hall
Madison, WI 53706

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