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

st: RE: egen fill() for string variables?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: egen fill() for string variables?
Date   Thu, 2 Sep 2004 09:50:57 +0100

In addition to other solutions, one way 
to do this is with -egen, repeat()- from 
-egenmore- on SSC. 

. egen foo = repeat(), values(cat dog rat fish) 

Note that there is no need to play around with
-expand-. The values stated are repeated in 
sequence, and -repeat()- automatically works 
out an appropriate variable type. 

It is closer in spirit to -egen, seq()- than to 
-egen, fill()-. As the syntax for -egen, fill()- 
states clearly that it feeds on a numlist, I 
wouldn't expect it to work for string values. 

Nick
n.j.cox@durham.ac.uk 

Wallace, John

I've found the fill() function for egen to be a very valuable tool, but I recently found it doesn't work for repeating patterns of string values.  For example, if I wanted to generate a variable containing "cat, dog, rat, fish" for a set of 100 observations, I would first -expand- the observations to 400 by using

.expand 4

and then using my idealized egen situation I'd type

.egen str4 animal = fill(cat, dog, rat, fish, cat, dog, rat, fish) /*possibly with double quotes enclosing the strings - e.g. "fish" */

does such a command exist somewhere?

If not, would a workaround such as the following work? (I'm not at a Stata enabled machine this morning)

.expand 4
.egen animal = fill(1,2,3,4,1,2,3,4)
.label define critter 1 "cat" 2 "dog" 3 "rat" 4 "fish"
.label value animal critter
.decode animal, gen(str_animal) 


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index