Announcement

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

  • Halfyearly

    Hello guys!

    I'm working with halfyearly data and I'm trying to use conditionals, however it appears and error. The format of the data is %th.

    This is what I'm trying to do:

    Code:
        gen y1h1=1 if hdate==2016h1
        replace y1h1=0 if hdate!=2016h1
        
        gen y1h2=1 if hdate==2016h2
        replace y1h2=0 if hdate!=2016h2
    
        gen y2h1=1 if hdate==2017h1
        replace y2h1=0 if hdate!=2017h1
        
        gen y2h2=1 if hdate==2017h2
        replace y2h2=0 if hdate!=2017h2
    
        gen y3h1=1 if hdate==2018h1
        replace y3h1=0 if hdate!=2018h1
        
        gen y3h2=1 if hdate==2018h2
        replace y3h2=0 if hdate!=2018h2
    
        gen y4h1=1 if hdate==2019h1
        replace y4h1=0 if hdate!=2019h1
        
        gen y4h2=1 if hdate==2019h2
        replace y4h2=0 if hdate!=2019h2
    After the first line it appears this error:

    Code:
    .         gen y1h1=1 if hdate==2016h1
    2016h1 invalid name
    r(198);
    I would really appreciate your help.

  • #2
    Replace
    Code:
    gen y1h1=1 if hdate==2016h1
    replace y1h1=0 if hdate!=2016h1
    with
    Code:
    gen y1h1 = hdate==th(2016h1)
    because the comparison will evaluate to 1 if the == is true and to 0 it is not true.

    For a discussion of using datetime values in expressions (like the use of the th() function above) see the output of
    Code:
    help datetime##s9
    More generally, 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 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 a footnote, if the goal is to get a bundle of indicator (some say dummy) variables for distinct dates, then


      Code:
      tab hdate, gen(hdate)
      will get you there directly. In turn, such a bundle may be redundant within modelling commands as you can use factor variable notation;,

      Comment


      • #4
        Originally posted by William Lisowski View Post
        Replace
        Code:
        gen y1h1=1 if hdate==2016h1
        replace y1h1=0 if hdate!=2016h1
        with
        Code:
        gen y1h1 = hdate==th(2016h1)
        because the comparison will evaluate to 1 if the == is true and to 0 it is not true.

        For a discussion of using datetime values in expressions (like the use of the th() function above) see the output of
        Code:
        help datetime##s9
        More generally, 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 and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.
        Thanks for the help. It worked!

        Comment

        Working...
        X