Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Use of local macros when generating new variables (-rowranks-)

From   Nick Cox <>
To   "" <>
Subject   Re: st: Use of local macros when generating new variables (-rowranks-)
Date   Thu, 23 May 2013 19:40:52 +0100

-rowranks- is from SJ 9-1.

It sounds as if you want something like

forval j = 1/3 {
      rowranks Cabc_`j' Cdef_`j' C456_`j' C789_`j', gen(R1_`j'-R4_`j')

The trick here is: how many repetitions are there going to be, and
what varies between repetitions?

The answer is 3, and the last character of each variable name varies,
so that's your loop.


On 23 May 2013 19:27, Michael Crain <> wrote:
> Using Nick Cox's -rowranks- ado that ranks values across rows, the
> general syntax is:
> rowranks x1-x5, generate(r1-r5)
> My very large dataset consists of subsets of rows (variables) that I
> want to rank. I need some help on the syntax (or approach).
> My variables have this general form: C[code string]_[time period]
> I want to rank across the C[code string] variables by _year_. For
> instance, my variables look like this:
> Cabc_1
> Cdef_1
> C456_1
> C789_1
> Cabc_2
> Cdef_2
> C456_2
> C789_2
> Cabc_3
> Cdef_3
> C456_3
> C789_3
> I want to rank across Cabc Cdef C456 C789 for each time period so:
> a. rank across Cabc_1 Cdef_1 C456_1 C789_1, then
> b. rank across Cabc_2 Cdef_2 C456_2 C789_2, then
> c. rank across Cabc_3 Cdef_3 C456_3 C789_3
> I tried using a loop but my problem is how to write the -rowranks-
> syntax for the subset variable range for the source variables and new
> rank variables.
> My loop is this:
> levelsof timedummy, local(timeperiod)
> levelsof codestring, local(code)
> foreach t of local timeperiod {
>    foreach c of local code {
>       rowranks C`c'_`t', gen(R`c'_`t')
>       }
>    }
> With this code, Stata sees my source variables (over 200 of them in year time period), but sees only one new variable name (an error). I tried variations of this syntax for the variables with no success,
> Can anyone help with the syntax? Or is there is a better approach?
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index