Home > front end >  Assign first date and last dates to a new variable
Assign first date and last dates to a new variable

Time:01-01

I have a dataset which contain 2 date fields (date1 and date2) , I have to create two new date fields where the date3 will be the earliest date in date1 and date4 will be date2 31 days. And it has to be done grouped on testid. Data that I have :

data have;
input acct_num test_id date1 date2 ;
DATALINES; 
55203610 1 2-feb-20    15-Apr-20
55203610 1 15-feb-20   15-Apr-20     
55203610 1 20-feb-20   15-Apr-20
55203610 2 1-Jan-20    20-May-20     
55203610 2 15-Mar-18   20-May-20
55203610 3 1-feb-17    1-Jul-20
;
run;

data want;
input acct_num test_id date1 date2 date3 date4;
DATALINES; 
55203610 1 2-feb-20    15-Apr-20   2-Feb-20   16-May-20
55203610 1 15-feb-20   15-Apr-20   2-Feb-20   16-May-20  
55203610 1 20-feb-20   15-Apr-20   2-Feb-20   16-May-20
55203610 2 1-Jan-20    20-May-20   15-Mar-18  20-Jun-20
55203610 2 15-Mar-18   20-May-20   15-Mar-18  20-Jun-20
55203610 3 1-feb-17    1-Jul-20    1-feb-17   1-Aug-20
;
run;

Any help will be appreciated.

CodePudding user response:

Use a DoW Loop

data want;
    do until (last.test_id);
        set have;
        by test_id;
        minval=min(minval, date1);
    end;

    do until (last.test_id);
        set have;
        by test_id;
        date3=minval;
        date4=date2   31;
        output;
    end;

    format date: date9.;
    drop minval;
run;
  • Related