Statalist


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

st: Selecting stubnames for -reshape- / macrolists


From   "pr9@duke.edu" <pr9@duke.edu>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   st: Selecting stubnames for -reshape- / macrolists
Date   Mon, 30 Jun 2008 07:31:02 -0700

.
My question is whether there is a clever way to identify the stubnames
for a -reshape-. Consider this example:

sysuse auto, clear
foreach j in 1 2 3 4 5 {
    gen x_`j'=rep78+`j'
    gen y_`j'=rep78-`j'
}
drop y_3
gen id=_n
keep id x_* y_*

/*
This data-set contains the following variables which I want to -reshape-:
x_1  x_2  x_3  x_4  x_5
y_1  y_2       y_4  y_5
I want to -reshape- by stubnames x_ and y_.
In my "real" data-set, I know that all variables I want to end up with
as stubnames contain an underscore (_). They all also do contain numbers
after the underscore, but there is no regular pattern there.

Now, I am generating a -macrolist- with unique stubnames, but this seems
like a detour to me, especially the loop:
*/

ds *_*
foreach v in `r(varlist)' {
        local f `=regexr("`v'","[0-9]+","")'
        local n `n' `f'
        local n: list uniq n
}
di "*** `n' ****"
reshape long `n', i(id) j(foo)

Two questions:
1) Is there a better way to identify stubnames for a -reshape-?
2) Is there a more straightforward way to arrive at a unique macrolist
than the one I chose? In particular, something like the -regexr()-
function, but with the ability to remove all instances (not just the
first) of the regular expression. Something like -subinstr()-, but for
regular expressions.

Thank you!
Philipp


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

Privileged, confidential or patient identifiable information may be contained in this message. This information is meant only for the use of the intended recipients. If you are not the intended recipient, or if the message has been addressed to you in error, do not read, disclose, reproduce, distribute, disseminate or otherwise use this transmission. Instead, please notify the sender by reply e-mail, and then destroy all copies of the message and any attachments.


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