Announcement

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

  • Inverting variable row' sequence

    Hello Everyone,

    I would be grateful if you could advise me whether there is a way to invert the sequence of the variable rows from "a", "b", "c" to "c", "b", "a". Thank you very much in advance!

  • #2
    I don't understand your question and your data structure. Please give a data example using -dataex-.

    If this is a single string variable, say called myvar, you can sort it in descending order using
    Code:
    gsort -myvar
    But I have no idea if this is actually what you want.
    Last edited by Hemanshu Kumar; 01 Sep 2022, 10:10.

    Comment


    • #3
      Originally posted by Hemanshu Kumar View Post
      I don't understand your question and your data structure. Please give a data example using -dataex-.

      If this is a single string variable, say called myvar, you can sort it in descending order using
      Code:
      gsort -myvar
      But I have no idea if this is actually what you want.
      I have a dataset that has variables in the following sequance from left to right - age, income, occupation, ID. I want to invert the sequance of variables from right to left, meaning I want them to have the following sequance - ID, occupation, income, age. I would use dataex, but I don't think it will be helful in this case. Thank you very much in advance!

      Comment


      • #4
        ah, you just need to do
        Code:
        order ID occupation income age
        for more, look at
        Code:
        help order
        In Stata, when you want to change the sequence in which rows (observations) appear, that is termed sorting. When you change the sequence in which the columns (variables) appear, that's ordering. You need the latter.
        Last edited by Hemanshu Kumar; 05 Sep 2022, 06:22.

        Comment


        • #5
          Originally posted by Hemanshu Kumar View Post
          ah, you just need to do
          Code:
          order ID occupation income age
          for more, look at
          Code:
          help order
          In Stata, when you want to change the sequence in which rows (observations) appear, that is termed sorting. When you change the sequence in which the columns (variables) appear, that's ordering. You need the latter.
          Thank you very much for the code and for the clarification! I have more than 2000 variables, so to order the variables with your specified code will be extremely tedious. I was wondering if there is a way I can escpate that situation.

          Thank you again in advance!

          Comment


          • #6
            Looping backwards is possible. For example,

            Code:
            local greek alpha beta gamma delta epsilon
            forval i= `=wordcount("`greek'")'(-1)1{
                display word("`greek'", `i')
            }
            Res.:

            Code:
            . forval i= `=wordcount("`greek'")'(-1)1{
              2.
            .     display word("`greek'", `i')
              3.
            . }
            epsilon
            delta
            gamma
            beta
            alpha
            This therefore suggests a solution of the form:

            Code:
            sysuse auto, clear
            list in 1
            qui ds
            local list
            forval i= `=wordcount("`r(varlist)'")'(-1)1{
                local list "`list' `=word("`r(varlist)'", `i')'"
            }
            order `list'
            list in 1
            Res.:

            Code:
            . list in 1
            
                 +----------------------------------------------------------------------------------------------------------------+
                 | make          price   mpg   rep78   headroom   trunk   weight   length   turn   displa~t   gear_r~o    foreign |
                 |----------------------------------------------------------------------------------------------------------------|
              1. | AMC Concord   4,099    22       3        2.5      11    2,930      186     40        121       3.58   Domestic |
                 +----------------------------------------------------------------------------------------------------------------+
            
            .
            . qui ds
            
            .
            . local list
            
            .
            . forval i= `=wordcount("`r(varlist)'")'(-1)1{
              2.
            .     local list "`list' `=word("`r(varlist)'", `i')'"
              3.
            . }
            
            .
            . order `list'
            
            .
            . list in 1
            
                 +----------------------------------------------------------------------------------------------------------------+
                 |  foreign   gear_r~o   displa~t   turn   length   weight   trunk   headroom   rep78   mpg   price   make        |
                 |----------------------------------------------------------------------------------------------------------------|
              1. | Domestic       3.58        121     40      186    2,930      11        2.5       3    22   4,099   AMC Concord |
                 +----------------------------------------------------------------------------------------------------------------+
            
            .

            Comment


            • #7
              Andrew's approach is good, but I'd also like to point to an approach to reversing a list that involves "stacking" rather than "queueing" items in the list, a small technique that can sometimes be useful in Stata:

              Code:
              sysuse auto, clear
              list in 1
              qui ds
              local list
              foreach v in `r(varlist)' {
                  local list "`v' `list'"  // last in, first out, so to speak
              }
              order `list'
              list in 1

              Comment


              • #8
                Mike Lacy and Andrew Musau , thank you so much for the solution. Digging more into the question, I dare to ask one more - is it possible to place a specific variable in the desired position? Let's say, the variable "id number" is the 105th variable in my dataset, and I want it to be the first. Thank you very much again, I really appreciate your support!
                Last edited by Nick Baradar; 07 Sep 2022, 07:58.

                Comment


                • #9
                  Code:
                  order id_number
                  will put the variable id_number in first position.

                  Comment


                  • #10
                    Originally posted by Hemanshu Kumar View Post
                    Code:
                    order id_number
                    will put the variable id_number in first position.
                    Thank you so much, Hemanshu!

                    Thanks everyone for your support!

                    Comment

                    Working...
                    X