Announcement

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

  • Long format to Wide format reshape

    Hello,

    I have a dataset that requires me to reshape the format from long to wide based on the project analysis needed. Below is an example of what my dataset looks like:
    id event_id date_of_event event_num
    1 20 12/5/2017 06:21:37 5
    2 20 10/22/2017 08:07:03 9
    3 20 11/14/2016 09:57:57 1
    4 20 10/30/2016 09:32:11 7
    4 20 10/30/2016 09:06:34 6
    4 20 10/30/2016 07:36:31 4
    5 20 1/1/2018 06:21:02 2
    5 20 12/18/2017 09:43:10 2
    6 20 10/15/2016 12:27:46 0
    I tried using the following code below but got the error message : variable date_of_event takes on too many values

    reshape wide event_num, i(id date_of_event) j(num)

    I'm obviously doing something wrong but I don't know what exactly that is. I'm new to STATA and I'm still learning the STATA jargons, so any help provided will be well appreciated.

    Thank you!

  • #2
    I have never seen that error message from -reshape- before, and I'm not sure what to make of it.

    I'm surprised, though, that you even got that far. Your -reshape- command specifies a variable, num, in the -j()- option that does not exist, at least not in the example data you show. As a result, it is also not clear to me what you want the end result of this to look like. In particular, note that had your command been able to run, and assuming that you actually do have some variable named num, based on the example data you show, there would be no change to your data set because the combination of id and date_of_event, the variables you specified in -i()-, uniquely identify observations.

    Bear in mind that when you -reshape wide- you severely restrict your ability to carry out further data management and analysis in Stata. While there are situations where you do need to do this, it isn't apparent from what you show that you are facing such a situation.

    For a more helpful response, I suggest you post back with a usable data example, that is, one created by the -dataex- command. Also show what you want the end result to look like, and explain why you want to go to wide layout in the first place.

    If you are running version 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.
    Last edited by Clyde Schechter; 26 Jan 2023, 10:03.

    Comment

    Working...
    X