Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Dynamic variables names from file name


From   "Jana J." <j.jarecki@gmx.net>
To   statalist@hsphsun2.harvard.edu
Subject   st: Dynamic variables names from file name
Date   Tue, 11 Oct 2011 12:39:02 +0200

Dear list,

could anybody tell me how to generate variable names dynamically which
are a combination of (i) a part of a file name + (ii) some characters.

Simplified example:
I have 2 datasets [e.g. called "a2010.dta" and "b2010.dta"]. Each
contains variable names that are identical except for the first part,
which comes from some part of the name of the dataset. [e.g. for
"a2010.dta" - av1, av2, ... and for "b2010.dta" - bv1, bv2, ... ]
Now I want to generate a loop using each of the two datasets and
compute things with the variables. [What I want is, e.g., to generate
a variable newvar = av1 + av2 and newvar =bv1+bv2, for both datasets.]
How do I generate and use variable names within the loop, which are a
combination of a part of the file name and some appended string?

I tried

for each i in a2010 b2010 {
   use "`i'.dta"
   gen j =substr("`i'",1,1)    // cut off the first letter in the name
of the dataset i
   gen newvar =`j'v1 + `j'v1       // try to get it into he variable name
}


I also tried to generate strings which are identical to the variable name

   gen j = substr("`i'",1,1)    // cut off the first letter in the
name of the dataset i
   gen k =v1  // generate second part of variable
   egen l =concat(j k)   // paste together

But I dont know how to use the content of this string variable "l" as
the name of a variable within the dataset, and not as the string
variable "l"  itself.


Thanks a lot in advance,
best regards
Jana
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index