Bookmark and Share

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


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

Re: st: Ordering a varlist according to "parent" and "child" variables


From   A Loumiotis <[email protected]>
To   [email protected]
Subject   Re: st: Ordering a varlist according to "parent" and "child" variables
Date   Mon, 19 Dec 2011 20:02:25 +0200

Thanks a lot Matt for your help.
It works for the particular example that I gave but if I modify the
example a bit I think it will not work.  Consider the following:

var4 has three "child" variables var1 var2 var6
var5 has one "child" variable var3
var6 has two "child" variables var8 var11
var7 has two "child" variable var4 var10

The ordered varlist should be as follows:
v5 v3 v7 v4 v1 v2 v6 v8 v11 v10 v9

The method you suggested will produce the following:
v1 v2 v6 v8 v11 v5 v3 v7 v4 v10 v9

Here is the output
. forvalues i=1/11 {
  2.         gen v`i'=.
  3. }
. local childofv4 v1 v2 v6
. local childofv5 v3
. local childofv6 v8 v11
. local childofv7 v4 v10
. ds
v1   v2   v3   v4   v5   v6   v7   v8   v9   v10  v11
. foreach var in `r(varlist)' {
  2.         if "`childof`var''" != "" order `childof`var'', after(`var')
  3. }
. ds
v1   v2   v6   v8   v11  v5   v3   v7   v4   v10  v9



On Mon, Dec 19, 2011 at 6:28 PM, Matthew White
<[email protected]> wrote:
> Hi Antonis,
>
> I think your setup so far seems fine, though note that the names of
> -local-s can be at most 31 characters, so since you're using 7 for
> "childof", you'll have trouble with variables whose names are more
> than 24 characters. How about a loop like this:
>
> ds
> foreach var in `r(varlist)' {
>    if "`childof`var''" != "" order `child`var'', after(`var')
> }
>
> Best,
> Matt
>
> On Mon, Dec 19, 2011 at 11:15 AM, A Loumiotis
> <[email protected]> wrote:
>> Hi
>>
>> I'm working with survey data where the variables are related in the
>> following way.  There are "parent" and "child" variables where each
>> "child" variable can have at most one "parent" variable.  What I would
>> like to do is to find a general way to reorder the initial varlist in
>> a way that the "child" variables follow right after the "parent"
>> variables.
>>
>> Consider the following simplified example of a survey dataset with 11
>> variables var1-var11.
>>
>> var1 has three "child" variables var2-var4
>> var3 has two "child" variables var5-var6
>> var5 has two "child" variables var8-var9
>> var7 has one "child" variable var11
>>
>> What I would like to do is to find a (general) way to reorder the
>> initial varlist var1-var11 as follows:
>>
>> var1 var2 var3 var5 var8 var9 var6 var4 var7 var11 var10
>>
>> What I have done up to now is to create locals for each variable that
>> contains the child variables if any.  For the simplified example these
>> locals are defined as follows:
>>
>> local childofvar1 var2 var3 var4
>> local childofvar2
>> local childofvar3 var5 var6
>> local childofvar4
>> local childofvar5 var8 var9
>> local childofvar6
>> local childofvar7 var11
>> local childofvar8
>> local childofvar9
>> local childofvar10
>> local childofvar11
>>
>> I've tried to use loops to generate the ordered varlist but I'm not
>> successful.  Any help will be greatly appreciated!!!
>>
>> Antonis
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>
>
>
> --
> Matthew White
> Data Coordinator
> Innovations for Poverty Action
> 101 Whitney Avenue, New Haven, CT 06510 USA
> +1 434-305-9861
> www.poverty-action.org
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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