Announcement

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

  • How to move the prefix as the suffix?

    I would like to move the prefixes to the suffixes. For example, if a variable is called tx_abc, I would like to change it as "abc_tx". The "x" could be 1, 2, 3, 4

    I wrote the following codes:
    Code:
    local VarList t1_L_nameMem t2_L_nameMem t3_L_nameMem t4_L_nameMem  ///
                  t1_name t2_name t3_name t4_name
                            
    foreach x of local VarList {
            local suffix = "_" + substr("`x'", 1, 2)
            local varName = substr("`x'", 4, .)
            local newName = `varName' + `suffix'
            rename `x' `newName'
    }
    But got this error message, can anyone help me?

    Code:
    . local VarList t1_L_nameMem t2_L_nameMem t3_L_nameMem t4_L_nameMem  ///
    >                         t1_name t2_name t3_name t4_name
    
    .                         
    . foreach x of local VarList {
      2.                 local suffix = "_" + substr("`x'", 1, 2)
      3.                 local varName = substr("`x'", 4, .)
      4.                 local newName = `varName' + `suffix'
      5.                 rename `x' `newName'
      6. }
    L_nameMem not found
    r(111);

  • #2
    ah, I found the answer, no need to add the "+" sign in the third local macro.

    Code:
    local VarList t1_L_nameMem t2_L_nameMem t3_L_nameMem t4_L_nameMem  ///
                  t1_name t2_name t3_name t4_name
                            
    foreach x of local identifiersList {
            local suffix = "_" + substr("`x'", 1, 2)
            local varName = substr("`x'", 4, .)
            rename `x' `varName'`suffix'
    }
    Last edited by Victor Smith; 22 Jul 2018, 07:31.

    Comment


    • #3
      Another approach is provided by the rename group command (see help rename group for details).
      Code:
      . describe, simple
      t1_L_nameMem  t3_L_nameMem  t1_name       t3_name
      t2_L_nameMem  t4_L_nameMem  t2_name       t4_name
      
      . 
      . rename (??_*) (*[3]_??)
      
      . 
      . describe, simple
      L_nameMem_t1  L_nameMem_t3  name_t1       name_t3
      L_nameMem_t2  L_nameMem_t4  name_t2       name_t4
      
      .

      Comment


      • #4
        This seems overly complicated. Try

        Code:
        rename (t(#)_*) (*[2]_t#[1])
        Arguably, the syntax for rename may seem complicated, too ...

        Best
        Daniel

        Comment


        • #5
          Thanks Williams and Daniel. But I can't use rename because I only wanted to change some of the variables in the dataset, not all of them. If i use rename, all of the variable names will be changed.

          Comment


          • #6
            Originally posted by Victor Smith View Post
            If i use rename, all of the variable names will be changed.
            That is not the case. It's either an overstatement, or you have not understood the code suggested by Daniel and me.

            Daniel's code in post #3 will move only prefixes that consist of the letter "t" followed by a single digit followed by an underscore to the end of the name.

            My code in post #2 will move only prefixes that consist of two characters followed by an underscore to the end of the name.

            Your explanation in post #1 does not fully describe the range of prefixes that you wish to move. Daniel assumed they will start with a t, I did not. Daniel assumed that there would be no prefixes like "t5_" that were not meant to be moved. I assumed that the prefixes might comprise any two characters followed by an underscore.

            So it is certainly not the case that "all variable names" will be changed. Perhaps some variable names that you would not want to be changed will be changed, but if so, that is a result of the incomplete description of the problem given in post #1.

            Comment

            Working...
            X