Announcement

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

  • Can't input date in Stata. SRF from HRF

    Hello everyone!

    I'm running into this really really annoying problem. I'm trying to import this column in STATA which contains the date However,, I can't seem to input it inside STATA while using any variation of the date() function. Whatever I do I end up getting missing values.

    Can anyone share a code through which I can input it successfully to Stata?

    I'm using stata 9.

    It currently looks like this - the data is in CSV.



    Click image for larger version

Name:	Column.png
Views:	1
Size:	12.6 KB
ID:	1365178



  • #2
    At a guess we are looking at part of a worksheet in Excel. What are the corresponding variable names and storage types in Stata? Please see
    http://www.statalist.org/forums/help#stata

    P.S. You have it right in "Stata". http://www.statalist.org/forums/help#spelling

    Comment


    • #3
      Thanks for the response! The variable name, in Stata, of the column is "date". It is stored as a str29 with its display format being "%29" . I don't really think that sharing the names of the other variables will be of help, that's why I didn't share this unnecessary detail.

      I think the problem is basically that the day is also appended with the MDY details. So maybe because of this the date() command isn't working and only missing values are being generated?
      Last edited by Saif Ul Haq; 22 Nov 2016, 08:27.

      Comment


      • #4
        For your example, what's most obvious is that you must ignore the "Sunday," part. So, this works for your example, but I don't know whether the rest of the data are all of similar form. The logic is that you must get Stata to look only at the substring after the first comma.


        Code:
        gen ndate = daily(substr(date, strpos(date, ",") + 1, .), "MDY")
        format ndate %td
        Note that %29 is not a legal format, but we can fill in %29s given the other information.

        Comment


        • #5
          Thanks for the help. I've used your command but still it is only generating a new variable that contains missing values only. What could potentially be causing this :/ ?

          Comment


          • #6
            I guess that something you are not telling us is causing the difference.

            Again, let me underline that my code works with your example:

            Code:
            . clear
            
            . set obs 1
            number of observations (_N) was 0, now 1
            
            . gen date = "Sunday, February 01, 2015"
            
            . gen ndate = daily(substr(date, strpos(date, ",") + 1, .), "MDY")
            
            . format ndate %td
            
            . list
            
                 +---------------------------------------+
                 |                      date       ndate |
                 |---------------------------------------|
              1. | Sunday, February 01, 2015   01feb2015 |
                 +---------------------------------------+
            So, at a minimum you need to show the code you tried and data on which it produced missing.

            Do please read the link given in #2 which you are not yet taking seriously enough!

            But you did say that you were using Stata version 9. I don't have access to version 9 at present, but I am not sure whether daily() was included in Stata 9. But if not, you would have got an error message, not missings everywhere.
            Last edited by Nick Cox; 22 Nov 2016, 11:02.

            Comment


            • #7
              Sorry. I'll pay more attention to that link from now on.

              There really isn't any use of sharing the code or my data set. I copied and pasted the code that you've shared:


              clear
              set obs 1
              number of observations (_N) was 0, now 1
              gen date = "Sunday, February 01, 2015"
              gen ndate = daily(substr(date, strpos(date, ",") + 1, .), "MDY")
              format ndate %td
              list
              and the result at my end was

              Click image for larger version

Name:	ScreenShot.png
Views:	1
Size:	14.2 KB
ID:	1365289


              I guess the only thing that could be causing it is the fact that i'm using Stata 9? There must be something really particular to Stata 9 that is causing it.

              Comment


              • #8
                first, please don't post screen shots - read the FAQ for why

                second, your first gen statement does not do what you think it does; try it again and then immediately list your data; you need to tell Stata that you are inputting a string variable; see the help for generate

                Comment


                • #9
                  Rich's general advice is bang on; otherwise I think the explanation lies elsewhere.

                  I've found a machine with Stata 9.2.

                  You need a different syntax.

                  Code:
                  gen ndate = daily(substr(date, strpos(date, ",") + 1, .), "mdy")
                  We're 5 versions on (a full decade) from Stata 9 and I had forgotten the syntax change.


                  Comment

                  Working...
                  X