Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ordering variables based on their names

    Hi everyone,
    Below is my variable list after running ds command. I would like to order variables in such a way the trim variables locate after their corresponding non-trim variables. For example, I would like to order HLE_50_511, trim_HLE~511, ULE_50_511, trim_ULE~511, and so on.
    Thanks.
    Nader

    HTML Code:
     ds
    HLE_50_511 HLE_60_611 HLE_70_711 trim_HLE~511 trim_ULE~611 trim_TLE~711
    ULE_50_511 ULE_60_611 ULE_70_711 trim_HLE~512 trim_ULE~612 trim_TLE~712
    TLE_50_511 TLE_60_611 TLE_70_711 trim_HLE~513 trim_ULE~613 trim_TLE~713
    PLE_50_511 PLE_60_611 PLE_70_711 trim_HLE~611 trim_ULE~711 trim_PLE~511
    HLE_50_512 HLE_60_612 HLE_70_712 trim_HLE~612 trim_ULE~712 trim_PLE~512
    ULE_50_512 ULE_60_612 ULE_70_712 trim_HLE~613 trim_ULE~713 trim_PLE~513
    TLE_50_512 TLE_60_612 TLE_70_712 trim_HLE~711 trim_TLE~511 trim_PLE~611
    PLE_50_512 PLE_60_612 PLE_70_712 trim_HLE~712 trim_TLE~512 trim_PLE~612
    HLE_50_513 HLE_60_613 HLE_70_713 trim_HLE~713 trim_TLE~513 trim_PLE~613
    ULE_50_513 ULE_60_613 ULE_70_713 trim_ULE~511 trim_TLE~611 trim_PLE~711
    TLE_50_513 TLE_60_613 TLE_70_713 trim_ULE~512 trim_TLE~612 trim_PLE~712
    PLE_50_513 PLE_60_613 PLE_70_713 trim_ULE~513 trim_TLE~613 trim_PLE~713

    .






    Last edited by Nader Mehri; 10 Mar 2020, 17:23.

  • #2
    Something like this:

    Code:
    ds trim_*, not
    foreach v of varlist `r(varlist)' {
        capture confirm var trim_`v', exact
        if c(rc) == 0 {
            order trim_`v', after(`v')
       }
      else if c(rc) != 111 {
           display as error "Unexpected problem: `v'"
           exit c(rc)
      }
    }

    Comment

    Working...
    X