简体   繁体   中英

longitudinal calculations in SAS

I have a patient data and their visit dates., I am to findout the change in their behaviour with in 90 Days of their visit.,

I have the data this way.,

PERSN Reg_date  out_date    Formulartyp
225 11-apr-12   03-jun-13   Gles
225 03-sep-13   06-sep-13   Täta
458 13-mar-13   03-apr-13   Täta
458 22-jul-13   07-aug-13   Täta
458 22-aug-13   28-nov-13   Gles
559 11OCT2013   25OCT2013   Täta
559 01-nov-13   04-dec-13   Gles
897 08-feb-12   11-jan-13   Gles
897 18-jan-13   05-feb-13   Täta
897 26-feb-13   30-apr-13   Täta
897 14MAY2013   01OCT2013   Gles
897 08OCT2013   29OCT2013   Täta
565 08-jan-13   17-jun-13   Gles
565 03-sep-13   27-dec-13   Gles
878 07-dec-12   09-jan-13   Täta
878 16-jan-13   31MAY2013   Gles
554 19-dec-12   08-jan-13   Gles
554 22-mar-13   16-apr-13   Täta
554 15MAY2013   16-jul-13   Gles
554 09-sep-13   17-sep-13   Täta
554 24-sep-13   29OCT2013   Gles

I want to find out the difference(in Days) between the out_date and the next reg_date, and then find out if there is a difference in formulartyp.

I am trying to use first.persnr and lag functions but I am not able to get it.!

One way is to use LAG function... I am giving a rough code. Modify it as per ur requirement

data compared;
attrib date_diff formulartyp_diff length=8.;
set indata;
prev_persn=lag(persn);
prev_out=lag(out_date);
if(persn eq prev_persn and formulartyp ne lag(formulartyp)) then do;
    formulartyp_diff=1;
end;
if(persn eq prev_persn) then do;
    date_diff=input(reg_date,date9.)-input(prev_out,date9.);
end;
run;

I tend not to use the lag function, I prefer using retain to get the values of a variable from the previous observation. The assignment needs to be done last thing in the data step. You need to transform the dates into sas dates (number of days since JAN 1st 1960) using the appropriate informat to compute the day difference. And data need to be sorted by patient and date.

data visits;
input
    @1  PERSN        3.
    @5  Reg_date     date9.
    @17 out_date     date9. 
    @29 Formulartyp  $4.
;
format Reg_date out_date date9.;
datalines;
225 11-apr-12   03-jun-13   Gles
225 03-sep-13   06-sep-13   Täta
458 13-mar-13   03-apr-13   Täta
458 22-jul-13   07-aug-13   Täta
458 22-aug-13   28-nov-13   Gles
559 11OCT2013   25OCT2013   Täta
559 01-nov-13   04-dec-13   Gles
897 08-feb-12   11-jan-13   Gles
897 18-jan-13   05-feb-13   Täta
897 26-feb-13   30-apr-13   Täta
897 14MAY2013   01OCT2013   Gles
897 08OCT2013   29OCT2013   Täta
565 08-jan-13   17-jun-13   Gles
565 03-sep-13   27-dec-13   Gles
878 07-dec-12   09-jan-13   Täta
878 16-jan-13   31MAY2013   Gles
554 19-dec-12   08-jan-13   Gles
554 22-mar-13   16-apr-13   Täta
554 15MAY2013   16-jul-13   Gles
554 09-sep-13   17-sep-13   Täta
554 24-sep-13   29OCT2013   Gles
;
run;
proc sort data=visits;
    by PERSN Reg_date;
run;
data visits;
    set visits;
    by PERSN;
    retain LagOut_date LagFormulartyp;
    if not first.PERSN then do;
        NDays=Reg_date-LagOut_date;
               /*boolean variable:1 if the condition is satisfied, 0 otherwise*/
        Change=(LagFormulartyp^=Formulartyp);
    end;
    LagFormulartyp=Formulartyp;
    LagOut_date=Out_date;
    drop LagFormulartyp LagOut_date;
run;

If you want you can then filter on NDays>=90

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM