Code: Select all
series first_week2 = @datediff(@date, @datefloor(@date, "mm"), "ww") = 0
series first_week3 = @recode(@month <> @month(-1) or @isna(@month(-1)), 1, 0)
For (2-1) and (2-2), it sounds like you want to take a dummy and make a trend at all the 1s. Your code is on the right track, but you've switched some arguments and made it more complicated than it needs to be. For example, if you have a dummy for the first week, you can build the trend like this,
Code: Select all
series mytrend = @recode(first_week, @cumsum(first_week), 0)
If you didn't want to create the dummy series beforehand, you can of course replace it with the dummy's generating expression. For example,
Code: Select all
series mytrend = @recode(@date < @datefloor(@date, "mm") + 7, @cumsum(@date < @datefloor(@date, "mm") + 7), 0)
For (2-3) and (2-5), you're trying to generate something that is trend-like but is constant within each month. A quick way to implement such pattern variants is find a formula that maps another pattern to the one you want. Based on the examples we've gone through, you should be able to create a basic trend for the first two weeks of each month, similar to (2-1) and (2-2). Ignoring the zeros, that trend of course follows the sequence 1, 2, 3, 4, 5, 6, etc., where as you want 1, 1, 2, 2, 3, 3, etc. You can easily map each number in the first sequence to the corresponding number in the second sequence with a simple formula, f(x) = floor((x + 1) / 2). For (2-3), this could translate into code as follows (using a dummy to keep the line from being too long),
Code: Select all
series first_second_week = @date < @datefloor(@date, "mm") + 14
series mytrend = @recode(first_second_week, @floor((@cumsum(first_second_week) + 1) / 2), 0)
You can use the same strategy for (2-4) and (2-6). Instead of mapping a basic trend to 1, 1, 2, 2, 3, 3, etc., you map it to 1, 2, 1, 2, 1, 2, etc.
Since there are usually may ways to generate any particular pattern, adapting a previous pattern won't always be the most obvious or most concise method. For example, in (2-6) we can make use of the fact that the non-zero elements you want in each month are both consecutive and one more than their preceedings value. These properties can be exploited to generate the series you want in a different way. For example,
Code: Select all
series second_third_week = @date >= @datefloor(@date, "mm") + 7 and @date < @datefloor(@date, "mm") + 21
series mytrend = @recode(second_third_week, mytrend(-1) + 1, 0)