Announcement

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

  • Rename or replace multiple variables

    Dear forum,

    With regards to my csv import issue, I am stuck with several datasets which need manual adjustment of variable values, due to incorrect import of the data. See https://www.statalist.org/forums/for...imiter-symbols, for my import issue.

    My general idea was to split one of my datasets in several parts, precisely one part which contains all data which is fine and other parts which need manual adjustment. After, I merge them back together.
    Suppose the original data has the variables, a - e, the imported that has variables a - e and v6, with a - e being random variable names. I split the data whether or not the observations have a missing value for v6 to determine, correctly and not correctly imported data.

    For the incorrect imported data, variable a should actually be a+b, b be c and so on.

    What I did was,
    Code:
    replace a = a + ";" + b
    drop b
    rename (c d e v6) (b c d e)
    which worked for me. However, I am looking for a more general solution if there is any as I am always renaming the variable with the variable name of the previous variable. Besides, renaming around 20 to 30 variables, most of them with long variable names looks pretty messy in my opinion.
    Does anybody know a way of dealing with this issue more smoothly?

    Alternatively, I tried:
    Code:
    replace a = a + ";" + b
    replace b = c
    ...
    replace e = v6
    drop v6
    This does only work if all my variables are string variables, which is not always the case. If, however this is the case, is there a way to loop this? I was thinking of something like,
    Code:
    foreach var of varlist b c d e v6{
        replace `var' = ...
    }
    but do not know how to specify that he should replace `var' with `var' +1.


    Thank you very much in advance.

    Best regards,
    Ali

  • #2
    As for the last thing.

    Code:
    local variables "a b c d e v6"
    local variablesToLoopOver "a b c d e"
    local counter = 1
    foreach var of local variablesToLoopOver {
        local counter = `counter' + 1
        local var_plusOne : word `counter' of `variables'
        
        di as result _newline "replacing `var' by `var_plusOne'" _newline
        replace `var' = `var_plusOne'
    }
    That might do what you want. See also my reply to your other post.

    If you are having troubles with strings and numbers mixing, I suggest you first -tostring- all your variables, as all numbers can be represented as strings. You can -destring- them again at the end.

    Comment


    • #3
      Dear Jesse,

      this works perfectly and is nice to adjust for similar problems I am facing with these datasets. Thank you very much!

      Comment


      • #4
        Below code might be more direct and also working with mixing-styles situation.
        Code:
        replace a = a + ";" + b
        ds a b, not
        local x `r(varlist)'
        ds a v6, not
        drop b
        ren (`x') (`r(varlist)')

        Comment


        • #5
          Dear Romalpa,

          thank you very much as well. I did not know about the -ds- command and this is exactly what I was looking for to optimize the renaming process.

          Best,
          Ali

          Comment

          Working...
          X