Announcement

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

  • Uses using wildcard to rename varlists in a loop

    Hi,

    I'm trying to merge data from a yearly questionnaire to form a panel.

    The date follows the following format:
    • cp08a_m
    • cp09b_m
    • cp10c_m
    That is, cp, two numbers representing the year, a letter and then _m. I want to rename them all as "date".

    Meanwhile, the questions' name follow the follow pattern:
    • cp08a001
    • cp08a002
    • cp09b001
    • cp09b002
    That is, cp, two numbers representing the year, a letter and then three numbers representing the question number.

    So far, I've tried this:

    Code:
    forval n = 1/10 {
        use "$path\Originales\Personality`n'.dta", clear
        tsunab year : c*_m
        rename `year' date
        replace date = 2007+`n'
        tsunab q : c*(###)
        rename `q' P(###)
        save "$path\Modifiees\Personality`n'_m.dta", replace
        clear
    }
    It successfully renames everything into date but the second tsunab, " tsunab q : c*(###)", returns "# invalid name". I've tried basically every iteration with wildcards I can think of, to no avail. I always get an invalid name or an unfound variable (e.g., c).

    Thank!
    Last edited by Samy Gallienne; 02 Apr 2019, 13:13. Reason: wildcard loop rename

  • #2
    I am guessing what you want -- I can't see that you say directly what you want done with names like cp08a001 -- but I am imagining you want to strip out the year element for a latter append.

    How about this?

    Code:
    forval n = 1/10 {
        * result in 07 08 09 10 ... 15 16 
        local which : di %02.0f `n' + 6 
        use "$path\Originales\Personality`n'.dta", clear
        drop c*_m
        gen date = 2007+`n'
        rename cp`which'* P* 
        save "$path\Modifiees\Personality`n'_m.dta", replace
        clear
    }
    Commenting on your code:


    Code:
     
     tsunab year : c*_m rename `year' date replace date = 2007+`n'
    As you know that c*_m catches just one variable, then you don't need tsunab to unpack it. Just drop it and create the date variable directly.


    Code:
     
     tsunab q : c*(###) rename `q' P(###)
    This seems more confused. I didn't know that tsunab supports a syntax anything like that. But what you want seems to be just a direct rename. Again, tsunab is a distraction.

    The small trick to mapping 1 to 10 to 07 to 16 is documented in https://journals.sagepub.com/doi/pdf...867X1001000115

    Comment


    • #3
      Originally posted by Nick Cox View Post
      As you know that c*_m catches just one variable, then you don't need tsunab to unpack it. Just drop it and create the date variable directly.
      I feel incredibly stupid for not having thought of this. Thank you.

      Originally posted by Nick Cox View Post
      This seems more confused. I didn't know that tsunab supports a syntax anything like that. But what you want seems to be just a direct rename. Again, tsunab is a distraction.

      The small trick to mapping 1 to 10 to 07 to 16 is documented in https://journals.sagepub.com/doi/pdf...867X1001000115
      Yes, it's simply a rename but I can't seem to form the varlist to do so.

      Even with your code, it returns "variable cp07 not found" or "variable cp08 not found" (if I correct the +6 to +7).

      Comment


      • #4
        I forgot to mention: it's not a continuous variable That is, there may be cp08a001 and cp08a003 but no cp08a002.

        Also, I'm using version 11.

        I have no idea if that matters or not but I thought I'd mention it.
        Last edited by Samy Gallienne; 02 Apr 2019, 17:29.

        Comment


        • #5
          Hah!

          https://www.stata.com/help.cgi?whatsnew11to12 documents that

          16. Renaming groups of variables is now easy using rename's new syntax that is 100% compatible with its old syntax. You can change names, swap names, renumber indices within variable names, and more. See [D] rename group.
          and that is what my code was using. Also, https://www.statalist.org/forums/help#version explains

          11. What should I say about the version of Stata I use?

          The current version of Stata is 15.1. Please specify if you are using an earlier version; otherwise, the answer to your question may refer to commands or features unavailable to you. Moreover, as bug fixes and new features are issued frequently by StataCorp, make sure that you update your Stata before posting a query, as your problem may already have been solved.

          Let me try again. It seems that your files 1 to 10 carry information for 2008 to 2017 (sorry that I confused 6 and 7) so my second stab at a solution is

          Code:
          forval n = 1/10 {
              * result in 08 09 10 ... 15 16 17 
              local which : di %02.0f `n' + 7 
              use "$path\Originales\Personality`n'.dta", clear
              drop c*_m
              gen date = 2007+`n'
              foreach v of var cp`which'* { 
                  local new : subinstr local v "cp`which'" "P", all 
                  rename `v' `new' 
              }
              save "$path\Modifiees\Personality`n'_m.dta", replace
              clear
          }

          Comment


          • #6
            Oh, thank you!

            I didn't think of mentioning the version initially because I couldn't find anything about rename changing from version 11 to 12. I'm sorry for the inconvenience.

            Comment


            • #7
              Any inconvenience is more your end in terms of the thread taking more time. Meanwhile, I have to be curious about where the ### syntax came from in #1. Were you working by analogy with other software? or something else? As with the excellent 5th amendment to the US Constitution, you don't have to incriminate yourself.

              Comment


              • #8
                I might have misread this (which is for version 13, if I read the url correctly).

                Comment


                • #9
                  Indeed. That is a public version of the manuals for Stata 13. As I write,

                  0. The manuals for Stata 12 and earlier are not available online.

                  1. The manuals for Stata 13 are available online at https://www.stata.com/manuals13

                  2. The manuals for Stata 14 are available online at https://www.stata.com/manuals14

                  3. The manuals for Stata 15 are available online at https://www.stata.com/manuals [sic]

                  from which I guess that it's StataCorp's practice to postdate previous versions whenever a new version of Stata appears.

                  It's hard to improve on the tautology that if you have Stata X (in your case X == 11) the manuals for Stata Y (Y > X) will be useful as indicating

                  (a) what's possible in Stata Y

                  (b) what's possible in Stata X if and only if Stata X functionality was the same as in Y on the point you're looking up.

                  I've forgotten already at what point the manuals were bundled with Stata as .pdf, so that anyone with access to a legally purchased copy of Stata has automatic access to them. Over much of Stata's history the manuals had to be purchased separately as paper copies.

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    I've forgotten already at what point the manuals were bundled with Stata as .pdf, so that anyone with access to a legally purchased copy of Stata has automatic access to them.
                    That came with Stata 11.

                    Best
                    Daniel

                    Comment

                    Working...
                    X