Announcement

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

  • How to create sequence number for ID-data pairs within each ID?

    Hello,

    A PhD student posting for the first time here. I'm having a problem with creating a sequence number for ID-data pairs within each ID. Please see the following illustration.
    ID Semester Code Course Semester #
    1 88 A 1
    1 88 B 1
    1 99 C 2
    2 77 D 1
    2 88 A 2
    2 88 E 2
    2 99 F 3
    3 77 D 1
    3 88 G 2
    I want to create Semester # for each student. For example, student with ID = 1 studied two semesters: 88 and 99, so I want to mark pairs (1,88) and (1,99) as his first and second semester, respectively. Same goes for other students.

    Although I understand that I can use levelsof ID and create loops, it doesn't seem like a good way to do it. Is there any better way to do this? Thank you very much.



    Best regards,

    Don Tawanpitak

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id semestercode) str2 course
    1 88 "A "
    1 88 "B "
    1 99 "C "
    2 77 "D "
    2 88 "A "
    2 88 "E "
    2 99 "F "
    3 77 "D "
    3 88 "G "
    end
    
    by id (semestercode), sort: gen semester_num = sum(semestercode != semestercode[_n-1])
    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 data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Added: In general, when you get the urge to use loops in Stata, stop and ask yourself whether the same thing can be done with -by:-. The answer is often yes, and in that circumstance code with -by- is both much more efficient (very noticeably so in large data sets) and more transparent.


    Comment


    • #3
      Clyde Schechter Thank you very much!

      Originally posted by Clyde Schechter View Post
      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 data.

      When asking for help with code, always show example data. When showing example data, always use -dataex-.

      Added: In general, when you get the urge to use loops in Stata, stop and ask yourself whether the same thing can be done with -by:-. The answer is often yes, and in that circumstance code with -by- is both much more efficient (very noticeably so in large data sets) and more transparent.
      I will. Thank you for your suggestion.

      Comment

      Working...
      X