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 <[email protected]>
To   [email protected]
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?

Kyle


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
[email protected]
*
* 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/

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