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

st: RE: Extract variable name in order to change it, iteratively


From   "David Harrison" <david.harrison@icnarc.org>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Extract variable name in order to change it, iteratively
Date   Tue, 2 Aug 2005 14:06:09 +0100

The -foreach- command is already doing exactly what you ask. On each iteration, it is putting each variable name in turn into the local macro `var'.

The final loop could go something like...

foreach var of varlist var1_88-var100_02 {
	local yy=substr("`var'",-2,2)
	if `yy'<50 {
		local cc 20
	}
	else {
		local cc 19
	}
	local newvar=substr("`var'",1,length("`var'")-2)+"`cc'`yy'"
	rename `var' `newvar'
}

David

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Jeppe Warberg
Larsen
Sent: 02 August 2005 13:15
To: statalist@hsphsun2.harvard.edu
Subject: st: Extract variable name in order to change it, iteratively


I have a wide data structure. Each observation consists of a unique
identifier (say, id) and some series of annual variables with a
two-digit suffix (running in different intervals). Like this:

id var1_88 var1_89 ... var1_02 var2_92 var2_93 ... var2_02
var3_...................................

Now I want to reshape it to long, but stata fails to read the
two-digit values after the turn of century (00 01 02), so I need to
rename all varibles to a four-digit year.

foreach var of varlist var1_88-var100_02{
	//save name of variable in a local string
	//do nessecary string manipulation (I can do that)
	//rename var 
}

My problem seems to be:
1) extracting the name of the current variable
2) rename

Do you understand what I'm asking?
I'd be glad if someone can help, and please don't tell me that it
would be easier to do this manually, because I will need to do this
operation many times...

Jeppe W. Larsen

*
*   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/



*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index