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" <>
To   <>
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. 


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:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index