Use Lag function in SAS find difference and delete if the value is less than 30

StackOverflow https://stackoverflow.com/questions/20200817

  •  04-08-2022
  •  | 
  •  

Pregunta

Eg.

Subject  Date   
1        2/10/13
1        2/15/13
1        2/27/13
1        3/15/13
1        3/29/13
2        1/11/13
2        1/31/13
2        2/15/13

I would need only the subjects with the dates between them more than 30.

required output:

Subject  Date   
1        2/10/13
1        3/15/13
2        1/11/13
2        2/15/13  
¿Fue útil?

Solución

This is a very interesting problem. I'll use the retain statement in the DATA step.

Since we are trying to compare dates between different observations, it's a bit more difficult. We can take advantage of the fact that SAS can convert dates to SAS date values (i.e. number of days after Jan 1 1960). Then we can compare these numeric values using conditional statements.

data work.test;
    input Subject Date anydtdte15.;
    sasdate = Date;

    retain x;

    if -30 <= sasdate - x <= 30 then delete;

    else x = sasdate;  

    datalines;
    1        2/10/13
    1        2/15/13
    1        2/27/13
    1        3/15/13
    1        3/29/13
    2        1/11/13
    2        1/31/13
    2        2/15/13
    ;
run;

proc print data=test;
    format Date mmddyy8.;
    var Subject Date;
run;    

OUTPUT as required:

                                Obs    Subject        Date

                                 1        1       02/10/13
                                 2        1       03/15/13
                                 3        2       01/11/13
                                 4        2       02/15/13
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top