Announcement

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

  • Help with collapse (sum) command

    I have a dataset, which has a (country_name) variable and a count of (requests) sent from that country to a company both variables are coded as long data.
    My question is how to summarize the values of the (requests) variable for each (country).
    If I use the code:

    collapse (sum) requests, by (country_name)

    the results are wrong.

  • #2
    The command looks right: it will create a dataset that for each country contains the sum of the variable requests. What makes you think that the results are wrong? Maybe you still have missing values that are not coded as missing values?
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank your for your message Maarten.
      I have manually added the values of several countries but the results are inconsistent could this be an issue with the (country_name) variable or the (requests) variable, which I encoded due to them being in the string format before?

      Comment


      • #4
        Using encode to create a numeric country code labelled with the country name was a fine idea.

        Using encode to convert a string variable containing a number to a numeric variable was not a good idea. The output of the help encode command tells us

        Do not use encode if varname contains numbers that merely happen to be stored as strings; instead, use generate newvar = real(varname) or destring; see real() or [D] destring.
        The following example demonstrates the problem.
        Code:
        . * Example generated by -dataex-. To install: ssc install dataex
        . clear
        
        . input str5 requests
        
              requests
          1. "42" 
          2. "666"
          3. end
        
        . destring requests, generate(var1)
        requests: all characters numeric; var1 generated as int
        
        . encode requests, generate(var2)
        
        . list, clean noobs
        
            requests   var1   var2  
                  42     42     42  
                 666    666    666  
        
        . list, clean noobs nolabel
        
            requests   var1   var2  
                  42     42      1  
                 666    666      2  
        
        .

        Comment

        Working...
        X