Announcement

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

  • Collapsing observations in different rows

    Hi everybody

    I have some data this way:
    SP_E_CONSECUTIVO SP_Y_ESP_ENSEÑAR SP_Y_ESP_EVALUAR SP_Y_ESP_FORMAR
    EK201630003018 168
    EK201630003018 150
    EK201630003018 202
    EK201630003986 183
    EK201630003986 192
    EK201630003986 182
    EK201630004999 173
    EK201630004999 177
    EK201630004999 190
    EK201630003798 141
    EK201630003798 150
    EK201630003798 135
    EK201630003823 212
    EK201630003823 181
    EK201630003823 188
    EK201630003906 213
    EK201630003906 194
    EK201630003906 200
    But I want to collapse the repeated observations in one row only, like this:
    SP_E_CONSECUTIVO SP_Y_ESP_ENSEÑAR SP_Y_ESP_EVALUAR SP_Y_ESP_FORMAR
    EK201630003018 168 150 202
    EK201630003986 183 192 182
    EK201630004999 173 177 190
    EK201630003798 141 150 135
    EK201630003823 212 181 188
    EK201630003906 213 194 200
    I couldn't find a quick method for doing this. Can somebody give me some advice? I'll appreciate it a lot.

  • #2
    It is, in fact, precisely the -collapse- command that you need:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str15 sp_e_consecutivo int(SP_Y_ESP_ENSEÑAR sp_y_esp_evaluar sp_y_esp_formar)
    "EK201630003018 " 168   .   .
    "EK201630003018 "   . 150   .
    "EK201630003018 "   .   . 202
    "EK201630003986 " 183   .   .
    "EK201630003986 "   . 192   .
    "EK201630003986 "   .   . 182
    "EK201630004999 " 173   .   .
    "EK201630004999 "   . 177   .
    "EK201630004999 "   .   . 190
    "EK201630003798 " 141   .   .
    "EK201630003798 "   . 150   .
    "EK201630003798 "   .   . 135
    "EK201630003823 " 212   .   .
    "EK201630003823 "   . 181   .
    "EK201630003823 "   .   . 188
    "EK201630003906 " 213   .   .
    "EK201630003906 "   . 194   .
    "EK201630003906 "   .   . 200
    end
    
    
    collapse (firstnm) SP_Y_ESP_ENSEÑAR sp_y_esp_evaluar sp_y_esp_formar, by(sp_e_consecutivo)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. 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 dat

    Comment

    Working...
    X