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

From |
Joseph Wagner <[email protected]> |

To |
[email protected] |

Subject |
Re: st: RE: method needed to alternate the order of variables |

Date |
Tue, 29 Jan 2008 16:53:15 -0500 |

Thanks Nick that worked. By they way, I did not mean to imply that SAS was inflexible, rather I think the folks making the request of me are being inflexible.

Nick Cox wrote:

A related problem is discussed within--

http://www.stata.com/support/faqs/data/reshape3.html

Here is the related problem.

-----------------------------------------------

Question

If I have many variables all occurring in pairs for two years 1997 and

1998, so that the dataset looks like A97, A98, B97, B98, and so on, is

there any easy way to reshape the data to long without typing all the

stub names?

Answer

The variable names are collectively *97 *98, so we need a way of

expanding that list of wildcard names automatically and then removing

the suffix. We can work on either *97 or *98. unab is usually billed as a programmer's command, but it can be used

interactively. It unabbreviates a varlist and puts the result in a local

macro.

. unab vars : *97

Then we zap all the occurrences of the suffix "97":

. local stubs : subinstr local vars "97" "", all

In other words, each occurrence of "97" is replaced by an empty string;

that is, they are removed. See macro.

Then we can

. reshape long `stubs', options

--------------------------------------

Thus if you

unab stubs : *_x local stubs : subinstr local stubs "_x" "", all

and do this

foreach s of local stubs { local vars `vars' `s'_x `s'_y }

you are some way closer to a solution.

I haven't used SAS, but I doubt that it is as inflexible as implied.

Nick

[email protected]

-----Original Message-----

From: [email protected]

[mailto:[email protected]] On Behalf Of Joseph Wagner

Sent: 29 January 2008 21:12

To: [email protected]

Subject: st: method needed to alternate the order of variables

I have a dataset with variables that end in an 'x' and a 'y' but follow a pattern such that:

idcode li_x lir_x li2_x li1_x id_x ptb_x pm_x pog_x . . .

. . . li_y lir_y li2_y li1_y id_y ptb_y pm_y pog_y

I have been asked to re-sort the variables so that they follow an "xy xy

xy" pattern:

idcode li_x li_y lir_x lir_y li2_x li2_y li1_x li1_y id_x id_y ptb_x ptb_y pm_x pm_y pog_x pog_y

-move- would be straightforward but I have 137 pairs of variables and I have a feeling i will need to do this again in the future. I suggested -aorder- since this would place the variables in alphabetic order like "var1x var1y var2x var2y" and so forth. Unfortunately, the person who needs this dataset re-ordered claims that the data is needed by someone (using SAS) who must have the data in a a particular order.

*

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

Joseph H. Wagner, M.P.H.

Lifespan Health Research Center

Wright State University Boonshoft School of Medicine

3171 Research Blvd.

Kettering, OH 45420-4014

(937) 775-1494 (LHRC office)

(937) 775-1456 (fax)

[email protected]

Visit the Lifespan Health Research Center Home Page at:

http://www.med.wright.edu/lhrc

*

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

**Follow-Ups**:**RE: st: RE: method needed to alternate the order of variables***From:*"Nick Cox" <[email protected]>

**References**:**st: method needed to alternate the order of variables***From:*Joseph Wagner <[email protected]>

**st: RE: method needed to alternate the order of variables***From:*"Nick Cox" <[email protected]>

- Prev by Date:
**Re: st: RE: store variable names in local macro** - Next by Date:
**st: coding question** - Previous by thread:
**st: RE: method needed to alternate the order of variables** - Next by thread:
**RE: st: RE: method needed to alternate the order of variables** - Index(es):

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