Announcement

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

  • Delete words and rename variables

    Dear All

    How can I delete first words from a variable and retain the remaining words. Please find below an example of my data set. I want to rename the variable and keep only the first word. For example ,my first variable is respondentgewog_dzongkhag, I want to rename it and keep gewog_dzongkhag. Ofcourse I can use rename but I have to do that for the other 100 variables. Is there a faster way since the word to be deleted is only "respondent" from all variables.

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str16 respondentgewog_dzongkhag str23 respondentgewog str75 respondentorg_name float(respondentfieldnamelatitude respondentfieldnamelongitude) int respondentfieldnamealtitude float respondentfieldnameaccuracy str18 respondentservice
    "trashigang" "kangpara"      "Gewog"                       27.12458 91.69782    0        20 "procurementofworks"
    "trashigang" "kangpara"      "Gewog"                        27.0907 91.63435 1616 4.5509996 "procurementofworks"
    "sarpang"    "sershong"      "Shershog"                    26.88797 90.55009  295    13.653 "procurementofworks"
    "sarpang"    "chhuzaggang"   "Chuzaggang"                  26.86816 90.52514  211    12.136 "other"             
    "trashigang" "kangpara"      "Khangpara Gewog"            27.126986 91.70812    0  3599.999 "procurementofworks"
    "trashigang" "kangpara"      "Kangpar Gewog"              27.127054 91.71771 1801         7 "procurementofworks"
    "trashigang" "kangpara"      "Kangpara gewog"              27.17652 91.80563    0         0 "procurementofworks"
    "sarpang"    "sershong"      "SERSHONG GEWOG"             26.890184 90.53951  285     7.585 "other"             
    "samtse"     "tendruk_tendu" "Tendu Gewog Administration"  27.12628 88.87532  946    10.619 "other"             
    "samtse"     "tendruk_tendu" "Dawa Dema construction"      27.12633 88.87543  979         8 "procurementofworks"
    end
    ------------------ copy up to and including the previous line ------------------

    Thank you

  • #2
    Hi Gliby, you may try this:
    Code:
    foreach var of varlist _all{
      local var_old="`var'"
      local var_new=substr("`var_old'",11,.)
      rename `var_old' `var_new'
    }
    2B or not 2B, that's a question!

    Comment


    • #3
      Code:
      rename respondent* *
      is a simpler way. rename will loop internally. See

      Code:
      help rename groups

      Comment


      • #4
        Yes, Nick offers an excellent new option and this would be much more convenient. I think using -rename- alone would be a better solution in this case.
        2B or not 2B, that's a question!

        Comment


        • #5
          Thank you so much

          Comment


          • #6
            I am guessing that Anna is your first or given name and Gliby your family name or surname. If so, it's up to you but you could use the CONTACT US button to get the forum administrators to change your identifier to "Anna Gliby",

            Comment

            Working...
            X