Announcement

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

  • Date format with decorrelation

    Hey!

    I currently have a dataset where I am trying to decorrelete 5 price variables on spot price for electricity on 2 variables of fixed price electricity. I am quite new to Stata, but I have two variables where the first variable A shows the year, for example 2023, and the second variable B shows the year and week, for example 2023-1. What I have been trying is to split the B variable up into B1 and B2 and then add them together to get a date variable to use like this:

    split B, parse ("-")
    gen date = date(B1 + "w" + B2, "Yw")

    But when I do this I am only getting dots in the date variable when browsing through the data (missing values)? And when I see over B1 and B2 they look fine. After that my idea was to use pca after I could get tsset date to work. Do anyone see the problem and could point me in the right direction?

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 B
    "2020-1"
    "2020-2"
    "2020-3"
    "2020-4"
    "2021-1"
    end
    
    gen wanted= weekly(B, "YW")
    format wanted %tw
    Res.:

    Code:
    . l
    
         +-----------------+
         |      B   wanted |
         |-----------------|
      1. | 2020-1   2020w1 |
      2. | 2020-2   2020w2 |
      3. | 2020-3   2020w3 |
      4. | 2020-4   2020w4 |
      5. | 2021-1   2021w1 |
         +-----------------+

    Comment


    • #3
      Note that weekly() knows nothing about any definition of week which implies that sometimes there are 53 weeks in a year. If that is a problem you need some other way to deal with them.

      Other way round date() is, and is only, a way to extract daily dates from string input, but that is not what you want.

      Comment


      • #4
        A Stata user and two R users walked into a bar.
        Last edited by Nick Cox; 31 Mar 2023, 09:38.

        Comment


        • #5
          Good jokes always seem to involve three people.

          (Sorry about the triple posting.)
          Last edited by Nick Cox; 31 Mar 2023, 09:40.

          Comment


          • #6
            Perhaps this improvement works?
            Code:
            split B, parse("-") destring
            gen timevar=yw(B1,B2)

            Comment

            Working...
            X