Announcement

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

  • Replacing date if in range

    Dear All,

    I am trying to replace date if it is in specific range. It works fine for a single year, however I have a data set which has 50 years of data. I could just make 50 line with updated date, however I believe it should be easier way to do it, and I would like to learn it.
    My code for a single year:

    replace nyear=1962 if inrange(newdate, 196207,196306)

    I tried with forval i=1962/2014 and replacing date with `i', however it did not work out as expected. Maybe someone has any ideas? Would appreciate it!

    Regards,
    Marijus

  • #2
    So, it looks like you have a rather unwieldy newdate variable. It is certainly not a Stata internal format date variable, because 196207 is Stata code for 12 March 2497. You will have nothing but grief from this way of representing dates. So the first step is to create real Stata internal format date variables. (I'm guessing that you actually have monthly variables, and that 196207 is supposed to be July 1962.)

    Code:
    gen stata_monthly_date = ym(floor(newdate/100), mod(newdate, 100))
    format stata_monthly_date %tm
    From there it seems that you want to extract a year from that, but you want the year to run from July through the subsequent june. With stata_monthly_date already counting off months, this is straightforward:

    Code:
    gen nyear = year(dofm(stata_monthly_date - 6))
    If you are going to work with chronological data in Stata you need to learn about Stata's system of date and time variables. Run -help datetime- and then click on the link to the PDF documentation for that. It's long and complicated, and even after reading it carefully, you won't remember everything. But you will at least understand how date and time variables are managed in Stata and will have been introduced to the numerous functions for transforming one kind to another, and for creating Stata date and time variables from information in other formats. You will also learn about how to assign display formats to these variables to that the inscruatble numbers will look natural to human eyes. After a short period of use you will get the habits you need, and you will remember the functions you use most often. You will probably always have to refer back to the help files or the PDF documentation for the ones you only need once in a while.

    Comment


    • #3
      Thank you, worked perfectly!

      Comment


      • #4
        I would want to know the STATA syntax to use if i want to know if date falls in a particular range of dates

        Comment


        • #5
          Re #4. Your question is not specific. Is the date of interest a particular constant date, or a variable? And is the range of dates defined by variables or by constant dates. In any case, see -help inrange()-. And then you may need to use the -td()- function to represent specific dates. (-help td()-)

          Comment


          • #6
            Re #5. Sorry, the date of interest is a variable (birth days) and the range of dates is constant (2 January 2013 to 1 January 2014)

            Comment


            • #7
              So

              Code:
              gen byte is_in_range = inrange(birthday, td(2jan2013), td(1jan2014))

              Comment


              • #8
                Thanks a lot Clyde, it worked!

                Comment


                • #9
                  Hello, is there a stata syntax I can use to identify invalid dates like 31 February 2014 in a dataset? Thanks for your help

                  Comment


                  • #10
                    Yes. In the future, for questions like this, it is best to show an actual example of your Stata data set, using the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                    For now, let me assume that your date variable is called date, and that they are all formatted in the way you show, day-month-year.

                    Code:
                    gen byte invalid_date = missing(daily(date, "DMY"))
                    The logic is simple: the daily() function attempts to convert the string variable date to a real Stata internal format date variable, parsing the strings as day followed by month followed by year. Whenever it encounters a value of date that cannot be correctly parsed that way as a valid calendar date, it returns missing value.

                    It seems you are doing a fair amount of work with dates. You need to familiarize yourself with Stata's date functions. So I suggest you interrupt your project and take the time to read the datetime chapter of the [D] volume of the Stata PDF documentation. Run -help time- and then click on the link at the top of that file. It's a lengthy chapter and there is a lot of material there. You won't remember it all: even experienced users sometimes have to go back to the help files or the documentation to refresh our memory of the details of some of the less frequently used functions. But it's all there. Read it from beginning to end so that you have a firm grasp on how Stata handles dates, and so that you will have seen all of the many functions Stata uses to convert between dates and strings, and between different kinds of dates. The time invested doing this will be manyfold repaid as you work.

                    Comment


                    • #11
                      Re #10. Thanks a lot for your help, I will have a look at the PDF documentation

                      Comment


                      • #12
                        Originally posted by Clyde Schechter View Post
                        So

                        Code:
                        gen byte is_in_range = inrange(birthday, td(2jan2013), td(1jan2014))
                        Clyde Schechter

                        Hi Clyde,

                        I'm using the following code to create a variable ramadan that takes the value of 1 if the individual was interviewed in ramadan

                        gen ramadan=.
                        replace ramadan =1 if inrange(intstartcmc, td(18mar1991), td(15apr1991))

                        My problem is, each time i run the code, it gives me different number of 1's in the ramadan variable. Any clue why this is happening and what i can do to fix it?

                        I'm attaching below data for only one country in one year. The idea is to do this for all countries in the dhs dataset. Ramadan happens on different dates in each year so i was using a code that looks like this:

                        replace ramadan =1 if inrange(intstartcmc, td(18mar1991), td(15apr1991)) | ///
                        inrange(intstartcmc, td(6mar1992), td(3apr1992)) | ///
                        inrange(intstartcmc, td(23feb1993), td(24mar1993))
                        .
                        .
                        . all the way for 24 years

                        the variable intstartcmc is cmc date varaible and i used the command convertCMC to change it in a stata readable date.

                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input long intstartcmc float ramadan
                        11441 .
                        11433 .
                        11413 1
                        11430 .
                        11440 .
                        11433 .
                        11421 1
                        11427 1
                        11415 1
                        11422 1
                        11432 .
                        11418 1
                        11434 .
                        11437 .
                        11421 1
                        11435 .
                        11434 .
                        11428 .
                        11436 .
                        11421 1
                        11441 .
                        11429 .
                        11416 1
                        11421 1
                        11428 .
                        11434 .
                        11417 1
                        11423 1
                        11433 .
                        11436 .
                        11415 1
                        11436 .
                        11424 1
                        11414 1
                        11434 .
                        11435 .
                        11421 1
                        11441 .
                        11414 1
                        11432 .
                        11442 .
                        11417 1
                        11438 .
                        11420 1
                        11425 1
                        11435 .
                        11420 1
                        11421 1
                        11433 .
                        11440 .
                        11428 .
                        11424 1
                        11433 .
                        11413 1
                        11436 .
                        11416 1
                        11420 1
                        11441 .
                        11429 .
                        11441 .
                        11436 .
                        11436 .
                        11429 .
                        11433 .
                        11437 .
                        11433 .
                        11429 .
                        11425 1
                        11415 1
                        11428 .
                        11442 .
                        11415 1
                        11433 .
                        11418 1
                        11437 .
                        11441 .
                        11440 .
                        11432 .
                        11441 .
                        11437 .
                        11442 .
                        11439 .
                        11434 .
                        11431 .
                        11426 1
                        11413 1
                        11415 1
                        11435 .
                        11413 1
                        11414 1
                        11418 1
                        11438 .
                        11436 .
                        11417 1
                        11430 .
                        11432 .
                        11417 1
                        11418 1
                        11422 1
                        11439 .
                        11436 .
                        11431 .
                        11428 .
                        11428 .
                        11435 .
                        11415 1
                        11423 1
                        11415 1
                        11429 .
                        11439 .
                        11439 .
                        11439 .
                        11428 .
                        11425 1
                        11442 .
                        11432 .
                        11428 .
                        11424 1
                        11440 .
                        11430 .
                        11436 .
                        11439 .
                        11419 1
                        11436 .
                        11419 1
                        11438 .
                        11429 .
                        11434 .
                        11418 1
                        11417 1
                        11417 1
                        11426 1
                        11419 1
                        11418 1
                        11428 .
                        11429 .
                        11440 .
                        11431 .
                        11441 .
                        11414 1
                        11430 .
                        11416 1
                        11422 1
                        11436 .
                        11436 .
                        11419 1
                        11426 1
                        11432 .
                        11429 .
                        11425 1
                        11424 1
                        11416 1
                        11422 1
                        11421 1
                        11425 1
                        11428 .
                        11415 1
                        11436 .
                        11420 1
                        11441 .
                        11438 .
                        11432 .
                        11441 .
                        11425 1
                        11432 .
                        11426 1
                        11421 1
                        11427 1
                        11420 1
                        11413 1
                        11418 1
                        11423 1
                        11416 1
                        11440 .
                        11438 .
                        11436 .
                        11431 .
                        11423 1
                        11432 .
                        11428 .
                        11423 1
                        11430 .
                        11424 1
                        11438 .
                        11432 .
                        11419 1
                        11425 1
                        11425 1
                        11427 1
                        11427 1
                        11436 .
                        11434 .
                        11428 .
                        11420 1
                        11442 .
                        11434 .
                        11425 1
                        11435 .
                        11429 .
                        11415 1
                        11422 1
                        11431 .
                        11425 1
                        11439 .
                        11420 1
                        11427 1
                        11430 .
                        11434 .
                        11424 1
                        11434 .
                        11432 .
                        11428 .
                        11422 1
                        11432 .
                        11418 1
                        11415 1
                        11428 .
                        11425 1
                        11432 .
                        11427 1
                        11414 1
                        11436 .
                        11419 1
                        11414 1
                        11438 .
                        11425 1
                        11439 .
                        11440 .
                        11440 .
                        11423 1
                        11430 .
                        11421 1
                        11438 .
                        11417 1
                        11427 1
                        11424 1
                        11433 .
                        11422 1
                        11431 .
                        11434 .
                        11440 .
                        11429 .
                        11415 1
                        11416 1
                        11418 1
                        11421 1
                        11442 .
                        11421 1
                        11413 1
                        11416 1
                        11433 .
                        11439 .
                        11416 1
                        11433 .
                        11442 .
                        11413 1
                        11433 .
                        11439 .
                        11435 .
                        11413 1
                        11420 1
                        11431 .
                        11426 1
                        11423 1
                        11416 1
                        11415 1
                        11436 .
                        11438 .
                        11425 1
                        11436 .
                        11416 1
                        11437 .
                        11430 .
                        11435 .
                        11441 .
                        11437 .
                        11424 1
                        11416 1
                        11433 .
                        11424 1
                        11441 .
                        11427 1
                        11422 1
                        11440 .
                        11423 1
                        11430 .
                        11424 1
                        11432 .
                        11427 1
                        11418 1
                        11427 1
                        11421 1
                        11442 .
                        11441 .
                        11431 .
                        11415 1
                        11438 .
                        11433 .
                        11424 1
                        11418 1
                        11441 .
                        11433 .
                        11431 .
                        11415 1
                        11423 1
                        11422 1
                        11439 .
                        11437 .
                        11413 1
                        11426 1
                        11418 1
                        11442 .
                        11420 1
                        11428 .
                        11431 .
                        11438 .
                        11423 1
                        11422 1
                        11426 1
                        11441 .
                        11441 .
                        11413 1
                        11426 1
                        11427 1
                        11416 1
                        11431 .
                        11436 .
                        11441 .
                        11436 .
                        11426 1
                        11434 .
                        11420 1
                        11420 1
                        11416 1
                        11416 1
                        11431 .
                        11424 1
                        11421 1
                        11433 .
                        11417 1
                        11434 .
                        11436 .
                        11434 .
                        11442 .
                        11434 .
                        11414 1
                        11440 .
                        11425 1
                        11419 1
                        11422 1
                        11437 .
                        11439 .
                        11438 .
                        11441 .
                        11414 1
                        11433 .
                        11426 1
                        11440 .
                        11420 1
                        11435 .
                        11424 1
                        11431 .
                        11427 1
                        11436 .
                        11432 .
                        11434 .
                        11421 1
                        11428 .
                        11425 1
                        11428 .
                        11414 1
                        11440 .
                        11414 1
                        11422 1
                        11427 1
                        11427 1
                        11418 1
                        11431 .
                        11424 1
                        11423 1
                        11428 .
                        11441 .
                        11433 .
                        11428 .
                        11420 1
                        11414 1
                        11441 .
                        11433 .
                        11439 .
                        11416 1
                        11441 .
                        11415 1
                        11433 .
                        11442 .
                        11423 1
                        11413 1
                        11427 1
                        11431 .
                        11427 1
                        11420 1
                        end
                        format %td intstartcmc
                        label values intstartcmc INTSTARTCMC
                        Last edited by danishussalam; 02 Oct 2021, 05:23.

                        Comment


                        • #13

                          You can get there with a statement like

                          Code:
                          gen ramadan = inrange(intstartcmc, td(18mar1991), td(15apr1991)) | inrange(intstartcmc, td(6mar1992), td(3apr1992)) | inrange(intstartcmc, td(23feb1993), td(24mar1993))
                          which generates a (0, 1) indicator. For more years than 1991 to 1993 this will inevitably get more complicated and there may be better ways to do it.


                          I can't think of any reason why this should give different results unless the dataset changes. There is no example in #12 to show this happening and there are no details on what else you may be doing.

                          See also https://www.stata-journal.com/articl...article=dm0099 for an attempt to cover basic principles with indicator variables. In particular, the popularity in code of first generate and then replace is hard to fathom whenever there is a direct approach.

                          However,
                          the variable intstartcmc is cmc date varaible and i used the command convertCMC to change it in a stata readable date.
                          Sorry, but I have no idea what this means and you don't explain the provenance of convertCMC.

                          Comment


                          • #14
                            Thanks, Nick Cox for the prompt reply.

                            Even with the code you suggested, it still gives me different values each time I run it. The only thing i do before running your code is convert the date variable because it's in the cmc format. I use the following code below:

                            use temp, clear
                            convertCMC intstartcmc
                            gen ramadan = inrange(intstartcmc, td(18mar1991), td(15apr1991))
                            tab ramadan

                            and each time i run the 4 lines above, i get different number of ones in the ramadan variable. The temp file is the data that i have attached below.

                            Code:
                            * Example generated by -dataex-. For more info, type help dataex
                            clear
                            input int intstartcmc
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            1096
                            end
                            label values intstartcmc INTSTARTCMC
                            label def INTSTARTCMC 1096 "1096", modify

                            Comment


                            • #15
                              The example dataset is just 1096 repeatedly. Sorry, but what use is that?

                              It's still true that you haven't explained convertCMC or gone beyond stating that you get different results

                              That's undoubtedly puzzling to you but I see nothing yet that helps anyone else explain.

                              Comment

                              Working...
                              X