Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

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

Date |
Fri, 24 May 2013 00:15:27 +0100 |

I am not sure how much more help you can be given without knowing all your names. If I understand you correctly, a generalisation is forval j = 1/3 { local a : word `j' of `A' local b : word `j' of `B' local c : word `j' of `C' local d : word `j' of `D' rowranks C`a'_`j' C`b'_`j' C`c'_`j' C`d'_`j', gen(`a'``b'`c'`d'R1_`j'-`a'`b'`c'`d'R4_`j') } and you need to define the lists in `A' `B' `C' `D' upstream of this. But watch out for over-length variable names. See also for some general comments on looping in parallel SJ-3-2 pr0009 . . . . . . . . . . . . . Speaking Stata: Problems with lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox Q2/03 SJ 3(2):185--202 (no commands) discusses ways of working through lists held in macros .pdf at http://www.stata-journal.com/sjpdf.html?articlenum=pr0009 Nick njcoxstata@gmail.com On 23 May 2013 20:47, Michael Crain <michaelcrain@ymail.com> wrote: > Many thanks for your message. This does not exactly take care of my problem. > > My dataset has over 200 variables for each year rather than four in my simple example. And the set of variables changes somewhat from year to year as several variables come and go. That is why I wrote the loop (below) with the local macro for the codestring variable. > > I get the idea when relatively few variables are being ranked. But I am trying to figure out an efficient way to write the syntax with over 200 variables for each time period. > > Any suggestions? Thank you. > > >>>> reply from Nick Cox > > -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. > > Nick > njcoxstata@gmail.com > > > On 23 May 2013 19:27, Michael Crain <michaelcrain@ymail.com> 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? > > --- On Thu, 5/23/13, Michael Crain <michaelcrain@ymail.com> wrote: > >> From: Michael Crain <michaelcrain@ymail.com> >> Subject: Use of local macros when generating new variables (-rowranks-) >> To: statalist@hsphsun2.harvard.edu >> Date: Thursday, May 23, 2013, 2:27 PM >> 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: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**References**:**Re: st: Use of local macros when generating new variables (-rowranks-)***From:*Michael Crain <michaelcrain@ymail.com>

- Prev by Date:
**st: Why does nl call all parameters as "_cons"?** - Next by Date:
**st: Rabe-Hesketh's gllamm: multivariate multilevel dropout model** - Previous by thread:
**Re: st: Use of local macros when generating new variables (-rowranks-)** - Next by thread:
**st: stata estimates to latex table in vertical** - Index(es):