Announcement

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

  • Dummy Variable and Date

    I have a dataset that has quit date in %td (DMY) format. I also have two other variables that provide information about use of nicotine patch (no, yes), and the blood pressure levels at 6 months of of quit date. I need to generate a new indicator variable that takes on a value of 1 if the quit date was before a certain date, use of nicotine patch was yes and blood pressure was above 120, and 0 otherwise.

    I am trying this:

    gen newvar = .
    replace newvar = 1 if birthdate<DMY & nicpatch==1 & bp >120 //nicpatch yes/no was coded as 1/0

    However, I am having an issue and getting an error message as:

    01apr2012 invalid name
    r(198);

    How can this problem be tackled?

    Thanks much.


  • #2
    You say your command was
    Code:
    replace newvar = 1 if birthdate<DMY & nicpatch==1 & bp >120
    but the error message suggests you actually typed
    Code:
    replace newvar = 1 if birthdate<01apr2012 & nicpatch==1 & bp >120
    That is not the correct way of giving a date constant. Instead you want
    Code:
    replace newvar = 1 if birthdate<td(01apr2012) & nicpatch==1 & bp >120
    to translate 01apr2012 into a daily date value.

    I need to generate a new indicator variable that takes on a value of 1 if the ... and 0 otherwise.
    In your code, for observations that do not meet the conditions newvar will take on the value missing rather than 0. You want
    Code:
    generate newvar = 0
    replace newvar = 1 if birthdate<td(01apr2012) & nicpatch==1 & bp >120
    Or you can do
    Code:
    generate newvar = birthdate<td(01apr2012) & nicpatch==1 & bp >120
    because logical expressions evaluate to 1 if they are true and 0 if they are false.

    The td() function is documented in the output of help datetime. Stata's "date and time" variables are complicated 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 Lisowski gives excellent advice as always. Further, echoing advice already given to you,

      Don't paraphrase or retype code that worked: copy and paste exactly what worked -- or what didn't work.

      Comment


      • #4
        Thanks much William and Nick; this was exactly what I was looking for. Works wonder. Nick, I will make sure that I post any codes in the correct manner from next time.

        Comment

        Working...
        X