Announcement

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

  • #16
    Hello everyone, I am having a similar issue with my Stata 17 and the collapse function. I have the 15 Feb 2022 update installed. Any advice or guidance would be most welcome.

    Code:
    collapse (mean) GDP_PC_annualgrowth (mean) GDP_PC_PPP (mean) pop_total (mean) pop_growth (mean) total_laborforce (mean) unemp_nationalest(mean) controlcorruption (mean) ruleoflaw (mean) regulatoryquality (mean) goveffectiveness (mean) politicalstability (mean)voiceaccountability (mean) instquality (mean) SocialInnovationRate,  by(country yrsurv)
    Code:
    variable mean not found
    r(111);
    
    end of do-file
    
    r(111);
    A little on the kind of data I am using here.
    Code:
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    --------------------------------------------------------------------------------------------------------------------
    GDP_PC_annual~h double  %12.0g                
    GDP per capita growth (annual %)
    GDP_PC_PPP      double  %12.0g                
    GDP per capita, PPP (current international $)
    pop_total       long    %12.0g                
    Population, total
    pop_growth      double  %12.0g                
    Population growth (annual %)
    total_laborfo~e double  %12.0g              * 
    Labor force participation rate, total (% of total population ages
                                                    15+) (modeled
    unemp_nationa~t double  %12.0g                
    Unemployment, total (% of total labor force) (national estimate)
    controlcorrup~n double  %12.0g                Control of Corruption: Estimate
    ruleoflaw       double  %12.0g                
    Rule of Law: Estimate
    regulatoryqua~y double  %12.0g                
    Regulatory Quality: Estimate
    goveffectiven~s double  %12.0g                
    Government Effectiveness: Estimate
    politicalstab~y double  %12.0g                
    Political Stability and Absence of Violence/Terrorism: Estimate
    voiceaccounta~y double  %12.0g                
    Voice and Accountability: Estimate
    instquality     float   %9.0g                 
    SocialInnovat~e float   %9.0g                 
    country         int     %22.0g     COUNTRY    country
    yrsurv          int     %8.0g                 Year survey was administered

    Comment


    • #17
      I do not see anything wrong with your code and don't understand why Stata is complaining about it. That said, it is not necessary to repeat (mean) before each variable. You can just write it once and then list all of the variables you want to take means of from there.

      Code:
      collapse (mean) GDP_PC_annualgrowth  GDP_PC_PPP  pop_total  pop_growth  total_laborforce  unemp_nationalest controlcorruption  ruleoflaw  regulatoryquality  goveffectiveness  politicalstability voiceaccountability  instquality  SocialInnovationRate,  by(country yrsurv)
      Perhaps Stata will find this less objectionable, though, again I don't see why it is objecting to what you wrote. I wonder if perhaps this command is inside a loop or a program and the error message is actually coming from some other command where the word mean has been used in a place where a variable name is expected?

      Comment


      • #18
        Correcting
        Code:
        ... (mean) unemp_nationalest(mean) controlcorruption ...
        to
        Code:
        ... (mean) unemp_nationalest (mean) controlcorruption ...
        should solve your problem, which I have reproduced with a similar collapse command, unless there are other cases where you omitted the space between the variable name and the following "(mean)".

        Better yet, follow Clyde's advice and just use (mean) once.

        Comment


        • #19
          Ah, I missed that missing space! Good pickup, William Lisowski!

          Comment


          • #20
            The only thing I have to add to this, is a slight simplification.

            It looks like we're averaging pretty much everything sans the ones we're collapsing on. I'm not at my computer, but if I recall the syntax, collapse is just "collapse (stat) varlist, by(x y z...)".

            So, isn't it possible to do
            Code:
            collapse (mean) ///
            GDP_PC_annualgrowth-Social, ///
            by(country yrsurv)

            Comment


            • #21
              Originally posted by Clyde Schechter View Post
              I do not see anything wrong with your code and don't understand why Stata is complaining about it. That said, it is not necessary to repeat (mean) before each variable. You can just write it once and then list all of the variables you want to take means of from there.

              Code:
              collapse (mean) GDP_PC_annualgrowth GDP_PC_PPP pop_total pop_growth total_laborforce unemp_nationalest controlcorruption ruleoflaw regulatoryquality goveffectiveness politicalstability voiceaccountability instquality SocialInnovationRate, by(country yrsurv)
              Perhaps Stata will find this less objectionable, though, again I don't see why it is objecting to what you wrote. I wonder if perhaps this command is inside a loop or a program and the error message is actually coming from some other command where the word mean has been used in a place where a variable name is expected?
              Thank you this did the trick, for some reason I thought I had to put (mean) before each variable.

              Thank you everyone for your advice, I appreciate it!

              Comment


              • #22
                Hello, I am having a related problem, where Stata/SE 17.0 crashes (closes all windows) after the collapse command. I have updated with the Jan 10, 2023 update. Here is the command:

                collapse (mean) outcome_* index* firstdiagdate , by(patient_id quarters)

                I have ~506 million observations. Perhaps that's the problem? But I ran the exact same code in a similar dataset with ~1.55 billion observations and it worked fine.

                Any ideas? Thanks!

                Comment


                • #23
                  Douglas Barthold , would you please contact Stata tech support at [email protected] with a copy of the dataset whcih casued the crash? Thanks.

                  Before the problem is resolved, you may try

                  Code:
                  collapse_16 (mean) outcome_* index* firstdiagdate , by(patient_id quarters)
                  as a work around.

                  Comment

                  Working...
                  X