Announcement

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

  • Reshaping long an usual dataset

    Dear Statalist,

    I know how to reshape long a dataset when it has the appropriate format, but I'm facing two unusual situations that require assistance. In both cases, I would like to have a dataset that looks like this:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4 name str2 id
    "xxxx" "K1"
    "yyyy" "K2"
    "zzzz" "K3"
    "aaaa" "K4"
    end
    However, the two datasets I have in front of me have this format:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4(DV DW DX DY) str2(FT FU FV FW)
    "xxxx" "yyyy" "zzzz" "aaaa" "K1" "K2" "K3" "K4"
    end
    i.e. the name variable and the id variable are in one line. Here is the second dataset :

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4(DV DW DX DY)
    "xxxx" "yyyy" "zzzz" "aaaa"
    "K1"   "K2"   "K3"   "K4"  
    end
    Could someone help me finding how to reach the first example from the two last examples I provided? Thank you so much!

  • #2
    Order matters here in the rename command. See

    Code:
    help order
    if you need to redefine the order of variables.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4(DV DW DX DY) str2(FT FU FV FW)
    "xxxx" "yyyy" "zzzz" "aaaa" "K1" "K2" "K3" "K4"
    end
    rename (DV-DY) name#, addnumber(1)
    rename (FT-FW) id#, addnumber(1)
    gen obsno=_n
    reshape long name id, i(obsno) j(which)
    drop obsno which
    Res.:

    Code:
    . l
    
         +-----------+
         | name   id |
         |-----------|
      1. | xxxx   K1 |
      2. | yyyy   K2 |
      3. | zzzz   K3 |
      4. | aaaa   K4 |
         +-----------+

    Comment


    • #3
      Andrew :

      Thank you for the fast reply! This code works like a charm. If I may ask just one more question, is there a way to make this code more suitable for a loop by renaming "the first half" of the dataset "name#" and "the other half" "id#" ? Because the number of variables to be named "name" might vary for different files.

      Thanks again for your precious help.

      Comment


      • #4
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str4(DV DW DX DY) str2(FT FU FV FW)
        "xxxx" "yyyy" "zzzz" "aaaa" "K1" "K2" "K3" "K4"
        end
        qui ds
        rename (`=word("`r(varlist)'", 1)' - `=word("`r(varlist)'", c(k)/2)') name#, addnumber(1)
        rename (`=word("`r(varlist)'", `=c(k)/2+1')' - `=word("`r(varlist)'", c(k))') id#, addnumber(1)
        gen obsno=_n
        reshape long name id, i(obsno) j(which)
        drop obsno which

        Comment


        • #5
          Dear Andrew: Thank you very much!

          Your code worked. Sorry for the delayed answer

          Comment

          Working...
          X