Announcement

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

  • Reshape long with multiple cases

    Let's say I have a bunch of variable names that start with either j1 or j2: j1move j2move j1clean j2clean j1out j2out, and so on. Is there an efficient way to write a loop that would rename all of those variables such that the "j#" gets moved from the beginning of the variable name to the end of the variable name?
    Last edited by Anne Todd; 23 May 2024, 13:09. Reason: EDIT: Sorry, I originally started working on a different question before realizing I needed to do the variable renaming before reshaping, but I failed to update the question title before submitting, h

  • #2
    Note that variable names cannot start with numbers.

    Code:
    rename j1* *j1
    See

    Code:
    help varlist
    Last edited by Andrew Musau; 23 May 2024, 13:17.

    Comment


    • #3
      I don't know what kind of reshape long you're warming up to.

      This works for your question about rename, and there is probably a shorter way to do it.


      Code:
      clear
      set obs 1
      
      foreach v in j1move j2move j1clean j2clean j1out j2out {
          gen `v' = 42
      }
      
      * you start here
      unab prefixes : *move  
      local prefixes : subinstr local prefixes "move" "", all
      
      foreach pre of local prefixes {
          rename (`pre'*)  (*`pre')
      }
      
      ds

      Last edited by Nick Cox; 23 May 2024, 13:37.

      Comment


      • #4
        Thanks Nick Cox . How could I also drop the "j" after moving it to the end, such that the end of each var name would simply just have a "1" or "2"?

        Comment


        • #5
          As usual, Nick is correct! You don't need to rename with stubs at the beginning, just reference them using @.

          Code:
          clear
          set obs 1
          foreach v in j1move j2move j1clean j2clean j1out j2out {
              gen `v' = 42
          }
          gen obsno=1
          list
          reshape long @move @clean @out, i(obsno) j(which) string
          list
          Res.:

          Code:
          . list
          
               +-------------------------------------------------------------+
               | j1move   j2move   j1clean   j2clean   j1out   j2out   obsno |
               |-------------------------------------------------------------|
            1. |     42       42        42        42      42      42       1 |
               +-------------------------------------------------------------+
          
          . 
          . reshape long @move @clean @out, i(obsno) j(which) string
          (j = j1 j2)
          
          Data                               Wide   ->   Long
          -----------------------------------------------------------------------------
          Number of observations                1   ->   2           
          Number of variables                   7   ->   5           
          j variable (2 values)                     ->   which
          xij variables:
                                    j1move j2move   ->   move
                                  j1clean j2clean   ->   clean
                                      j1out j2out   ->   out
          -----------------------------------------------------------------------------
          
          . 
          . list
          
               +------------------------------------+
               | obsno   which   move   clean   out |
               |------------------------------------|
            1. |     1      j1     42      42    42 |
            2. |     1      j2     42      42    42 |
               +------------------------------------+
          
          
          ​​​​​​​

          Comment


          • #6
            Isn't that a different question from #1? Either way, the loop should now be something like


            Code:
            foreach pre of local prefixes { 
                local pre2 = substr("`pre'", 2, .)
                rename (`pre'*)  (*`pre2')
            }

            Comment


            • #7
              Thanks Andrew Musau , now to extend to my actual data and eventual reshape, how could I do that with the below example? If I use the below code taken from yours, I get the error 'variable which contains all missing values'



              Code:
              reshape long @change @change_who, i(person_id date) j(which)
              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte person_id str9 date byte(j1change j2change) str6(j1change_who j2change_who)
              1 "2017Aug15" 0 . ""       ""      
              1 "2017Aug16" 0 . ""       ""      
              1 "2017Aug17" 0 . ""       ""      
              1 "2017Aug18" 1 . "Me"     ""      
              1 "2017Aug19" 1 . "Friend" ""      
              1 "2017Aug20" 0 . ""       ""      
              1 "2017Aug21" 0 . ""       ""      
              1 "2017Aug22" 0 . ""       ""      
              2 "2017Aug15" 0 0 ""       ""      
              2 "2017Aug16" 0 0 ""       ""      
              2 "2017Aug17" 0 0 ""       ""      
              2 "2017Aug18" 0 0 ""       ""      
              2 "2017Aug19" 0 1 ""       ""      
              2 "2017Aug20" 0 0 ""       ""      
              2 "2017Aug21" 0 0 ""       ""      
              2 "2017Aug22" 1 0 "Me"     ""      
              3 "2017Aug15" 0 . ""       ""      
              3 "2017Aug16" 1 . "Friend" ""      
              3 "2017Aug17" 0 . ""       ""      
              3 "2017Aug18" 0 . ""       ""      
              3 "2017Aug19" 1 . "Friend" ""      
              3 "2017Aug20" 0 . ""       ""      
              3 "2017Aug21" 0 . ""       ""      
              3 "2017Aug22" 1 . "Me"     ""      
              4 "2017Aug15" 1 0 "Me"     ""      
              4 "2017Aug16" 1 0 "Me"     ""      
              4 "2017Aug17" 1 0 "Me"     ""      
              4 "2017Aug18" 1 0 "Me"     ""      
              4 "2017Aug19" 0 0 ""       ""      
              4 "2017Aug20" 0 0 ""       ""      
              4 "2017Aug21" 0 1 ""       "Friend"
              4 "2017Aug22" 0 0 ""       ""      
              end

              Comment


              • #8
                #7 I think you need the string option. See help reshape.

                Comment

                Working...
                X