Announcement

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

  • Remove characters from middle of variable name

    Hello!

    I am working with a longitudinal data with repeat measures. The person programming the survey placed suffixes do denote survey at the end of most variables, but for some, the "suffix" is actually in the middle of the variable. For example, what should be fu_a_time___1 is fu_a_time_v2___1. This is making reshaping the data quite difficult and so I am trying to figure out how to efficiently drop the v2 from with the name for visits 2-9. I have tried this code:

    Code:
    foreach var of varlist fu_a_time_v2___1 [rest of vars]   {
        local newname : subinstr local var "__v2" "", all
        if "`newname'" != "`var'" {
            rename `var' `newname'
        }
    }
    While it does not produce an error, the data in memory don't actually change.

    Any ideas or suggestions would be welcomed!

  • #2
    I read your code as looking for __v2 (underscore underscore v 2) but your example mentions only one variable name and it has infix _v2 (underscore v 2). So on that evidence it's not a surprise that nothing was changed yet there was no error. It is like asking to remove a caat but Stata can't find any yet you really meant cat.

    Comment


    • #3
      Code:
      help rename group
      Code:
      clear
      input str5 (fu_a_time_v2___1 fu_a_time_v2___5 fu_a_time_v2___9)
      "" "" ""
      end
      
      rename (fu_a_time_v2___*) (fu_a_time___*)
      Res.:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str5(fu_a_time___1 fu_a_time___5 fu_a_time___9)
      "" "" ""
      end

      Comment


      • #4
        Thank you both! It was indeed a case of an extra underscore.

        Comment

        Working...
        X