Announcement

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

  • how to add more rows for a unique id

    childid session id
    1 UP/GBN/RU/3I/KHP/KHZ/CLC/1
    2 UP/GBN/RU/3I/KHP/KHZ/CLC/1
    3 UP/GBN/RU/3I/KHP/KHZ/CLC/1
    4 UP/GBN/RU/3I/KHP/KHZ/CLC/1
    5 UP/GBN/RU/3I/KHP/KHZ/CLC/1
    1 UP/GBN/RU/3I/KSN/KNB/CLC/3
    2 UP/GBN/RU/3I/KSN/KNB/CLC/3
    3 UP/GBN/RU/3I/KSN/KNB/CLC/3

    In case total number of rows should be same for both the sessions ids viz, UP/GBN/RU/3I/KHP/KHZ/CLC/1 and UP/GBN/RU/3I/KSN/KNB/CLC/3, lets say it should be 10. For first session id, 5 students are present and 10-5=5 are absent and I would to include five more rows with the same session id and blank childids.

    Similarly, for second session id (UP/GBN/RU/3I/KSN/KNB/CLC/3), 3 are present and 10-3 =7 are absent and I want to include 7 more rows with the second session id and blank child ids.

    What is the stata command for that, Please help?



  • #2
    Cchavi:
    see -expand-.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte childid str26 sessionid
      1 "UP/GBN/RU/3I/KHP/KHZ/CLC/1"
      2 "UP/GBN/RU/3I/KHP/KHZ/CLC/1"
      3 "UP/GBN/RU/3I/KHP/KHZ/CLC/1"
      4 "UP/GBN/RU/3I/KHP/KHZ/CLC/1"
      5 "UP/GBN/RU/3I/KHP/KHZ/CLC/1"
      1 "UP/GBN/RU/3I/KSN/KNB/CLC/3"
      2 "UP/GBN/RU/3I/KSN/KNB/CLC/3"
      3 "UP/GBN/RU/3I/KSN/KNB/CLC/3"
      end
      sort sessionid childid
      generate copies = 1
      by sessionid: replace copies = 11-_N if _n==_N
      list, clean
      expand copies, generate(new)
      replace childid = . if new==1
      sort sessionid childid
      drop copies new
      list, clean
      Code:
      . sort sessionid childid
      
      . generate copies = 1
      
      . by sessionid: replace copies = 11-_N if _n==_N
      (2 real changes made)
      
      . list, clean
      
             childid                    sessionid   copies  
        1.         1   UP/GBN/RU/3I/KHP/KHZ/CLC/1        1  
        2.         2   UP/GBN/RU/3I/KHP/KHZ/CLC/1        1  
        3.         3   UP/GBN/RU/3I/KHP/KHZ/CLC/1        1  
        4.         4   UP/GBN/RU/3I/KHP/KHZ/CLC/1        1  
        5.         5   UP/GBN/RU/3I/KHP/KHZ/CLC/1        6  
        6.         1   UP/GBN/RU/3I/KSN/KNB/CLC/3        1  
        7.         2   UP/GBN/RU/3I/KSN/KNB/CLC/3        1  
        8.         3   UP/GBN/RU/3I/KSN/KNB/CLC/3        8  
      
      . expand copies, generate(new)
      (12 observations created)
      
      . replace childid = . if new==1
      (12 real changes made, 12 to missing)
      
      . sort sessionid childid
      
      . drop copies new
      
      . list, clean
      
             childid                    sessionid  
        1.         1   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
        2.         2   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
        3.         3   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
        4.         4   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
        5.         5   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
        6.         .   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
        7.         .   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
        8.         .   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
        9.         .   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
       10.         .   UP/GBN/RU/3I/KHP/KHZ/CLC/1  
       11.         1   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       12.         2   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       13.         3   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       14.         .   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       15.         .   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       16.         .   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       17.         .   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       18.         .   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       19.         .   UP/GBN/RU/3I/KSN/KNB/CLC/3  
       20.         .   UP/GBN/RU/3I/KSN/KNB/CLC/3  
      
      .
      Note that because you did follow the instructions in the FAQ (to which you were referred in your earlier post on this problem) for presenting your example data using the dataex command, this example code will very likely need modification to work with your actual data.

      Comment


      • #4


        I tried the above syntax, it is working but it is creating null values for my other important variables.

        Comment


        • #5
          Thanks @William Lisowski . I have been able to solve this situation.

          Comment

          Working...
          X