Announcement

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

  • Reshape from wide to long, error

    Good morning,

    I am using reshape on Stata 14. I am trying to convert my data from wide to long. I have data from a choice experiment, and each one of the 60 choice variables is called base_choice_n_ , where n is a number from 1 to 60.

    Now, the code I am using is
    Code:
     reshape long base_choice_, i(id) j(question_number)
    but an error message comes up, saying that question_number (my j variable) contains all missing values. Any advice about this matter would be greatly appreciated!

    Kind regards,

    Chiara Pastore

  • #2
    You can use the string option to the reshape command, but that will give you string values for your question_number variable, and I believe that's not what you want. Instead, you should rename your variables first in order that they have a numeric suffix. See below (begin at the "Begin here" comment).

    .ÿversionÿ14.1

    .ÿ
    .ÿclearÿ*

    .ÿsetÿmoreÿoff

    .ÿquietlyÿsetÿobsÿ2

    .ÿgenerateÿbyteÿidÿ=ÿ_n

    .ÿforvaluesÿiÿ=ÿ1/6ÿ{
    ÿÿ2.ÿÿÿÿÿÿÿÿÿgenerateÿbyteÿÿbase_choice_`i'_ÿ=ÿ0
    ÿÿ3.ÿ}

    .ÿ
    .ÿ*
    .ÿ*ÿBeginÿhere
    .ÿ*
    .ÿcaptureÿnoisilyÿreshapeÿlongÿbase_choice_,ÿi(id)ÿj(question_number)
    variableÿquestion_numberÿcontainsÿallÿmissingÿvalues

    .ÿrenameÿÿbase_choice_*_ÿÿbase_choice_*

    .ÿreshapeÿlongÿbase_choice_,ÿi(id)ÿj(question_number)
    (note:ÿjÿ=ÿ1ÿ2ÿ3ÿ4ÿ5ÿ6)

    Dataÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿwideÿÿÿ->ÿÿÿlong
    -----------------------------------------------------------------------------
    Numberÿofÿobs.ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ2ÿÿÿ->ÿÿÿÿÿÿ12
    Numberÿofÿvariablesÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ7ÿÿÿ->ÿÿÿÿÿÿÿ3
    jÿvariableÿ(6ÿvalues)ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ->ÿÿÿquestion_number
    xijÿvariables:
    base_choice_1ÿbase_choice_2ÿ...ÿbase_choice_6->base_choice_
    -----------------------------------------------------------------------------

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3
      So, you have variables with names like base_choice_37_, base_choice_42_? If so, I have a feeling the trailing underscore may be causing problems. Instead try names like base_choice_37, base_choice_42.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://academicweb.nd.edu/~rwilliam/

      Comment


      • #4
        Dear Joseph and Richard,

        Thank you for your reply! Problem was solved by renaming the variables as you suggested.

        Kind regards

        Comment

        Working...
        X