Announcement

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

  • how to view a compilation of "labeled" variable values for a single case in one page or screen?

    I have often missed a tool to help me to quickly get a feel for individual cases in a data set. I want to see the values of a number of variables for one single case (e.g. in household survey data). This is a small example (on state level, not household level, in this case):

    . sysuse census, clear
    . list state pop popurban marriage if state=="Idaho"

    +---------------------------------------+
    | state pop popurban marriage |
    |---------------------------------------|
    12. | Idaho 943,935 509,702 13,428 |
    +---------------------------------------+



    displays the values of (three) selected variables of interest for the case "Idaho". However, if the variable names are not as telling as "pop", "popurban", and "marriage" are (but are something cryptic such as V4, V9, and V12), and if many more variables (rather than just the three V4, V9, and V12) are of interest, so that one cannot easliy memorize the meanings of all variables, then the list output is not really helpful.
    one could use
    . table pop if state=="Idaho", stubwidth(40)

    -----------------------------------------------------
    Population | Freq.
    -----------------------------------------+-----------
    943,935 | 1
    -----------------------------------------------------

    . table popurban if state=="Idaho", stubwidth(40)

    -----------------------------------------------------
    Urban population | Freq.
    -----------------------------------------+-----------
    509,702 | 1
    -----------------------------------------------------

    . table marriage if state=="Idaho", stubwidth(40)

    -----------------------------------------------------
    Number of marriages | Freq.
    -----------------------------------------+-----------
    13,428 | 1
    -----------------------------------------------------


    which displays the variable labels (Population, Urban population, and Number of marriages) together with the figures of interest (943,935, etc.) but this requires much code and the output takes up so much space that it does not fit in one screen or page if, e.g. 20 variables are of interest (rather than three in the example).

    Is there any way to produce output, e.g something such as

    -----------------------------------------------------
    state=="Idaho"
    -----------------------------------------------------
    Population | 943,935
    Urban population | 509,702
    Number of marriages | 13,428
    -----------------------------------------------------


    I found it difficult search FAQs etc. for a solution possibly due to a lack of a good idea how to label this probelm...
    could you help?
    Stephan

  • #2
    Welcome to Statalist, Stephan!

    You say that "list output is not really helpful" because of uninformative names A solution is to give all variables informative names or informative labels. Here is one list solution, with automobile make identifying the observation.
    Code:
    set linesize 100
    sysuse auto, clear
    keep in 1/3
    qui levelsof make, local(makes)
    foreach x of local makes {
        di _newline(2)"`x'"
        list mpg-foreign if make=="`x'", ab(40) noobs
    }
    with result:
    Code:
    AMC Concord
    
      +------------------------------------------------------------------------------------------------+
      | mpg   rep78   headroom   trunk   weight   length   turn   displacement   gear_ratio    foreign |
      |------------------------------------------------------------------------------------------------|
      |  22       3        2.5      11    2,930      186     40            121         3.58   Domestic |
      +------------------------------------------------------------------------------------------------+
    
    
    AMC Pacer
    
      +------------------------------------------------------------------------------------------------+
      | mpg   rep78   headroom   trunk   weight   length   turn   displacement   gear_ratio    foreign |
      |------------------------------------------------------------------------------------------------|
      |  17       3        3.0      11    3,350      173     40            258         2.53   Domestic |
      +------------------------------------------------------------------------------------------------+
    
    
    AMC Spirit
    
      +------------------------------------------------------------------------------------------------+
      | mpg   rep78   headroom   trunk   weight   length   turn   displacement   gear_ratio    foreign |
      |------------------------------------------------------------------------------------------------|
      |  22       .        3.0      12    2,640      168     35            121         3.08   Domestic |
      +------------------------------------------------------------------------------------------------+
    I note that you tried to display code and output with a monospace font. As you can see the table headings and content did not line up. The solution is to post all code and output between CODE delimiters, described in FAQ 12, which is essential reading for new Statalist members.
    .
    Last edited by Steve Samuels; 01 Jun 2018, 21:56.
    Steve Samuels
    Statistical Consulting
    [email protected]

    Stata 14.2

    Comment


    • #3
      Here's somethng closer to what you describe.
      Code:
      sysuse auto, clear
      keep in 1/3
      
      tempvar id
      gen `id'=_n
      qui levelsof `id',local(ids)
      
      foreach idx of local ids{
          di _newline make[`idx']
         di as text _dup(60) "-"
          foreach v of varlist mpg-trunk {
              local l`v' : variable label `v'
            if `"`l`v''"' == "" {
                   local l`v' "`v'"
                    }
              di  `"`l`v''"' _col(25) "| " `v'[`idx']
              }
           di as text  _dup(60) "-"
          }
      with results
      Code:
      AMC Concord
      ------------------------------------------------------------
      Mileage (mpg)           | 22
      Repair Record 1978      | 3
      Headroom (in.)          | 2.5
      Trunk space (cu. ft.)   | 11
      ------------------------------------------------------------
      
      AMC Pacer
      ------------------------------------------------------------
      Mileage (mpg)           | 17
      Repair Record 1978      | 3
      Headroom (in.)          | 3
      Trunk space (cu. ft.)   | 11
      ------------------------------------------------------------
      
      AMC Spirit
      ------------------------------------------------------------
      Mileage (mpg)           | 22
      Repair Record 1978      | .
      Headroom (in.)          | 3
      Trunk space (cu. ft.)   | 12
      ------------------------------------------------------------
      Last edited by Steve Samuels; 02 Jun 2018, 12:44. Reason: Used temporary ID
      Steve Samuels
      Statistical Consulting
      [email protected]

      Stata 14.2

      Comment


      • #4
        Here is a version that displays all variables except the ID variable (make)
        Code:
        sysuse auto, clear
        keep in 1/3
        
        local idvar "make"  // ID variable
        /* variables to show */
        local vlist  "*"   // all variables
        unab vv1: `vlist'
        local vv2: list vv1-idvar
        
        tempvar id2
        gen `id2'=_n
        levelsof `id2', local(id2s)
        
        foreach idx of local id2s{
            di _newline make[`idx']
           di as text _dup(60) "-"
            foreach v of local vv2 {
                local l`v' : variable label `v'
              if `"`l`v''"' == "" {
                     local l`v' "`v'"
                      }
                di  `"`l`v''"' _col(25) "| " `v'[`id2']
                }
             di as text  _dup(60) "-"
            }
        with results
        Code:
        AMC Concord
        ------------------------------------------------------------
        Price                   | 4099
        Mileage (mpg)           | 22
        Repair Record 1978      | 3
        Headroom (in.)          | 2.5
        Trunk space (cu. ft.)   | 11
        Weight (lbs.)           | 2930
        Length (in.)            | 186
        Turn Circle (ft.)       | 40
        Displacement (cu. in.)  | 121
        Gear Ratio              | 3.5799999
        Car type                | 0
        ------------------------------------------------------------
        
        AMC Pacer
        ------------------------------------------------------------
        Price                   | 4099
        Mileage (mpg)           | 22
        Repair Record 1978      | 3
        Headroom (in.)          | 2.5
        Trunk space (cu. ft.)   | 11
        Weight (lbs.)           | 2930
        Length (in.)            | 186
        Turn Circle (ft.)       | 40
        Displacement (cu. in.)  | 121
        Gear Ratio              | 3.5799999
        Car type                | 0
        ------------------------------------------------------------
        
        AMC Spirit
        ------------------------------------------------------------
        Price                   | 4099
        Mileage (mpg)           | 22
        Repair Record 1978      | 3
        Headroom (in.)          | 2.5
        Trunk space (cu. ft.)   | 11
        Weight (lbs.)           | 2930
        Length (in.)            | 186
        Turn Circle (ft.)       | 40
        Displacement (cu. in.)  | 121
        Gear Ratio              | 3.5799999
        Car type                | 0
        ------------------------------------------------------------
        Last edited by Steve Samuels; 02 Jun 2018, 13:21. Reason: removed diagnostic display
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment


        • #5
          Dear Steve, thank you for considering my question so thoroughly. Particularly your June 2 responses are very helpful and provide a solution that is very valuable for me (as I hesitate to change cryptic variable names to iformative ones in a data set which is used by several colleagues). Your solution even inspired me to extend it in a way that I had not thought of in my original question: For variables that come with value labels I want these value labels displayed rather than the numerical variable values.
          The following code considers also this issue:


          Code:
          sysuse auto, clear
          keep in 52/54
          
          tempvar id
          gen `id'=_n
          qui levelsof `id',local(ids)
          
          describe
          
          
          foreach idx of local ids{
              di _newline make[`idx']
             di as text _dup(60) "-"
              foreach v of varlist displacement-foreign {
                  local l`v' : variable label `v'
                  local w`v' : value label `v'    /* name of value label */
                  local v1 = `v'[`idx']           /* numerical value of the variable */
                if `"`l`v''"' == "" {
                       local l`v' "`v'"
                        }
                if "`w`v''" == "" {
                  di  `"`l`v''"' _col(25) "| " `v'[`idx']
                }
                if "`w`v''" != ""   {
                  local     v2 : label `w`v'' `v1'  /* value label of the variable */
                  di  `"`l`v''"' _col(25) "| "  "`v2' "
                }  
                }
               di as text  _dup(60) "-"
              }

          In this example the value labels for the variable 'foreign' are used:
          Code:
          Pont. Sunbird
          ------------------------------------------------------------
          Displacement (cu. in.)  | 151
          Gear Ratio              | 2.73
          Car type                | Domestic
          ------------------------------------------------------------
          
          Audi 5000
          ------------------------------------------------------------
          Displacement (cu. in.)  | 131
          Gear Ratio              | 3.2
          Car type                | Foreign
          ------------------------------------------------------------
          
          Audi Fox
          ------------------------------------------------------------
          Displacement (cu. in.)  | 97
          Gear Ratio              | 3.7
          Car type                | Foreign
          ------------------------------------------------------------
          so again, thank you very much, Steve!
          Stephan

          Comment


          • #6
            Very nice, Stephan!
            Steve Samuels
            Statistical Consulting
            [email protected]

            Stata 14.2

            Comment

            Working...
            X