Announcement

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

  • Collapse over all value labels

    Hi,

    I want to save a dataset containing the frequencies of various variables for all possible value labels, including those that are not included in the original dataset (see below the current and desired output). Any suggestions?

    Thanks!

    Code:
    clear all
    input id country
    1 1
    2 1
    3 1
    4 2
    5 3
    6 4
    7 2
    8 4
    9 2
    10 3
    end
    forvalues i=1/5 {
        la def country `i' "Country `i'", modify
    }
    la val country country
    labelbook country
    
    gen one=1
    collapse (sum) one, by(country)
    list
    
    /*
         +-----------------+
         |   country   one |
         |-----------------|
      1. | Country 1     3 |
      2. | Country 2     3 |
      3. | Country 3     2 |
      4. | Country 4     2 |
         +-----------------+
        
    Ideal output:
         +-----------------+
         |   country   one |
         |-----------------|
      1. | Country 1     3 |
      2. | Country 2     3 |
      3. | Country 3     2 |
      4. | Country 4     2 |
      5. | Country 5     0 |
         +-----------------+
    
    */

  • #2
    You can program it, but what do you intend to do with the output? If you are simply after tabulations, see this recent thread for some community-contributed commands in this area: https://www.statalist.org/forums/for...-to-a-variable

    Comment


    • #3
      Thanks for this Andrew Musau. The data above is just for illustration, I'm using survey data and I'd like to export these datasets on Excel to show which response options were selected, including those that were not selected. Unfortunately, there is no way to extract information from "labelbook variable" or from "describe variable" to extract all possible value labels and a variable's value label, respectively.

      Comment


      • #4
        Hmm... if the labels are sequential this may work:

        Code:
        clear all
        input id country
        1 1
        2 1
        3 1
        4 2
        5 3
        6 4
        7 2
        8 4
        9 2
        10 3
        end
        forvalues i=1/5 {
            la def country `i' "Country `i'", modify
        }
        la val country country
        labelbook country
        
        gen one=1
        
        label list country
        forvalues nc = `r(min)'(1)`r(max)' {
            local newrow = _N + 1
            set obs `newrow'
            replace country = `nc' in `newrow'
        }
        
        collapse (sum) one, by(country)
        list
        Results:

        Code:
             +-----------------+
             |   country   one |
             |-----------------|
          1. | Country 1     3 |
          2. | Country 2     3 |
          3. | Country 3     2 |
          4. | Country 4     2 |
          5. | Country 5     0 |
             +-----------------+

        Comment


        • #5
          Code:
          lab list
          will list names and contents of value labels, and as I pointed out, the linked thread shows you commands that will enable you to export tabulations that include absent categories. You need to tabulate these one by one and append them to a CSV file, which can subsequently be opened in Excel. However, I do not know if svy will complicate things with the community-contributed commands.

          Comment

          Working...
          X