Announcement

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

  • renaming a group of variables

    Hi, everyone.
    I hope you are doing very well.
    There is a letter at the end of variable names I want to remove that letter from the end of variable names and add sequential numbers to the end of the variables
    They are the variables having these names q237a, q237b,...., q237ab, q237ac, and so on. I want to change them to q2371, q2372,.......up to the end.
    could anyone help me, please?
    Thanks beforehand

  • #2
    Code:
    clear 
    set obs 1 
    foreach v in q237a q237b q237ab q237ac {
        gen `v' = 42 
    }
    
    rename (q237? q237??) (q237#), renumber 
    
    list 
    
         +-------------------------------+
         | q2371   q2372   q2373   q2374 |
         |-------------------------------|
      1. |    42      42      42      42 |
         +-------------------------------+

    Comment


    • #3
      Mr Cox, thank you very much for your response.
      But unfortunately, there are a lot more many variables that I did not write all of them down above. All variables are as below.
      How can we function the above-mentioned command for them?
      Is it possible to use a loop because of the large number of variables?
      q237a q237b q237c q237d q237e q237f q237g q237h q237i q237j q237k q237l q237m q237o q237n q237p q237q q237r q237s q237t q237u q237v q237w q237x q237y q237z q237aa
      q237ab q237ac q237ad q237ae q237af q237ag q237ah q237ai q237aj q237ak q237al q237am q237an q237ao q237ap q237aq q237ar q237as q237at q237au q237av q237awa q237aw
      q237ax q237ay q237az q237ba q237bb q237bc q237bd q237be q237bf q237bg q237bh q237bi q237bj q237bk q237bl q237bm q237bn q237bo q237bp q237bq q237br q237bs q237bt
      q237bu q237bv q237bw q237bx q237by q237bz q237ca q237cb q237cc q237cd q237ce q237cf q237cg q237ch q237ci q237cj q237ck q237cl q237cm.
      thanks

      Comment


      • #4
        Code:
         
         rename (q237? q237??) (q237#), renumber
        is a loop in disguise and should work with your set-up. Did you try it? P.S. The title Mr isn't what I call myself. Nick is fine here, so please use it.

        Comment


        • #5
          I am really sorry for not being aware of your title, I will call you Nick after this.
          In the case of the question, I did it like below but did not work.
          foreach v in q237* {
          gen `v' = 42
          rename (`v'*) (`v'#), renumber
          }
          Last edited by Taqi Nabizada; 09 Aug 2023, 04:19.

          Comment


          • #6
            You don't need to apologise for calling me Mr: you could hardly be aware of my preference on that detail.

            But you should please focus on my suggested code!

            As implied in #2 and reiterated in #4 my suggestion is already a loop and so there is no need to write your own.

            Your own code is very buggy.

            Your variables already exist and you have no need to create them. My code in #2 created a facetious data example because you didn't give one, but it was intended to be instructive on technique.

            Further, your loop would need to start

            Code:
            foreach v of var q237*
            because foreach is strict on needing of not in.

            The rename command inside the loop is also quite wrong. It could be rewritten, something like this,

            Code:
            local j = 1 
            
            foreach v of var q237* { 
                  rename `v'  q237`j' 
                  local ++j 
            }


            But that's pointless because you already have a solution, given back in #2.

            Comment


            • #7
              Thank you, Nick, #6 command worked well and solved my problem.
              # 2 errors message (q237 variable is already defined).

              Comment


              • #8
                Hi Nick, I have a question which is related to this renaming on numbers. I have variables named: year_1945, year_1946, year_1947,...., year_2020. I want to change those variables 10 years earlier such as: year_1935, year_1936, year_1937,..., year_2010. Instead of renaming it one by one, is it possible to do that in a loop?

                Comment


                • #9
                  Code:
                  rename year_# year_#, renumber(1935)
                  This follows from functionality explained at #17 in

                  Code:
                  help rename groups

                  Comment


                  • #10
                    Thanks Nick. it works perfectly.

                    Comment

                    Working...
                    X