Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: RE: create many variables at once

From   [email protected]
To   [email protected]
Subject   st: RE: RE: create many variables at once
Date   Tue, 23 Jul 2002 11:17:21 -0400

Elisabeth Simantov, PhD wrote
> Perhaps someone can help me with this. I am not a
> programmer, so please be explicit.
> New question.
> I have 19 variables that start with the prefix  me_  and 19
> variables that
> start with the prefix  ppg_ . They all have other letters after the
> underscore, actually the same for each group of 19.
> I have to create the following 19 new variables:
> newvars=(me_ * var1)+(ppg_ * var1) for each observation in the data
set (510)
> Is there a way for me to do this in one step. If not, can
> you please tell me how?

and Ronan Conroy replied
> 1. Verify that the variables are in the same order. If
> not, issue the -aorder- command to get them correctly
> ordered.
> 2. Use the -for- command to define two lists of variables.
> By default, the lists are called X and Y (in capitals).
> There are ways of imposing your own naming conventions,
> but you don't need that for a simple problem.
> . for var me_* \ var ppg_* : gen X_Y = (X * var1) + (Y * var1)

Nick Cox also suggested
> Tacit in this is that there is a pairing between
> the -me_*- and the -ppg_*-
> 1. Use -for-
> ============
> One way to do it is with -for-:
> for var me_* \ var ppg_* : gen stub_X_Y = var1 * (X + Y)
> Naturally you can devise your own naming convention.
> This presupposes that the two sets of variables
> are both in an appropriate order in memory.
> order me_* ppg_*

The above solutions reminded me that -for-'s functionality hasn't quite been
subsumed by the introduction of -foreach- and -forvalues- as I have been
prone to believe.  In version 6, I was quite content using the set of
looping commands (tokenize, while, etc.) and hadn't got to learning -for-
just yet.  Thus, when -foreach- and -forvalues- came about with Stata 7, I
saw no reason to bother with it.   But the solutions are excellent
illustrations that -for-'s sometimes results in more succinct and, arguably,
elegant solutions as it avoids unnecessary looping.

To right the wrong, I looked up for.hlp once more and noted small
inconsistency the examples provided.  The last example reads:

(begin excerpt)
        . for any male female: save c:\data\X if sex=="X"

will not work.  for will think there are three commands:

        save c:
        X if sex=="X"

Instead, you must type

        . for any male female: save "c:\data\X" if sex=="X"
(end of excerpt)

The last line won't work either since the syntax of save doesn't allow  the
[if exp] condition -- a non-feature that I, among others, wouldn't mind
seeing implemented in -save- BTW. ;)  (NB: I know it's been discussed on the
list before)

Patrick Joly
[email protected]
[email protected]
*   For searches and help try:

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