Statalist The Stata Listserver


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

st: Create String Variable out of Variable Names


From   Kyle C Longest <[email protected]>
To   [email protected]
Subject   st: Create String Variable out of Variable Names
Date   Wed, 25 Oct 2006 17:43:24 -0400

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/




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