Announcement

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

  • Problem with reshape command

    Hello!

    I have a dataset with 106 observations and 3,165 variables. There are 5 individual surveys included in the dataset (e.g. 5 different time points: baseline, month 2, month 3, month 4, month 5). The variables that appear in all 5 surveys are differentiated by the following variable suffixes: "_1" for baseline, "_2" for month 2, "_3" for month 3, "_4" for month 4, and "_5" for month 5. The stubnames for the variables are relatively complex, and some contain several underscores and/or numbers within the stubname that may impact my ability to reshape the data from its current wide format to long format.

    Some examples of the format of the stubnames in my dataset are as follows.
    varname
    varname1
    varname01
    varname_100
    varname___1

    With the month 3 suffix added as an example, the variables look as follows:
    varname_3
    varname1_3
    varname01_3
    varname_100_3
    varname___1_3

    Is it possible to reshape the dataset from wide to long with the above stubnames and suffixes? One of the variables I would like to reshape has the stubname "qol01" and appears in all 5 surveys as qol01_1 qol01_2 qol01_3 qol01_4 qol01_5. I have tried using the string option and the @ option to reshape by survey time point, but keep getting the error message "no xij variables found".

    I am sure there is something very simple I am missing here, but would very much appreciate some advice as to whether I will be able to reshape given the current naming format of my variables.

    Thanks!
    Hannah



  • #2
    Welcome to Statalist, Hannah.

    Let's take your questions in reverse order.

    Without your having shown us the actual code you have tried and the error message Stata issues, I cannot be sure, but the simple thing you are missing is perhaps an understanding that for purposes of the reshape command, if you have variables
    Code:
    qol01_1 qol01_2 qol01_3 qol01_4 qol01_5
    the stubname will be qol01_ rather than qol01 as you stated. The following commands should do what you want, assuming that you have a variable which I call id that distinctly identifies observations.
    Code:
    reshape long qol01_, i(id) j(survey)
    rename qol01_ qol01
    which will replace an observation having the variables
    Code:
    id qol01_1 qol01_2 qol01_3 qol01_4 qol01_5
    with five observations having the variables
    Code:
    id survey qol01
    where survey will take on the values 1 through 5.

    With that easy answer out of the way, for your more general problem, the reshape command will look like
    Code:
    reshape long varname_ varname1_ varname01_ varname_100_ varname___1_, i(id) j(survey)
    followed by
    Code:
    rename (*_) (*)
    which for every variable ending in an underscore will remove that underscore. This is documented in the output of help rename group which I encourage you to read,

    Let me add a piece of advice about getting the most out of Statalist. As I alluded to above, your omission of your actual commands and the corresponding error messages means we're left to guess what you did that failed. It would have been easier to just copy your command and correct the copy, without having to wonder if there were other possibilities I'd overlooked.

    Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

    Section 12.1 is particularly pertinent

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Comment


    • #3
      Thank you William. That helped a lot. I will be sure to include the code I use in future posts.

      Comment

      Working...
      X