Announcement

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

  • How to generate a variable using dates in the IF function

    Dear Stata users,

    I'm having trouble generating a new variable in Stata. This is the current command I run:

    gen regulation=.

    replace regulation=0 if DateAnnounced<=9/2/1992 & TargetNation=="AS"

    However, Stata mentions that 0 real changes are made. However, I do know that in my dataset are observations present with the TargetNation being "AS" (Austria) and are announced before september 2nd, 1992 (I found out by running a command where I used my Year variable instead of my DateAnnounced variable. If I look at the variable DateAnnounced, I see in the top box that Stata sees the variable as a date, so I figured Stata would also know if I want observations on that date and before. However, this does not seem to work. I need dates instead of years as I need specific days on which I want to run my regression. Does someone know why Stata doesn't seem to get dates if I use the <= and then put in the desired date? (see screenshot for how the variable looks)

    Thank you in advance!

    Kind regards,
    Danielle



    Click image for larger version

Name:	Stata question.png
Views:	1
Size:	139.6 KB
ID:	1662929

  • #2
    if here is a qualifier, not a function.

    The difficulty is that Stata does not know that you think 9/2/1992 is a date. It thinks you're asking for divisions. You can check how it is thinking directly:

    Code:
    . display 9/2/1992
    .00225904
    
    . display %td  9/2/1992
    01jan1960
    So the syntax is legal and yields a number that in date terms is 1 January 1960, which clearly is not what you want.

    The cardinal rules for daily dates are that

    1. daily dates are held as integers with 0 = 1 January 1960

    2. the display format is something completely different.

    What you want can be got in various ways, the easiest to my mind being

    Code:
    if DateAnnounced<= mdy(9, 2, 1992) 
    with the rest of your syntax looking to be legal so far as I can see.

    As a matter of taste I go for 0, 1 indicators as explained at length in

    https://www.stata.com/support/faqs/d...rue-and-false/

    https://www.stata-journal.com/articl...article=dm0087

    https://www.stata-journal.com/articl...article=dm0099

    You could get such a variable directly with

    Code:
    gen whatever =  DateAnnounced<=mdy(9,2,1992) & TargetNation=="AS"

    Comment


    • #3
      Originally posted by Nick Cox View Post
      if here is a qualifier, not a function.

      The difficulty is that Stata does not know that you think 9/2/1992 is a date. It thinks you're asking for divisions. You can check how it is thinking directly:

      Code:
      . display 9/2/1992
      .00225904
      
      . display %td 9/2/1992
      01jan1960
      So the syntax is legal and yields a number that in date terms is 1 January 1960, which clearly is not what you want.

      The cardinal rules for daily dates are that

      1. daily dates are held as integers with 0 = 1 January 1960

      2. the display format is something completely different.

      What you want can be got in various ways, the easiest to my mind being

      Code:
      if DateAnnounced<= mdy(9, 2, 1992) 
      with the rest of your syntax looking to be legal so far as I can see.

      As a matter of taste I go for 0, 1 indicators as explained at length in

      https://www.stata.com/support/faqs/d...rue-and-false/

      https://www.stata-journal.com/articl...article=dm0087

      https://www.stata-journal.com/articl...article=dm0099

      You could get such a variable directly with

      Code:
      gen whatever = DateAnnounced<=mdy(9,2,1992) & TargetNation=="AS"
      Thank you so much for the fast reply! It worked

      Comment

      Working...
      X