Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Create String Variable out of Variable Names

From   Kyle Hood <>
Subject   Re: st: Create String Variable out of Variable Names
Date   Wed, 25 Oct 2006 18:25:50 -0400 (EDT)

I'm a bit confused -- which observations do you want to have each variable assigned to, or do you just want a dataset with one observation for each variable name? To get a column that contain all of the variables, you could do the following:

gen varname = ""
local i = 1
foreach var of varlist * {
replace varname = "`var'" in `i'
local i = `i' + 1
keep varname
drop if varname == "varname"
drop if varname = ""

Why do you need the number of observations for your new variable to be equal to the number of observations in your dataset?


On Wed, 25 Oct 2006, Kyle C Longest wrote:

I have a set of variables that I would like to use to generate a singular String variable. The number of cases in each resulting value is inconsequential.

So for example if I had 5 variables (a b c d e) in a data set with 100 observations I would like a String variable that looked something like:

Var | Frequency
a 20
b 20
c 20
d 20
e 20

Now I know that in this simple case I could do this line by line with:

egen var = seq(), f(1) t(5)
replace var = "a" if var==1
replace var = "b" if var==2

But in the case of 64 variables this is unrealistically tedious, especially as I am hoping to incorporate this into a more encompassing program to be used with different sets of variables.

The problem I've run into with -foreach- is that it loops so that in the end the resulting variable looks like (using the example from above).

Var | Freq
e 100

[Also note that the variables are not mutually exclusive (ala dummies), so that a case could ==1 on several of the variables]

Any help with this would be greatly apprecaited,
Thank you,
Kyle C. Longest
Phd Student - Dept of Sociology
Univ. of North Carolina - Chapel Hill
* For searches and help try:

*   For searches and help try:

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