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

st: condition global assignment on value of local variable?

From   Trudy Ann Cameron <>
Subject   st: condition global assignment on value of local variable?
Date   Fri, 28 May 2004 09:38:05 -0700

Dear Statalisters:

Can anyone enlighten me as to how to condition a global assignment on the
current value of a local variable?

* An abbreviated version of what isn't working.  ;

foreach c of numlist 1/2 {;
global LINK_`c'      "recordcase+z+lstatute_`c'+z+lsystem_`c'+z+lsourceid_`c'";
global PROFILE_`c'   "recordcase+z+pstatute_`c'+z+psystem_`c'+z+psourceid_`c'";
global INSPECT_`c'   "recordcase+z+istatute_`c'+z+isystem_`c'+z+isourceid_`c'";

* An abbreviated version of an alternative that works just fine.  ;

global LINK_1     "recordcase+z+lstatute_1+z+lsystem_1+z+lsourceid_1";
global PROFILE_1  "recordcase+z+pstatute_1+z+psystem_1+z+psourceid_1";
global INSPECT_1  "recordcase+z+istatute_1+z+isystem_1+z+isourceid_1";

global LINK_2     "recordcase+z+lstatute_2+z+lsystem_2+z+lsourceid_2";
global PROFILE_2  "recordcase+z+pstatute_2+z+psystem_2+z+psourceid_2";
global INSPECT_2  "recordcase+z+istatute_2+z+isystem_2+z+isourceid_2";

Further details:

I'd like to define four sets of concatenated strings, one set for each
value of an index, `c'.  These are all character strings that I need to
concatenate with tab characters in between.  (The z variable is defined
previously as the tab character.)  The foreach task I display above tries
to interpret each variable, but I don't want that to happen.  All I want to
do is to define the string, explicitly as shown, except for the
substitution of a different numeric value for `c' in each round.

The alternative explicitly indexed code (displayed above, after the foreach
example) works just fine and does what I want it to do.  However, I was
hoping for a simpler way to scale up the task from 1/2 to 1/100, say,
without having to put all 100 sets of global statements in my program.  In
fact, if I can figure out how to embed a local variable into a global
statement, I can even avoid having to create all 100+ sets of global
statements, because then I could only create the ones I end up needing, on
the fly.  Without that capability, I'm stuck with using trial and error to
guess the maximum number of global statements I'll need to accommodate all
of the cases in my data, and creating them all in advance.

(Why do I need to do this?:  I need to convert a data set with systematic
but horrible organization into something that is usable.  Different parts
of each multiple-line record have to be mixed and matched with each other
to redefine what constitutes an "observation."  There are many observations
of different types associated with each original data record.  It is pure
character manipulation at this point.  A couple of days of experimentation
got Stata doing exactly what I want with a small demonstration sample of
cases, but now I'm having trouble scaling it up to a huge sample without
solving this problem.)

Maybe there is an obvious alternative to using global statements for this task.

My humble apologies if this is something obvious, but I'm stumped. Maybe
I'm missing something on pp. 220+ of the "Programming" manual, or in U[21],
although perhaps I just haven't recognized what I need.


Trudy Ann Cameron
R.F. Mikesell Professor of Environmental
           and Resource Economics
Department of Economics, 430 PLC
1285 University of Oregon
Eugene, OR  97403-1285

phone:  (541) 346-1242
FAX:     (541) 346-1243

*   For searches and help try:

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