Announcement

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

  • Creating a loop to add a prefix to a variable

    Hi,

    I'm trying to create a loop to add a two letter prefix (e.g. AB) to a varlist I've merged into a dataset so I know that they weren't part of the original data.

    Does anyone have any ideas on how I could create a loop that would change varname to AB_varname for the whole list without easily? I've seen various posts about loops for renaming variables but they all seem to involve numbers

    Thanks

    Robert

  • #2
    Try this
    Code:
    foreach v of varlist _all{
    ren `v' AB_`v'
    }
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      No loops needed since Stata 12.

      Code:
      rename (*) (AB_*)
      Best
      Daniel

      Comment


      • #4
        Thanks, I'm a little confused as to what v refers to - is it a macro that I need to assign?

        Robert

        Comment


        • #5
          It is the name of the local macro that gets assigned in that first line of the foreach block, see also the guide: http://www.stata.com/manuals13/pforeach.pdf
          You can use whatever name. e.g.,:
          Code:
          foreach my_intuitive_local_name of varlist _all{
          ren `my_intuitive_local_name' AB_`my_intuitive_local_name'
          }
          However, as Daniel noted; no need for a loop.

          Comment


          • #6
            Hello All,
            I am using Stata 16
            I tried using the below code for creating a prefix to my 30 variables
            Code:
            foreach x of varlist catcurrentpolicieswarminglevel- ngfsdelayed2âc25thpercentile {
             rename `x' atmp_`x'
             }
            However I am getting the following error message
            1 new variable name invalid
            You attempted to rename catcurrentpolicieswarminglevel to atmp_catcurrentpolicieswarminglevel. That is an invalid Stata variable name.

            Edit: I did use
            Code:
            ren * x*
            and I still get the same error message.

            Kindly suggest what I may be doing wrong here? Grateful for any help!

            Last edited by Sanika Rr; 18 Feb 2022, 03:03.

            Comment


            • #7
              Stata variable names are at most 32 characters long.

              Code:
              . di strlen("atmp_catcurrentpolicieswarminglevel")
              35
              help varname explains. So, loop or no loop, the problem is the same.

              Comment


              • #8
                Thank you, Sir. I fixed it.

                Comment

                Working...
                X