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

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

From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: method needed to alternate the order of variables
Date   Tue, 29 Jan 2008 21:28:22 -0000

A related problem is discussed within

Here is the related problem. 

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?  

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

  . 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. 
[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:

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