Announcement

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

  • "Collect Layout" Sorting in Descending Order

    Hi everyone,

    Can someone please help me sort these rows in descending order? The "collect" commands are difficult for me.
    Click image for larger version

Name:	table.png
Views:	1
Size:	113.0 KB
ID:	1767237



    The code used is
    Code:
    collect clear
    
    dtable 1.(m_bus m_und), ///
    title(Table A1: Academic Majors) ///
    note(note: Students can select more than one major)
    
    quietly collect levelsof colname
    foreach level in `s(levels)'{
        collect label levels var `level' "`:var lab `=substr("`level'", 3, .)''"
    }
    collect layout
    I get the error "input statement exceeds linesize limit. Try specifying fewer variables" even with only 2 variable when trying to use dataex. So I apologize for not providing sample code.

    This is a continuation of the previous thread: https://www.statalist.org/forums/for...iple-variables

    Thank you!

  • #2
    You could specify the order manually, but based on your previous example, here is some code that will automatically generate the sorted order.

    Code:
    clear
    input float(m_fin m_gen m_his m_und)
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 1 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 1 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 1
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    end
    
    label var m_fin   "Finance"
    label var m_gen "Genetics"
    label var m_his   "History"
    label var m_und      "Undecided"
    
    cap frame drop sort
    frame put m_*, into(sort)
    frame sort{
        gen long obsno=_n
        reshape long m_, i(obsno) j(which) string
        collapse (sum) m_, by(which)
        gsort -m_
        local order
        forval i=1/`=_N'{
            local order "`order' 1.m_`=which[`i']'"
        }
    }
    frame drop sort
    
    collect clear
    dtable 1.(m_fin m_gen m_his m_und)
    quietly collect levelsof colname
    foreach level in `s(levels)'{
        collect label levels var `level' "`:var lab `=substr("`level'", 3, .)''"
    }
    collect layout (var[`order']) (result)
    Res.:

    Code:
    . dtable 1.(m_fin m_gen m_his m_und)
    
    --------------------
                Summary
    --------------------
    N                100
    Finance            
      1         7 (7.0%)
    Genetics            
      1         1 (1.0%)
    History            
      1         1 (1.0%)
    Undecided          
      1       14 (14.0%)
    --------------------
    
    .
    . quietly collect levelsof colname
    
    .
    . foreach level in `s(levels)'{
      2.
    .     collect label levels var `level' "`:var lab `=substr("`level'", 3, .)''"
      3.
    . }
    
    .
    . collect layout (var[`order']) (result)
    
    Collection: DTable
          Rows: var[ 1.m_und 1.m_fin 1.m_gen 1.m_his]
       Columns: result
       Table 1: 4 x 1
    
    --------------------
                Summary
    --------------------
    Undecided 14 (14.0%)
    Finance     7 (7.0%)
    Genetics    1 (1.0%)
    History     1 (1.0%)
    --------------------
    
    .

    Comment

    Working...
    X