0

I have a list of variables for which I want to create a list of numbered variables. The intent is to use these with the reshape command to create a stacked data set. How do I keep them in order? For instance, with this code

local ct = 1  
foreach x in q61 q77 q99 q121 q143 q165 q187 q209 q231 q253 q275 q297 q306 q315 q324 q333 q342 q351 q360 q369 q378 q387 q396 q405 q414 q423 {  
    gen runs`ct' = `x'  
    local ct = `ct' + 1  
}  

when I use the reshape command it generates an order as

runs1 runs10 runs11 ... runs2 runs22 ...   

rather than the desired

runs01 runs02 runs03 ... runs26

Preserving the order is necessary in this analysis. I'm trying to add a leading zero to all ct values less than 10 when assigning variable names.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Steve
  • 25
  • 1
  • 5
  • I edited your local macro displays (and other text: keep it terse and assume goodwill). Use indenting for syntax display whenever the use of backticks clashes with the desire to show literal backticks for Stata local macro references. (There is another solution, to escape the backticks, but indenting works more easily here.) – Nick Cox Nov 17 '16 at 21:19

1 Answers1

2

Generating a series of identifiers with leading zeros is a documented and solved problem: see e.g. here.

local j = 1 
foreach v in q61 q77 q99 q121 q143 q165 q187 q209 q231 q253 q275 q297 q306 q315 q324 q333 q342 q351 q360 q369 q378 q387 q396 q405 q414 q423 { 
   local J : di %02.0f `j' 
   rename `v' runs`J' 
   local ++j 
} 

Note that I used rename rather than generate. If you are going to reshape the variables afterwards, the labour of copying the contents is unnecessary. Indeed the default float type for numeric variables used by generate could in some circumstances result in loss of precision.

I note that there may also be a solution with rename groups.

All that said, it's hard to follow your complaint about what reshape does (or does not) do. If you have a series of variables like runs* the most obvious reshape is a reshape long and for example

clear 
set obs 1
gen id = _n

foreach v in q61 q77 q99 q121 q143 { 
    gen `v' = 42 
} 

reshape long q, i(id) j(which) 

list 

     +-----------------+
     | id   which    q |
     |-----------------|
  1. |  1      61   42 |
  2. |  1      77   42 |
  3. |  1      99   42 |
  4. |  1     121   42 |
  5. |  1     143   42 |
     +-----------------+

works fine for me; the column order information is preserved and no use of rename was needed at all. If I want to map the suffixes to 1 up, I can just use egen, group().

So, that's hard to discuss without a reproducible example. See https://stackoverflow.com/help/mcve for how to post good code examples.

Community
  • 1
  • 1
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • Nick, I didn't think about the number format. That works great. Thanks. I did search for a solution, but found none. On the complaint on reshape, I had none. It does what it's suppose to do. Many thanks! – Steve Nov 17 '16 at 21:41
  • 1
    Noted. It should be added that if you insist on suffixes with some (or all) leading zeros, you would need the `string` option of `reshape` to preserve them! – Nick Cox Nov 18 '16 at 07:53