Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Re: reshape when the stub is at the beginning of the variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Re: reshape when the stub is at the beginning of the variables
Date   Wed, 26 Apr 2006 22:17:05 +0100

OK. Let's see how that would work. Let's imagine 

Example 1
=========

Afoo1 - Afoo9 
Bfoo1 - Bfoo9 
... 
Zfoo1 - Zfoo9 

is an example of a set of variables with both 
prefixes and suffixes. As you postulate, each prefix is
no longer unique. You could cycle over
the prefixes -- but note the crucial assumption that 
_no_ other variable starts with each prefix -- 

foreach l in `c(Alpha)' { 
	renpfix `l' 
	
	foreach v of var foo* { 
		rename `v' `v'`l' 
	}
} 

and you end up with 

foo1A-foo9A 
foo1B-foo9B
...
foo1Z-foo9Z 

If you what you wanted was 

fooA1-fooA9 
fooB1-fooB9 
..
fooZ1-fooZ9 

you would need to do more work. 

Now how about a direct attack? 

foreach v of var *foo* { 
	local pre = substr("`v'",1,index("`v'", "foo") - 1) 
	local post = substr("`v'",index("`v'", "foo") + 1,.) 
	rename `v' foo`pre'`post' 
} 

or whatever. 

Example 2
=========

Or, let's go for a different set-up. 

Afoo, Abar, Abazz, ..., Afrog, Anewt, Atoad 
Bfoo, ..., Btoad 
... 
Zfoo, ..., Ztoad 

foreach l in `c(Alpha)' { 
	renpfix `l' 
	
	foreach v of var foo-toad { 
		rename `v' `v'`l' 
	}
} 

and you end up with 

fooA-toadA 
...
fooZ-toadZ 

A direct attack in this case could be 

foreach v of var Afoo-Ztoad { 
	local pre = substr("`v'",1,1) 
	local stub = substr("`v'",2,.) 
	rename `v' `stub'`pre' 
} 

or whatever. In each case, I made equally 
demanding assumptions about variable order 
that might not be true. 

Comparisons
===========

So, two warnings about using -renpfix-: 

1. Using -renpfix- means you have to be careful
that the prefix is shared only by the variables
you want to rename. 

2. If you strip a prefix, you may need to rename
the bare variables promptly before doing something 
else. 

As for what I called a direct attack: 

1. In each second code fragment, only one loop is needed. 
Also, I think it's simpler overall. 

2. I can't think that comparable warnings are needed. 

You may be thinking of something a bit different, and 
I constructed my examplesto favour my prejudices. 

That said, I'm just not convinced that -renpfix- is 
especially helpful for this kind of problem. 

Nick 
[email protected] 

Paul Millar
 
> A good solution Nick.  I maintain that renpfix may still be a good 
> solution for such situations where the prefix A*, B*, C* is for each 
> wave or cycle of a survey.  If there are a hundred or more variables 
> and only a few cycles, the -renpfix- is a better approach.  It 
> depends on what there is more of.
 
Nick Cox 

> >Various suggestions were made in this thread. In
> >general, use of -reshape-'s advanced syntax seems
> >most direct to me. But renaming solutions are
> >of interest for many other problems and I will
> >focus on those.
> >
> >Using -renpfix- is not a good solution.
> >Here the prefix is variable, not constant.
> >That is, within
> >
> >ASMOK BSMOK CSMOK
> >
> >the prefix is variously A B C.
> >
> >So -renpfix- offers no advantage over
> >one by one -rename-s in this instance.
> >
> >Another solution is
> >
> >foreach v of var *SMOK {
> >         local pre = substr("`v'",1,index("`v'", "SMOK")-1)
> >         rename `v' SMOK`pre'
> >}
> >
> >For who prefer the user-written -renvars- from the SJ,
> >a solution is possible in the same spirit
> >
> >renvars *SMOK, map("SMOK" + substr("@",1,index("@","SMOK")-1))
> >
> >Alternatively,
> >
> >foreach v of var *SMOK {
> >         local pre : subinstr local v "SMOK" ""
> >         rename `v' SMOK`pre'
> >}
> >
> >with again a -renvars- analog, something like this:
> >
> >renvars *SMOK, map("SMOK" + `"`: subinstr local v "SMOK" ""'"')
> >
> >Tricky, but it can be done! I prefer
> >
> >foreach v of var *SMOK {
> >         local pre : subinstr local v "SMOK" ""
> >         rename `v' SMOK`pre'
> >}
> >
> >to using -renvars- in this case.
> >
> >Nick
> >[email protected]
> >
> >Paul Millar
> >
> > > Another approach might be to use the renpfix command, which
> > > renames (or removes the prefix) see -whelp renpfix-
> > >
> > > Once the prefix is removed it is easy to reshape.

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