Announcement

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

  • Generating unique ID's for my observations

    Hi everyone,

    I have data of the following type for which I want to generate unique ID's:

    School_ID Number of classrooms in grade 9 Number of classrooms in grade 8
    1101 2 1
    1102 1 0

    I want to generate a variable by the name of Class_ID which should contain an unique ID for each classroom of each grade.

    For example: For School with School_ID 1101 -- I want the class ID for the first classroom of grade 9 to be like 11019A (where in the first four digits are the School_ID, next digit is what grade whether 9 or 8, and last digit is the unique identifier for that classroom denoted by letter A).

    Similarly, for the another classroom in grade 9, the ID should be 11019B.

    Is there a code which can help me to do so?

    Kindly please suggest!

    Thank you very much,

    Rachita


  • #2
    Hi Rachita,

    Based on the two lines of example code, it looks like you only have one line of data per school and multiple variables within a school (i.e. wide data). In order to achieve what you are asking you need to convert your data from wide to long (see Stata command -reshape-). This will allow multiple classrooms within a school with the same school ID, and each classroom will be a unique observation. Is this what you wanted? An example of this would look something like:
    Code:
    schoolID grade classID
    1101 9 11019A
    1101 9 11019B
    1101 8 11018A
    1102 9 11029A

    Comment


    • #3
      Hi Matt,

      Thanks for replying! So what have here is in wide format :-

      School_id grade8 grade9
      1101 1 2
      1102 0 1

      I used -reshape- as you suggested by: reshape long grade, i(School_id) j(class_id)

      So I got this:

      School_id class_id grade
      1101 8 1
      1101 9 2
      1102 8 0
      1102 9 1

      But, this is not what I want. How did you get yours?

      Kindly help.

      Comment


      • #4
        Rachita: Please read and act on FAQ Advice #12 on showing data examples and code. I see no classroom variable in your example, but this is I think similar in spirit to Matt's solution.

        Code:
        clear 
        input School_id grade8 grade9 str1 classroom 
        1101 1 2  A 
        1102 0 1  A 
        end 
        
        reshape long grade, i(School_id) j(Class_id)
        
        egen ID = concat(School_id Class_id classroom) 
        
        list 
        
        
             +-------------------------------------------------+
             | School~d   Class_id   grade   classr~m       ID |
             |-------------------------------------------------|
          1. |     1101          8       1          A   11018A |
          2. |     1101          9       2          A   11019A |
          3. |     1102          8       0          A   11028A |
          4. |     1102          9       1          A   11029A |
             +-------------------------------------------------+

        Comment


        • #5
          If grade defines a count of the number of classrooms, you should delete observations for which grade=0 in #3. In the absence of a classroom variable in Nick's example, #2 from the following link may be useful

          https://www.statalist.org/forums/for...ns-in-alphabet

          Comment

          Working...
          X