Announcement

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

  • Inserting blank rows at specific observations in dataset

    I am trying to manipulate a text (.tex) document (a multiple-choice exam) in Stata, which I realize is a bit strange. I have copied the text to the data editor so that each line is an observation for the variable v1. I have found Stata quite handy for text manipulation, but I am stuck on one issue: I need to duplicate specific blocks of four observations (the MCQ choices) and insert them directly below the original, so that I can manipulate the correct answer. To do this, I thought to start by inserting four blank rows after each question, and then doing a replace. However, I haven't found a way to do this, including through the ingap command.

    It is possible to identify each MCQ using strmatch because each question-line begins with "\mc", and likewise the block of choices ends with the same value "\end{MC}". Here is a snippet of the data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str145 v1
    "%Label1: "                                                                                                                                      
    "\mc The production possibility curve (PPC) represents the \_\_\_\_\_\_\_\_ combinations of goods that can be produced with available resources."
    "\begin{MC}"                                                                                                                                     
    "\item minimum attainable"                                                                                                                       
    "\item maximum attainable"                                                                                                              
    "\item only"                                                                                                                                     
    "\item equitable"                                                                                                                                
    "\end{MC}"                                                                                                                                       
    "%Label2: "                                                                                                                                      
    "\mc A point on the production possibility curve (PPC) is \_\_\_\_ if it is outside of the curve."                                     
    "\begin{MC}"                                                                                                                                     
    "\item attainable and efficient"                                                                                                                 
    "\item attainable and inefficient"                                                                                                               
    "\item unattainable"                                                                                                                             
    "\item \textbf{None of the above}"                                                                                                               
    "\end{MC}"                                                                                                                                       
    end
    I would like to manipulate the data so that it would now be:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str145 v1
    "%Label1: "                                                                                                                                      
    "\mc The production possibility curve (PPC) represents the \_\_\_\_\_\_\_\_ combinations of goods that can be produced with available resources."
    "\begin{MC}"                                                                                                                                     
    "\item minimum attainable"                                                                                                                       
    "\item \textbf{maximum attainable}"                                                                                                              
    "\item only"                                                                                                                                     
    "\item equitable"                                                                                                                                
    "\end{MC}"    
    "\begin{MC}"                                                                                                                                     
    "\item minimum attainable"                                                                                                                       
    "\item \textbf{maximum attainable}"                                                                                                              
    "\item only"                                                                                                                                     
    "\item equitable"                                                                                                                                
    "\end{MC}"                                                                                                                                              
    "%Label2: "                                                                                                                                      
    "\mc A point on the production possibility curve (PPC) is \_\_\_\_\_\_\_\_\_ if it is outside of the curve."                                     
    "\begin{MC}"                                                                                                                                     
    "\item attainable and efficient"                                                                                                                 
    "\item attainable and inefficient"                                                                                                               
    "\item unattainable"                                                                                                                             
    "\item \textbf{None of the above}"                                                                                                               
    "\end{MC}"                                
    "\begin{MC}"                                                                                                                                     
    "\item attainable and efficient"                                                                                                                 
    "\item attainable and inefficient"                                                                                                               
    "\item unattainable"                                                                                                                             
    "\item \textbf{None of the above}"                                                                                                               
    "\end{MC}"                                                                                                                                         
    end

  • #2
    To do this, I thought to start by inserting four blank rows after each question, and then doing a replace.
    I am not sure I get what you want to achieve, but here is a way to create 4 blank observations (not rows!) after each block.

    Code:
    gen id=_n
    gen toexpand=cond(strpos(v1,"\end{MC}")& v1[_n+1]!="" , 5,1)
    expand toexpand, gen(new)
    sort id new
    replace v1= "" if new
    Res.:

    Code:
    . l v1, sep(1)
    
         +------------------------------------------------------------------------------------------------------------------------------------+
         | v1                                                                                                                                 |
         |------------------------------------------------------------------------------------------------------------------------------------|
      1. | %Label1:                                                                                                                           |
         |------------------------------------------------------------------------------------------------------------------------------------|
      2. | \mc The production possibility curve (PPC) represents the \_\_\_\_\_\_\_\_ combinations of goods that can be produced with avail.. |
         |------------------------------------------------------------------------------------------------------------------------------------|
      3. | \begin{MC}                                                                                                                         |
         |------------------------------------------------------------------------------------------------------------------------------------|
      4. | \item minimum attainable                                                                                                           |
         |------------------------------------------------------------------------------------------------------------------------------------|
      5. | \item maximum attainable                                                                                                           |
         |------------------------------------------------------------------------------------------------------------------------------------|
      6. | \item only                                                                                                                         |
         |------------------------------------------------------------------------------------------------------------------------------------|
      7. | \item equitable                                                                                                                    |
         |------------------------------------------------------------------------------------------------------------------------------------|
      8. | \end{MC}                                                                                                                           |
         |------------------------------------------------------------------------------------------------------------------------------------|
      9. |                                                                                                                                    |
         |------------------------------------------------------------------------------------------------------------------------------------|
     10. |                                                                                                                                    |
         |------------------------------------------------------------------------------------------------------------------------------------|
     11. |                                                                                                                                    |
         |------------------------------------------------------------------------------------------------------------------------------------|
     12. |                                                                                                                                    |
         |------------------------------------------------------------------------------------------------------------------------------------|
     13. | %Label2:                                                                                                                           |
         |------------------------------------------------------------------------------------------------------------------------------------|
     14. | \mc A point on the production possibility curve (PPC) is \_\_\_\_ if it is outside of the curve.                                   |
         |------------------------------------------------------------------------------------------------------------------------------------|
     15. | \begin{MC}                                                                                                                         |
         |------------------------------------------------------------------------------------------------------------------------------------|
     16. | \item attainable and efficient                                                                                                     |
         |------------------------------------------------------------------------------------------------------------------------------------|
     17. | \item attainable and inefficient                                                                                                   |
         |------------------------------------------------------------------------------------------------------------------------------------|
     18. | \item unattainable                                                                                                                 |
         |------------------------------------------------------------------------------------------------------------------------------------|
     19. | \item \textbf{None of the above}                                                                                                   |
         |------------------------------------------------------------------------------------------------------------------------------------|
     20. | \end{MC}                                                                                                                           |
         +------------------------------------------------------------------------------------------------------------------------------------+

    Comment


    • #3
      I recently learned about the built-in -insobs- (insert observations) command, which might also be useful here.

      Comment


      • #4
        The command -insobs- seems to be introduced from Stata 14. Type -which insobs- get the following (part):
        Code:
        *! version 1.0.2  10mar2015

        Comment


        • #5
          -insobs- didn't seem to work for my (admittedly rare) purposes, which is to insert observations depending on matching a string. But Andrew's code works perfectly - thanks!

          Comment

          Working...
          X