Announcement

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

  • count

    Hello everyone,

    I need to count the number of times a condition was satisfied. I have got different variables which are gdp quarterly levels, Q12008-Q42015.
    I want to know how many quatrimesters the gdp was below the initial level (varname maxval_crisis).

    I am using the following command:

    gen count = 0
    gen sum = 0
    forval j = Q12008/Q42015 {
    replace count = count + ( var `j' < maxval_crisis )
    replace sum = sum + var `j' if var `j' < maxval_crisis
    }

    data are attached. Thanks for your help.

    Best,
    Attached Files

  • #2
    Spreadsheet attachments are specifically advised against. Would you post a Stata dataset to an Excel forum?

    Please go back and read and act on https://www.statalist.org/forums/help#stata 12.5 It explains precisely why spreadsheets are much less useful than you think. The shared software on Statalist is Stata, certainly not MS Excel.

    Comment


    • #3
      When you review the FAQ advice that Nick pointed out, please also review the rest of the Statalist FAQ, noting especially sections 9-12 on how to best pose your question.

      Section 12.1 is particularly pertinent

      12.1 What to say about your commands and your problem

      Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
      ...
      Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
      The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

      Looking at your code, but not your data, there are three places where you apparently invent what you think code might be, without actually reading and understanding the help files to see if it is correct.

      1) You apparently have a list of variables Q12008 Q22008 ... Q42015. Your forvalues loop is appropriate for a list of numbers, not a list of variable names. For that, you need the syntax for a varlist shown in help foreach.
      Code:
      foreach v of varlist Q12008-Q42015
      2) What on earth does var `j' mean in your two replace commands? That's completely invented syntax. With the foreach loop above,
      Code:
      replace count = count + (`v' < maxval_crisis )
      replace sum = sum + `v' if `v' < maxval_crisis
      My samples of code may or may not be correct, because I have not tested them on your data.

      Comment


      • #4
        If you insist on using a forvalues loop then this code should do the trick (without having tested on your data that is)
        Code:
        forvalues year=2008/2015{
            forvalues quarter=1/4{
              replace count = count + (Q`quarter'`year'< maxval_crisis )
              replace sum = sum + Q`quarter'`year' if Q`quarter'`year' < maxval_crisis
            }
        }
        This approach is not necessarily better than the foreach loop by William Lisowski. It's just another way of writing the same problem.

        Comment


        • #5
          Thanks for your help. Both codes work well.

          Comment

          Working...
          X