Announcement

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

  • create dummy variable from date: %tcnn/dd/ccYY_hh:MM

    Hello, I tried searching for similar questions, but wasn't able to get anything to work. I want to create a dummy variable just based on the year in the date variable (format: %tcnn/dd/ccYY_hh:MM). Can anyone help me with the code?

    So far I have tried:

    Code:
    generate sample_1 = .
    replace sample_1 = 1 if Date<td(12/05/1995)
    Thank you in advance.

  • #2
    your variable is of type "tc" but you used "td" in your expression - that won't work

    a second problem is that you are setting up a 1/. variable which is almost certainly not what you really want

    please read the FAQ and follow it's advice in re-posting so that someone can, maybe, provide exact code

    Comment


    • #3
      Code:
      gen wanted = dofc(date) < td(12/05/1995)

      Comment


      • #4
        Øyvind Snilsberg Thank you for your help! Indeed, the 'td' didn't work, but the code doesn't work with "tc" variable type either, unfortunately.
        Last edited by Julie Well; 28 May 2022, 15:42.

        Comment


        • #5
          Thank you Rich for your reply and patience with a novice! I read through the FAQ again, can you point me to what part you think I should review? I'm new and not sure what I am doing wrong in my post. I appreciate your comments about variable type ("tc") and approach. If you are so inclined, I would appreciate any further help.

          Comment


          • #6
            read esp. section 12 and the material on -dataex-

            Comment


            • #7
              In #1 your daily date is nearly 13000 days after 1 January 1960 and as a date-time the same number is nearly 13000 ms (nearly 13 seconds) after midnight on 1 January 1960

              Code:
              . di td(12/05/1995)
              12915
              
              . di %tc  td(12/05/1995)
              01jan1960 00:00:12
              which is not what you want. You're expecting Stata to be like a smart research assistant who knows what you mean, but it can only take account of what you say. You must translate one way or the other.

              This should explain why your code was perfectly legal, just wrong from your point of view.

              Comment

              Working...
              X