Announcement

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

  • Use foreach to iterate over each variable and rename it based on value in first and second row

    I am fairly new to Stata, but I am used to Python and R where something like this seems easier to do. Basically, I want to iterate over each variable and rename it the value of its first and second row. For example, if the data looked like this:
    A1 A2 A3 A4 ...
    C99 C92 C91 C30 ...
    J J K K
    I want to transform it to this:
    C99J C92J C91K C30K
    C99 C92 C91 C30 ...
    J J K K ...

    So far, I have a loop that looks like so:

    unab varlist : *
    foreach i of loc varlist {
    display `i'[1]
    }


    Which displays the value of each first row. I am confused on how you rename based on those values though. If I try the following to just name it by the first row:

    unab varlist : *
    foreach i of loc varlist {
    rename `i' `i'[1]
    }


    It does not appear to work. Any help would be greatly appreciated!




  • #2
    Welcome to Statalist. Perhaps something like this

    Code:
    clear
    input str3(A1 A2 A3 A4)
    "C99" "C92" "C91" "C30"
    "J"   "J"   "K"   "K"  
    end
    
    foreach v of varlist _all {
        ren `v' `=`v'[1] + `v'[2]'
    }
    Result
    Code:
      +---------------------------+
      | C99J   C92J   C91K   C30K |
      |---------------------------|
      |  C99    C92    C91    C30 |
      |    J      J      K      K |
      +---------------------------+

    Comment


    • #3
      This problem is better solved at the stage of importing the data in Stata. Otherwise, the next question will probably be how to transform string variables into numeric ones... The answer to this is probably destring by the way.

      Comment


      • #4
        It is pretty much what Justin did.

        But I have an existential question to ask Carl Isaac. When "but I am used to Python and R where something like this seems easier to do", why dont you just do it in Python or R?

        Comment


        • #5
          What Justin did does not seem to work for me. I ended up getting:

          syntax error
          Syntax is
          rename oldname newname [, renumber[(#)] addnumber[(#)] sort ...]
          rename (oldnames) (newnames) [, renumber[(#)] addnumber[(#)] sort ...]
          rename oldnames , {upper|lower|proper}

          Joro, I have decided to start learning Stata in more depth since it is commonly used in the econ community. That way I have the ability to work on the same code with colleagues.

          Comment


          • #6
            For future reference, please copy and paste your exact command and the resulting Stata output. Most probably, your error results from spaces within strings, e.g., C 32 instead of C32. Stata variable names cannot contain spaces or characters other than letters, numbers and underscores. In addition, they cannot begin with numbers. The function -strtoname()- may help here, see

            Code:
            help strtoname()
            Code:
            foreach v of varlist _all {
                ren `v' `=strtoname(`v'[1] + `v'[2])'
            }
            Last edited by Andrew Musau; 30 Sep 2020, 15:52.

            Comment


            • #7
              Good luck with learning Stata then !

              Justin's code is fine and it should work. Most likely it is what Andrew says in #6.

              But if you try what Andrew suggested and still it does not work, do

              Code:
              set trace on
              then run the code. Look up to the message in red which is saying exactly where and why the error occurred. Then copy and paste here say 7 lines before and 5 lines after the message in red.



              Originally posted by Carl Isaac View Post
              What Justin did does not seem to work for me. I ended up getting:

              syntax error
              Syntax is
              rename oldname newname [, renumber[(#)] addnumber[(#)] sort ...]
              rename (oldnames) (newnames) [, renumber[(#)] addnumber[(#)] sort ...]
              rename oldnames , {upper|lower|proper}

              Joro, I have decided to start learning Stata in more depth since it is commonly used in the econ community. That way I have the ability to work on the same code with colleagues.

              Comment

              Working...
              X