Announcement

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

  • Problem with reshape

    I am trying to reshape a wide data to a long one. My data includes several data ending in 0, 6, 18, and 36. Given that my data includes a lot of number of variables, it is inconvenient to specify them. Instead, I followed the instruction here to avoid specifying the variables that I want to reshape but it did not work. Below is what I tried as well as my variables.

    My code:
    unab vars : *0 *6 *18 *36

    local stubs0 : subinstr local vars "0" "", all
    local stubs6 : subinstr local vars "6" "", all
    local stubs18 : subinstr local vars "18" "", all
    local stubs36 : subinstr local vars "36" "", all

    reshape long `stubs0' `stubs6' `stubs18' `stubs36' , j (Time) i (UID Cohort)
    my variables:
    ------------------------------------------------------------------------------------
    Variable Storage Display Value
    name type format label Variable label
    -------------------------------------------------------------------------------------
    UID str5 %-9s
    Cohort double %10.0g
    CDI0 double %10.0g
    EIN0 double %10.0g
    SAW0 double %10.0g
    SJU0 double %10.0g
    COM0 double %10.0g
    CMG0 double %10.0g
    INN0 double %10.0g
    NEG0 double %10.0g
    PMC0 double %10.0g
    VIS0 double %10.0g
    BOC0 double %10.0g
    IOC0 double %10.0g
    FDI0 double %10.0g
    ISC0 double %10.0g
    IPM0 double %10.0g
    LCM0 double %10.0g
    STH0 double %10.0g
    TPO0 double %10.0g
    ADV0 double %10.0g
    CCP0 double %10.0g
    FUT0 double %10.0g
    HEQ0 double %10.0g
    MCE0 double %10.0g
    SDH0 double %10.0g
    SAN0 double %10.0g
    Interpersonal0 double %10.0g
    Organization0 double %10.0g
    Community0 double %10.0g
    Personal0 double %10.0g
    CDI6 double %10.0g
    EIN6 double %10.0g
    SAW6 double %10.0g
    SJU6 double %10.0g
    COM6 double %10.0g
    CMG6 double %10.0g
    INN6 double %10.0g
    NEG6 double %10.0g
    PMC6 double %10.0g
    VIS6 double %10.0g
    BOC6 double %10.0g
    IOC6 double %10.0g
    FDI6 double %10.0g
    ISC6 double %10.0g
    IPM6 double %10.0g
    LCM6 double %10.0g
    STH6 double %10.0g
    TPO6 double %10.0g
    ADV6 double %10.0g
    CCP6 double %10.0g
    FUT6 double %10.0g
    HEQ6 double %10.0g
    MCE6 double %10.0g
    SDH6 double %10.0g
    SAN6 double %10.0g
    CDI36 double %10.0g
    EIN36 double %10.0g
    SAW36 double %10.0g
    SJU36 double %10.0g
    COM36 double %10.0g
    CMG36 double %10.0g
    INN36 double %10.0g
    NEG36 double %10.0g
    PMC36 double %10.0g
    VIS36 double %10.0g
    BOC36 double %10.0g
    IOC36 double %10.0g
    FDI36 double %10.0g
    ISC36 double %10.0g
    IPM36 double %10.0g
    LCM36 double %10.0g
    STH36 double %10.0g
    TPO36 double %10.0g
    ADV36 double %10.0g
    CCP36 double %10.0g
    FUT36 double %10.0g
    HEQ36 double %10.0g
    MCE36 double %10.0g
    SDH36 double %10.0g
    SAN36 double %10.0g
    Interpersonal6 double %10.0g
    Interpersonal36 double %10.0g
    Organization6 double %10.0g
    Organization36 double %10.0g
    Community6 double %10.0g
    Community36 double %10.0g
    Personal6 double %10.0g
    Personal36 double %10.0g
    CDI18 double %10.0g
    EIN18 double %10.0g
    SAW18 double %10.0g
    SJU18 double %10.0g
    COM18 double %10.0g
    CMG18 double %10.0g
    INN18 double %10.0g
    NEG18 double %10.0g
    PMC18 double %10.0g
    VIS18 double %10.0g
    BOC18 double %10.0g
    IOC18 double %10.0g
    FDI18 double %10.0g
    ISC18 double %10.0g
    IPM18 double %10.0g
    LCM18 double %10.0g
    STH18 double %10.0g
    TPO18 double %10.0g
    ADV18 double %10.0g
    CCP18 double %10.0g
    FUT18 double %10.0g
    HEQ18 double %10.0g
    MCE18 double %10.0g
    SDH18 double %10.0g
    SAN18 double %10.0g
    Interpersonal18 double %10.0g
    Organization18 double %10.0g
    Community18 double %10.0g
    Personal18 double %10.0g
    -----------------------------------
    Last edited by Nader Mehri; 17 Aug 2023, 20:07.

  • #2
    There are a couple of problems here. One of them is that because any variable that ends in 36 also ends in 6, local macro stubs6 will contain several stubs that now end in 3--for which there is no corresponding variable in the dataset. The other is that apart from the 36:6 problem, the names of the variables ending in different numbers are largely, if not entirely (I didn't check carefully), the same, so that stubs0, stubs18, and stubs36 will be highly repetitious of each other, if not identical. -reshape- will not allow you to repeat the same stub more than once.

    The following should solve your problem:
    Code:
    unab stubs : *0 *6 *18 *36
    foreach n of numlist 36 18 6 0 { // IMPORTANT THAT 36 PRECEDES 6
        local stubs: subinstr local stubs "`n'" "", all
    }
    local stubs: list uniq stubs // REMOVE DUPLICATE MENTIONS
    
    reshape long `stubs', i(UID Cohort) j(Time)
    Note: Because you did not provide example data to work with, this code is untested. It may contain typos or other errors, but it contains the gist of a correct solution. If it does not solve your problem, when posting back, please show example data, using the -dataex- command to do so. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Also, when posting back, and more generally, in the future, please do not say that something "didn't work." There are many ways code may fail to perform as expected, and saying that it "didn't work" leaves the failure mode to be guessed it. Your chances of getting a timely and helpful response are improved when are specific about what went wrong. The best way to do that is to show (not describe) the exact code you used and the exact output, including any error messages, that Stata gave you, as well as an example of the resulting state of the data in memory. Then explain, or better still, show, how what happened differed from what you expected/wanted.

    The more, and more exact and relevant information you provide in your posts, the better the chance that somebody will be able to help you quickly and correctly.


    Comment

    Working...
    X