Announcement

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

  • Plotting variable for 5th and 25th day each month for multi-month panel data


    Hello (new here so excuse if my jargon sounds odd) For my panel data (going from 1st Jan 2012 - 30 December 2013) I want to plot the values for a variable x for the 5th and 25th day of each month, starting in July 2012. I managed to drop the months before July 2012 with
    Code:
    drop x_201201* x_201202* x_201203* x_201204* x_201205* x_201206*
    , but that seems not only inefficient, but is also not the way I want to extract the values for the 5th and 25th observations, as this would get the code really messy. Is ther a way to extract the 5th and 25th each month somehow that I can plot it? Ive read solutions which apply for specific weekdays, but not specific days. Would appreciate any help here. [note: In case it is necessary info: my date variable "date" is not formatted e.g. through
    Code:
    gen date = date(day, "YMD") format date %td]

  • #2
    If I understand correctly, your problem arises from a deeper problem, holding time series in a wide layout (some people say wide structure or wide format) when almost everything in Stata is easier with a long layout;

    Code:
    reshape long
    and then you can select observations (as they now are) to your heart's content.

    Comment


    • #3
      Hello Nick,

      thanks for the quick answer.

      I have reshaped my code already to have the dates in the long format as seen here. :

      Code:
      use data.dta, clear
      keep id x_2012* x_2013*
      drop x_201201* x_201202* x_201203* x_201204* x_201205* x_201206*
      reshape long x_, i(id) j(date)
      Nevertheless, I dont quite get your last sentence. I mean, I could code my problem by generating an own variable for each day

      Code:
      gen fivejuly12 = date if date == 20120705
      Code:
      gen twenfithjuly12 = date if date == 20120725
      , but isn't that super tidious and messy to this for until end 2013 (i.e. 17 month)? And since I have to do this for 5 different groups as well, it would take me 85 variables.

      Or do I misinterpreted your statement, and there is a simpler way?
      Last edited by Pierre Kind; 15 Jul 2020, 04:44.

      Comment


      • #4
        I am struggling here without a data example from you.

        But even if your dates have the unsatisfactory form of integers like 20120705 and 20120725 you can select such dates with

        Code:
        ... if inlist(mod(date, 100), 5, 25)
        without any need to create new variables.

        Your dates are best converted to orthodox Stata daily dates. One reason why is shown by this example: Stata has no homunculus inside spotting that 20121231 and 20130101 "really" differ by 1 not 8870.

        Code:
        gen betterdate = daily(string(date, "%8.0f"), "YMD")  
        format betterdate %td
        is one way forward.
        Last edited by Nick Cox; 15 Jul 2020, 05:37.

        Comment


        • #5
          Cross-posted at https://stackoverflow.com/questions/...th-time-period

          We do have a policy on cross-posting and it is explicit. You are asked to tell us about it. https://www.statalist.org/forums/help#crossposting

          Some people regard cross-posting in different forums as thoughtless, if not offensive, in implying that you don't care two hoots about the possibility that people might waste their time answering because the same answer, or a better one, has already been posted elsewhere. I don't go that far but know that this view exists.

          Comment


          • #6
            Hello,

            first of all. Thanks, the inlist command worked out. Super thank you.

            Second, really sorry for that. I didn't know something like a cross-posting policy existed. I will mention it in my posts from now on. Thanks for letting me know.

            Comment


            • #7
              OK, but the Statalist home page and the prompt for every new thread enjoin you to read the FAQ Advice before posting, which explains this policy, among other things.

              So the implication is that you didn't do that. That is a very small deal in the big scheme of things, but we did ask.

              Comment


              • #8
                Oh ok, I didn't read the FAQ. I will do that now. Sorry again for the behavior.

                Comment

                Working...
                X