Announcement

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

  • "Count if" with two conditions

    Hi everyone,

    I have cross-sectional loan data from 2007-2010. For each year I have at least 100 observations (loans). Now I would like to count all the loans that have the loan status "default" in year 2007.

    Code:
    count if loan_status=="Default" & Year==2007
    0

    The output is zero, but I know that there must be more than 40.
    Variable "Year" has the format %tdCCYY. Does anyone know what is wrong with my code?

    I found a post in the forum for someone with a similar situation:

    bysort year loan_status : gen count = _n == 1
    by year : replace count = sum(count)
    by year: replace count = _N
    tabdisp year, cell(count)

    However, I do not know if 1) this code fits my problem and 2)how to implement the if-condition (if loan_status==Default). I would be happy if anyone could help me out. Thanks a lot in advance!

  • #2
    Your Year variable is apparently a Stata Internal Format (SIF) daily date - year, month, and day - being displayed with a format that only shows the year. If you were to do the following
    Code:
    generate Day = Year
    format %td Day
    tab Day
    you will see that all the dates now display as something like 1Jan2007 or 31Dec2009.

    Since the months and days are meaningless in your data, you should create a SIF yearly date - which just takes the value of the year, as you expected - and use it in your work, ignoring the current variable.
    Code:
    rename Year Day
    generate Year = yofd(Day)
    Stata's "date and time" variables are complicated - as you have now seen - and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    Comment


    • #3
      William, thank you so much. It is working!
      Nevertheless, I will read the PDF as well.

      Thanks a lot!!

      Comment

      Working...
      X