Announcement

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

  • Subject: Reshaping Data from Wide to Long Format in Stata


    input byte IDstudy double(ssuction1 esuction1 ssuction2 esuction2) byte Elapsedtimeheartrate int(G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE)
    1 1.5 10.56 15 17.23 . 0 0 0 0 0 0 0 0 90 95 96 95 94 100 105 110 111 112 111 113 115 120 122 120 121 124 125 126 127 128 129 130 135 135 136 137 138 139 134 135 135 136 136 136 137 138 139 140 135 136 136
    2 2.76 3.76 10 16.6 . 135 140 146 138 143 146 147 135 150 145 133 140 148 149 153 145 149 145 153 158 161 161 145 149 145 153 158 161 161 140 148 149 153 145 149 158 161 161 140 148 149 153 145 149 158 140 148 149 153 145 149
    3 3.46 20 25 27.8 . 137 140 150 142 150 148 150 146 145 132 150 147 148 152 153 142 146 159 158 157 155 154 142 146 159 158 157 155 154 147 148 152 153 142 146 157 155 154 147 148 152 153 142 146 157 147 148 152 153 142 146
    4 15 25.6 30 35.6 . 135 150 154 139 154 150 145 150 151 135 160 155 150 149 150 149 156 159 154 156 160 159 149 156 159 154 156 160 159 155 150 149 150 149 156 156 160 159 155 150 149 150 149 156 156 155 150 149 150 149 156
    5 16 30.1 35.3 40.5 . 135 140 155 146 156 139 150 151 135 145 150 147 148 150 155 156 157 157 158 163 160 165 156 157 157 158 163 160 165 147 148 150 155 156 157 163 160 165 147 148 150 155 156 157 163 147 148 150 155 156 157
    6 10 16.86 20.78 29.8 . 137 151 145 150 143 138 142 145 130 153 136 145 144 149 150 150 156 157 158 159 158 158 150 156 157 158 159 158 158 145 144 149 150 150 156 159 158 158 145 144 149 150 150 156 159 145 144 149 150 150 156
    7 8.98 17.5 25.6 27.9 . 140 152 156 151 141 137 147 144 140 150 130 144 148 150 149 150 156 155 160 160 156 161 150 156 155 160 160 156 161 144 148 150 149 150 156 160 156 161 144 148 150 149 150 156 160 144 148 150 149 150 156
    8 11.23 14.56 20.3 24.456 . 135 140 145 142 142 146 147 150 151 140 146 147 149 151 140 145 150 157 161 162 164 165 145 150 157 161 162 164 165 147 149 151 140 145 150 162 164 165 147 149 151 140 145 150 162 147 149 151 140 145 150

    Hello Stata Community,
    I am currently working on a dataset that contains various actions of resuscitation, such as suctioning and ventilation. I would like to reshape my data from wide to long format, where:
    • The columns represent time counting (elapsed time).
    • The rows represent different actions of resuscitation.
    • I want the start and end points of each action to be displayed below the corresponding time.
    Could anyone guide me on how to achieve this in Stata? Is there a specific command or method I should use to ensure that the start and end points align correctly with the matched time?
    Thank you for your assistance!

  • #2
    Could you use the table function and the data for the first row (IDStudy = 1) and show how you'd like the final data to look like?

    Comment


    • #3
      I agree with Ken Chui that the desired result is not clearly given in #1. But I will go out on a limb and make a guess. I'm guessing that the values in the variables G through BE are the measurements of some variable (?heart rate, but I'll just call it var) at consecutive time points. The variable IDstudy identifies units of observation, and variables ssuction* and esuction* represent parameters that characterize the different units of analysis in some way and apply to all of the time points. I assume that O.P. wants to convert this data into a standard panel data set layout. If I have that right, it is straightforward except for the fact that the variable names G through BE do not lend themselves to parsing by -reshape-. So they must be -rename-d first:
      Code:
      rename (G-BE) var#, addnumber
      reshape long var, i(IDstudy) j(elapsed_time)
      Another possible interpretation is that ssuction* and esuction* represent the start and end times for suction*, and O.P. wants the types of suction and the associated start and end times to become separate variables, not blended into names like ssuction1. And the values in G-BE just "go along for the ride." That also requires some renaming of the variables before -reshape- can apply, and a little bit of "surgery" after:

      Code:
      rename ssuction* time*_start
      rename esuction* time*_end
      
      reshape long time, i(IDstudy) j(_j) string
      split _j, parse(_) gen(v) destring
      order v*, after(IDstudy)
      rename v1 suction_num
      rename v2 event
      drop _j
      gsort IDstudy time -event
      If neither of these is what was wanted, then, as Ken Chui proposed, a table illustrating what the end results should look like is needed.
      Last edited by Clyde Schechter; 05 Aug 2025, 08:26.

      Comment

      Working...
      X