Announcement

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

  • Control order of vars from tab output

    Hi all, while trying to generate some frequencies and percentages with -tab-, it seems like Stata's default is to provide a table where the vars are ordered alphabetically, even if I have it sorted the way I'd like it ahead of time. Is there any way to get it to use a custom sorted order within the -tab- command?

    Code:
    gen order = .
    replace order = 1 if group == "yes"
    replace order = 2 if group == "no"
    replace order = 3 if group == "maybe"
    replace order = 4 if group == "test"
    replace order = 5 if group == "not_test"
    replace order = 6 if group == "local"
    replace order = 7 if group == "external"
    
    sort order
    tab group fail_by50 [fw=frequency], row

  • #2
    You need to define a value label that orders your string values the way you want them. Then -encode- the group variable with that label. You didn't provide example data, so here's a toy data set to illustrate the approach:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8 group
    "external"
    "external"
    "local"   
    "local"   
    "local"   
    "local"   
    "maybe"   
    "maybe"   
    "maybe"   
    "no"      
    "no"      
    "no"      
    "no"      
    "not_test"
    "not_test"
    "not_test"
    "not_test"
    "not_test"
    "not_test"
    "not_test"
    "not_test"
    "not_test"
    "test"    
    "test"    
    "test"    
    "yes"     
    "yes"     
    end
    
    
    sort group
    tab group // TABLE SORTED ALPHABETICALLY
    
    label define order    1    "yes"    ///
                        2    "no"    ///
                        3    "maybe"    ///
                        4    "test"    ///
                        5    "not_test"    ///
                        6    "local"    ///
                        7    "external"
    encode group, label(order) gen(order)
    tab order // TABLE SORTED IN DESIGNATED ORDER
    In the future, when asking for help with code, please use the -dataex- command and show example data. Although sometimes, as here, it is possible to give an answer that has a reasonable probability of being correct, this is usually not the case. Moreover, such answers are necessarily based on experience-based guesses or intuitions about the nature of your data. When those guesses are wrong, both you and the person trying to help you have wasted their time as you end up with useless code. To avoid this, a -dataex- based example provides all of the information needed to develop and test a solution.

    If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.


    Last edited by Clyde Schechter; 03 Jul 2023, 11:00.

    Comment


    • #3
      To make @Clyde Schechter's points differently:

      Your variable group is one (and only one) string variable, so by default its distinct values will indeed be listed alphabetically by tabulate.

      The sort order of the dataset doesn't affect that.

      You could override that default with the sort option of tabulate which orders the values by frequency. That again is nothing to do with the sort order of the dataset. The two are separate matters.

      To get a different desired order in a table, you need first to define value labels and encode, as Clyde explained.

      Whether yes, no and maybe is the right order is an interesting detail. The classification seems to mix criteria.

      Comment

      Working...
      X