Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: data transformation

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: data transformation Date Mon, 4 Mar 2013 10:14:35 +0000

```gen Z_trans = .

qui foreach v of var X Y {
replace Z_trans = `v' if Z == "`v'"
}

Nick

P.S. for a bigger problem, use some varlist notation.

On Mon, Mar 4, 2013 at 1:41 AM, Seok-Woo Kwon <kwonsw@gmail.com> wrote:

> I do not know how to describe my problem in general terms. So let me
> use an example to describe it. (I am using Stata 12 for Windows.)
>
> I have about 10,000 observations and 1,000 variables on individuals,
> but 4 observations and 3 variables will be enough to show the problem.
> My data looks like this:
>
>            id       X      Y    Z
>           -----------------------
>           001      1      2    X
>           002      2      3    Y
>           003      3      5    2
>           004      4      6    Y
>           -----------------------
>
> I need data that looks like this:
>
>            id       X      Y    Z     Z_transformed
>           --------------------------------------------
>           001       1    2    X                   1
>           002       2    3    Y                   3
>           003       3    5    2                   2
>           004       4    6    Y                   6
>           --------------------------------------------
>
> That is, some values in the variable "Z" will be a variable name (like
> "X" or "Y"). I would like to transform that variable into a value of
> that variable for the observation.
> For example, the value of Z for id 001 is X. Instead of X, I would
> like to show the value of X for id oo1 (which is 1). Is there a way to
> program this in Stata?
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```