Announcement

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

  • How to insert a blank line by group

    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str1 var1 float group
    "a" 1
    "b" 1
    "c" 1
    "d" 1
    "e" 1
    "a" 2
    "b" 2
    "c" 2
    "e" 2
    "a" 3
    "b" 3
    "c" 3
    "d" 3
    "e" 3
    "a" 4
    "c" 4
    "d" 4
    "e" 4
    end
    [/CODE]
    ------------------ copy up to and including the previous line ------------------
    How to Insert a blank line after each group according to the group?
    insob 1, after(_N) // An empty line can be inserted at the end, but it cannot be grouped

    the data that i wanted are as follows:

    "a" 1
    "b" 1
    "c" 1
    "d" 1
    "e" 1

    "a" 2
    "b" 2
    "c" 2
    "e" 2

    "a" 3
    "b" 3
    "c" 3
    "d" 3
    "e" 3

    "a" 4
    "c" 4
    "d" 4
    "e" 4



  • #2
    Maybe try
    Code:
    drop _all
    
    input str1 var1 float group
    . . .
    end
    *
    * Begin here
    *
    tempfile tmpfil0
    quietly save `tmpfil0'
    
    contract group
    drop _freq
    
    append using `tmpfil0'
    gsort +group -var1
    by group: replace group = . if _n == _N
    
    list, noobs separator(0)
    
    exit

    Comment


    • #3

      ----------------------- copy starting from the next line -----------------------
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str1 var1
      "a"
      "b"
      "b"
      "c"
      "d"
      "e"
      "a"
      "b"
      "c"
      "e"
      "a"
      "b"
      "c"
      "d"
      "e"
      "a"
      "c"
      "d"
      "e"
      "a"
      "b"
      "b"
      "c"
      "d"
      "d"
      "e"
      end
      ------------------ copy up to and including the previous line ------------------

      In another case, I have no grouping, only the variable VAR1. Every time I encounter a specific value or a specific string (such as the letter “e”), I insert a blank line after it. How to implement it? thank you very much


      Comment


      • #4
        Originally posted by fu gang View Post
        I have no grouping, only the variable VAR1. Every time I encounter a specific value or a specific string (such as the letter “e”), I insert a blank line after it. How to implement it?
        Similar, but there might be easier ways.
        Code:
        version 17.0
        
        clear *
        
        input str1 var1
        "a"
        "b"
        "b"
        "c"
        "d"
        "e"
        "a"
        "b"
        "c"
        "e"
        "a"
        "b"
        "c"
        "d"
        "e"
        "a"
        "c"
        "d"
        "e"
        "a"
        "b"
        "b"
        "c"
        "d"
        "d"
        "e"
        end
        
        *
        * Begin here
        *
        generate long row = _n
        
        tempfile tmpfil0
        quietly save `tmpfil0'
        
        quietly keep if var1 == "e"
        quietly expand 2
        quietly bysort row: replace var1 = "" if _n == _N
        
        merge m:1 row using `tmpfil0', assert(match using) nogenerate noreport
        
        gsort +row -var1
        drop row
        
        list, noobs separator(0)
        
        exit

        Comment


        • #5
          Actually, you could do it in-place, too.
          Code:
          *
          * Begin here
          *
          generate long row = _n
          quietly expand 2
          
          quietly bysort row: drop if _n == _N & var1 != "e"
          quietly by row: replace var1 = "" if _n == 2
          drop row
          
          list, noobs separator(0)
          
          exit

          Comment


          • #6
            thank you very much ,it's very king of you
            I need to take some time to understand these procedures

            Comment


            • #7
              Also, take a look at the help files for the commands that are used. For example, I just took a fresh look at the help file for -expand- and saw that it has a couple of options that I didn't remember. Using these options makes the code easier to read and more compact.
              Code:
              *
              * Begin here
              *
              
              generate long row = _n
              
              quietly expand 2 if var1 == "e", generate(erase_me)
              quietly bysort row (erase_me): replace var1 = "" if erase_me
              drop row erase_me
              
              list, noobs separator(0)
              
              exit

              Comment


              • #8
                It is possible to insert blank rows in groups, but there are additional content after the data in the first method, which I don’t need,
                And this method causes the order of the variables to change. I want to insert new rows in groups while keeping the original order.
                Maybe there is a better and more direct way to solve the problem, such as combining other commands with insobs

                Comment


                • #9
                  Here is a somewhat similar approach which may be better suited for your purposes:
                  Code:
                  expand 2 if group!=group[_n-1], gen(expand)
                  sort group expand var1
                  foreach var of varlist _all{
                      replace `var'=`:word `=`="`=substr("`:type `var''",1,3)'"=="str"'+1' of "." `""""'' if expand
                  }
                  drop expand
                  (assumes data is presorted)
                  Last edited by Ali Atia; 19 Aug 2021, 10:27.

                  Comment


                  • #10
                    thank you very much , a very good method,It can Solve the problem well
                    I am going to take some time to understand

                    Comment

                    Working...
                    X