Announcement

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

  • Rename Variables with "." in it

    Hi!
    I am using the "LatinobarĂ³metro" database. There are a few variables with "." in the middle (e.g. "P15STGBSC.A"), I am trying to rename them because otherwise I can't use them properly.
    I have already tried things such as:

    rename P15STGBSC* (A B C D E F G H)

    But it shows error. How can I rename these variables?

    First time in Statalist, sorry if I am not clear enough or if my english is not that well (is not my native language).

    Thanks!

  • #2
    Welcome to Statalist !

    If they have a dot in the middle as you say, they cannot be variable names in the first place. I'm assuming they are actually located in the first observation of your dataset. Is it possible to see an example of your dataset using -dataex- ? You just have to enter the command in Stata with your dataset loaded and paste the output here

    Without looking at your data my intuition tells me your problem might be solved with the -subinstr()- function. Try to have a look at

    Code:
    help subinstr()

    Comment


    • #3
      As their names have dots in the middle, I can't use dataex with these variables, so here is a screenshot of the Data Editor. I know is the least elegant way to show the data but I don't know a better way to do it because of the dots.
      Click image for larger version

Name:	Captura de pantalla 2023-03-01 111525.png
Views:	1
Size:	126.4 KB
ID:	1703965



      Sorry for the horrible presentation.

      Comment


      • #4
        Hola, Lucas.
        The mechanical and simple way would be to do one by one:

        Code:
        rename P15STGBSC.A P15STGBSCA
        That would be the easy way of doing it, just make sure that you have all of them.
        Is there a reason why you would like to do this?
        The way I understand it is...before the "." is a question and after the "." is the option.
        For example, in the item that you show...I see a question about confidence...I would answer that I don't trust my government, but I trust the Catholic Church :P. Therefore, My answer in P15...C would have a 1.

        I hope this helps.
        Kind regards,
        Jorge

        Comment


        • #5
          Also, there is a trick in Excel...and you can copy and paste it in Stata.
          1. Make sure you have all the variables that you need to rename at hand. Maybe, with the "des" command.
          2. Once you have all the variables that you want to rename, copy them to excel.
          3. Create a column that says rename
          4. Create a column that lists all the variables that you wish to rename
          5. Create a column where you list all the variables that you want to rename without the "."
          6. Crete a variable that you concatenate "rename + the variable that you wish to rename + the name name without the "." and make sure that you include the spaces
          7. Copy that code into Stata that should be lots of rename X.A XA...and so on...

          There you go. You are welcome.

          Saludos,
          Jorge

          Comment


          • #6
            Thanks for the advice Jorge, I did exactly what you suggested but it still doesn't work. The problem is the same as if I just typed:

            Code:
            rename P15STGBSC.H P15STGBSCH
            It happens with each variable I am trying to rename. Stata doesn't allow to include dots in the oldname var.

            Comment


            • #7
              Hola, Lucas:
              Another solution then would be to create a row / line where you have the variable names.
              Is your data originally in Excel or in Stata?
              We can work from there. If we have the data in Excel maybe you could import it and make sure that the first row is NOT the variable name. From there you can do what our friend Thomas Brot suggested and use the command substr( ) to get rid of the ".".
              Once that is done, you convert the first row to variable name.

              Kind regards,
              Jorge

              Comment


              • #8
                Use Mata.

                Code:
                version 17
                
                mata :
                
                void fix_invalid_varnames()
                {
                    real scalar k
                    
                    
                    for (k=1; k<=st_nvar(); k++)
                        if ( !st_isname(st_varname(k)) )
                            st_varrename(k, ustrtoname(st_varname(k)))
                }
                
                end
                The function above will fix invalid variable names. For example, it will change P15STGBSC.H to P15STGBSC_H. You can then use the regular rename command.

                Obviously, a more direct approach, identifying variable names with a dot (.) is possible; I prefer the more general solution.


                Edit: I should have provided more explicit guidance. The code above defines the Mata function. Copy the code into a do-file and add the line

                Code:
                mata : fix_invalid_varnames()
                to call the function and fix the variable names. Then, add the line

                Code:
                rename (*_?) (*?)
                to obtain the final variable names.

                By the way from the screenshot, it seems you should also unicode translate your file.



                Last edited by daniel klein; 01 Mar 2023, 12:49.

                Comment


                • #9
                  Hi daniel klein ,

                  We worked it out in the end. We saved the data as an Excel spreadsheet and then we imported it and replaced the first row. Then, we turned the first row into variable names.
                  Please, see below

                  Code:
                  import excel latinobarometro.xlsx, clear
                  
                  *We got rid of the "." here
                  foreach var of varlist A-OE {
                      replace `var'=subinstr(`var',".","",.)    
                  }
                  
                  We replaced the variable names with the first row here
                  foreach v of varlist * {
                     local vname = strtoname(`v'[1])
                     rename `v' `vname'
                  }
                  drop in 1
                  
                  saveold latinobariometro, replace
                  It worked out just fine.

                  Kind regards,
                  Jorge

                  Comment


                  • #10
                    Originally posted by Jorge L. Guzman View Post
                    Hi daniel klein ,

                    We worked it out in the end. We saved the data as an Excel spreadsheet and then we imported it and replaced the first row. Then, we turned the first row into variable names.
                    Great if that worked for you. The problem with the approach is that you necessary are importing all data as string; that typically leads to further problems or at least to the need to destring.

                    Comment

                    Working...
                    X