Announcement

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

  • Rename To Reshape

    Hi All,

    Suppose you have repeated variables at different assessments: A1SMOKE A2SMOKE A3SMOKE A4SMOKE. And you want to reshape these into long format. How can I loop it to rename them SMOKE1 SMOKE2 SMOKE3 SMOKE4 or SMOKEA1 SMOKEA2 SMOKEA3 SMOKEA4. I have to do this for many variables but have not seen a loop that will assist. Cheers.

  • #2
    Code:
    rename *SMOKE SMOKE*
    Or if you mean to say you have more such repeated variable names components:
    Code:
    local stubnames "SMOKE XYZ"
    foreach stub of local stubnames{
    ren *`stub' `stub'*
    }
    Last edited by Jorrit Gosens; 20 Feb 2018, 05:39.

    Comment


    • #3
      Hi Leon,

      without a proper data example (see the FAQ, especially section 12.2, on how to create such), this is only guesswork, but I don't think you have to rename your variables at all. reshape's syntax features a mechanism to directly specify where in the variable (stub-)names it should search for the enumerator:
      Code:
      clear
      input id A1SMOKE A2SMOKE A3SMOKE A4SMOKE
      1 1 1 1 1
      2 2 2 2 2
      3 3 3 3 3
      end
      reshape long A@SMOKE , i(id) j(wave)
      See help reshape.

      Kind regards
      Bela

      Comment


      • #4
        Jorrit and Daniel both give excellent advice. But I'll add that on the whole, and in my experience, making prefixes informative is the better way to go. So, I'd prefer names smoke* to names *smoke.

        Comment


        • #5
          I hope this helps, it will not rename your variables but once they are well renamed then you can incorporate the prefix of your variables in foreach line and the resultant variable in the reshape line; you should have something that looks like this:
          local stubnames "SMOKE XYZ" foreach stub of local stubnames{ ren *`stub' `stub'* } ***then...

          foreach v in smoke* climate* truck*{
          local l`v' : variable label `v'
          }
          macro list

          reshape long smoke climate truck, i(id) j(time)

          foreach v of var * {
          local L`v' : variable label `v'
          if `"`L`v''"' == "" & `"`l`v''"' != "" {
          label var `v' `"`l`v''"'
          }
          }
          Last edited by Festus odingo; 21 Feb 2018, 02:05.

          Comment


          • #6
            This won't work. Look at

            Code:
            foreach v in smoke* climate* truck*{
            local l`v' : variable label `v'
            }
            That is going to fail first time round the loop when you try

            Code:
            local l`v' : variable label smoke*
            as you're feeding a wildcard where a variable name is expected.

            Comment


            • #7
              Is it possible to reshape long when your data look like
              A5SMOKE A1SMOKE A2SMOKE A3SMOKE A4SMOKE
              0 1 1 0 1
              1 1 1 0 0

              Comment


              • #8
                #7 looks like the same question as already answered. If the difference is the order of variables, that's immaterial.

                #3 already gave advice on better ways to present data. We really want you to take it.

                Comment

                Working...
                X