How to create a new variable called TEST that equals the value 1 (in all lines) whenever the sequence 1-2-1 occurs in the lines of variable VAR_B for each group of PAR_PESS and DT_DATE, if the interval between DT_DATE is greater or equal to 1, calculated from variables with value 1 in VAR_B (and create a variable named DIFF with the time difference value):
Data example:
I would like it to stay that way:
Data example:
| PAR_PESS | VAR_B | DT_DATE |
| 1 | 1 | 01/01/23 |
| 1 | 2 | 02/01/23 |
| 1 | 1 | 03/01/23 |
| 1 | 1 | 04/01/23 |
| 1 | 2 | 05/01/23 |
| 1 | 1 | 06/01/23 |
| 2 | 2 | 01/01/23 |
| 2 | 1 | 02/01/23 |
| 2 | 1 | 03/01/23 |
| 2 | 2 | 04/01/23 |
| 2 | 1 | 05/01/23 |
| 3 | 1 | 02/01/23 |
| 3 | 2 | 03/01/23 |
| 3 | 1 | 04/01/23 |
| 3 | 2 | 05/01/23 |
| 4 | 1 | 01/01/23 |
| 4 | 2 | 02/01/23 |
| 4 | 2 | 03/01/23 |
| 4 | 1 | 04/01/23 |
| 4 | 2 | 05/01/23 |
| 4 | 2 | 06/01/23 |
| 4 | 1 | 07/01/23 |
| 4 | 2 | 08/01/23 |
| 4 | 1 | 09/01/23 |
I would like it to stay that way:
| PAR_PESS | VAR_B | DT_DATE | TEST | DIFF |
| 1 | 1 | 01/01/23 | 1 | . |
| 1 | 2 | 02/01/23 | 1 | . |
| 1 | 1 | 03/01/23 | 1 | 2 |
| 1 | 1 | 04/01/23 | . | . |
| 1 | 2 | 05/01/23 | 1 | . |
| 1 | 1 | 06/01/23 | 1 | 3 |
| 2 | 2 | 01/01/23 | . | . |
| 2 | 1 | 02/01/23 | 1 | . |
| 2 | 1 | 04/01/23 | . | . |
| 2 | 2 | 04/01/23 | 1 | . |
| 2 | 1 | 05/01/23 | 1 | 3 |
| 3 | 1 | 02/01/23 | 1 | . |
| 3 | 2 | 03/01/23 | 1 | . |
| 3 | 1 | 04/01/23 | 1 | 2 |
| 3 | 2 | 05/01/23 | . | . |
| 4 | 1 | 01/01/23 | 1 | . |
| 4 | 2 | 02/01/23 | 1 | . |
| 4 | 2 | 03/01/23 | . | . |
| 4 | 1 | 04/01/23 | 1 | 3 |
| 4 | 2 | 04/01/23 | . | . |
| 4 | 2 | 06/01/23 | 1 | . |
| 4 | 1 | 07/01/23 | 1 | 3 |
| 4 | 2 | 08/01/23 | 1 | . |
| 4 | 1 | 09/01/23 | 1 | 2 |

Comment