Announcement

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

  • How do I extract month and year data from combined variables?

    Hi,
    I have 51 state name variables(long data) and income data (e.g.starting from inc_JAN_1996, inc_Feb_1996, ..., inc_Dec_2004, ..., inc_Dec_2005) with a wide shape.
    In other words, I have 51 long shaped and 120 wide shaped dataset and want to change into long data.
    --------------------
    reshape long inc_Jan inc_Feb ... inc_Dec, i(statename) j(year)
    _____________
    From this command, I could get only year data. Please help me to extract month and year data.

    Thanks in advance.

  • #2
    Code:
    reshape long inc_, i(statename) j(month_year) string
    gen mdate = monthly(month_year,    "MY")
    format mdate %tm
    drop month_year

    Comment


    • #3
      Cross-posted at https://stackoverflow.com/questions/...a-into-numeric

      I saw that before this thread and answered. What a waste of effort....

      We make our policy on cross-posting explicit. You are asked to tell us about it: https://www.statalist.org/forums/help#crossposting

      Comment


      • #4
        Thank you for reply. I am sorry that I did not notice your policy and did not mean to disregard it. I will not make a same mistake next time.

        I have more questions about code you made. The code is able to make 'mdate' variables with the shape of like 1996m4, 2000m12 and so on. Are these can be regarded as numeric? I assume these are not. Because, it was not possible to use the 'mdate' variables with if statement. What I want to do with 'mdate' is to replace existing variable with 1 if 'mdate' is greater or equal than 2000m3. Here's the code what I have been trying to do.

        gen post = 0
        replace post = 1 if mdate >= 2000m3

        I think it is hard to recognize 'mdate' as numeric. Is there any way to fix it?

        Comment


        • #5
          You're alluding to #2.

          mdate is completely and utterly numeric.

          The help for monthly() tells you that it creates a variable. with integer values.

          Code:
          describe mdate
          will tell you that it is numeric, more precisely float.

          The problem lies in your use of 2000m3 and is a little subtle. You can't use a date display format like that.

          You can do something like

          Code:
          gen post = 0
          replace post = 1 if mdate >= ym(2000, 3)
          or even better

          Code:
          gen post = mdate >= ym(2000, 3) 
          or as good or better

          Code:
          gen post = mdate >= tm(2000m3) 
          For more, see

          Code:
          help datetime functions
          Please do read the entire FAQ Advice, as every prompt reminds you. https://www.statalist.org/forums/help For example, learning how to present code well is very easy and is explained there.

          Comment


          • #6
            Cross-posted at https://stackoverflow.com/questions/...a-into-numeric

            Comment


            • #7
              Is there a way to extract the month from a XXXXmY-formatted variable regardless of the year? I'd like to create season dummies equal to one if the month of mdate is in winter, summer, spring, fall...

              Thanks!

              Comment


              • #8
                See
                Code:
                help datepart()
                or
                Code:
                help clockpart()
                depending on whether you have a numeric date variable or a time variable. You might even want to use quarter().

                Comment


                • #9
                  The conventional climatological seasons are DJF, MAM, JJA, SON in what I hope is transparent form. DJF is Southern Hemisphere summer, and so forth.

                  In short, you may need to spell out your definitions of seasons if different.

                  Comment


                  • #10
                    Hemanshu and Nick:

                    Thank you both for your useful comments. datepart() worked perfectly, and indeed I should first create a dummy that distinguishes hemispheres. Thanks!

                    Comment

                    Working...
                    X