Viewing 11 posts - 1 through 11 (of 11 total)
  • Any SAS programmers about?
  • whippersnapper
    Free Member

    If so can I ask for a little help please. I have bitten off more than I can chew…SAS programming (well all programming) is a little alien to me, I have got my head round the SQL part of it but I don’t think SQL is appropriate for the next bit. What I want to do isn’t difficult, I have managed it in VBA, unfortunately I have no idea where to start in SAS. Any tips or pointers greatly appreciated.

    I have a list of dates for a particular groups, so A could have 4 dates, B 10, C 1 etc. I want to look at a date, compare it to the record (row) previous and if the year is not consecutive then add in as many years to make it consecutive. eg. 23/06/2001, 04/12/2005 (2 rows) I would want to become 23/06/2001, 01/01/2002, 01/01/2003, 01/01/2004, 04/12/2005 (the program inserts 3 new rows). This would be done for each group only, so comparisons between last row of A and first of B are not made.

    Apologies for asking here first, I could try a programmers forum but they are scarier than this place!

    whippersnapper
    Free Member

    bump for lunch…

    druidh
    Free Member

    It’s a long time since I did SAS – before they started using SQL actually.

    qwerty
    Free Member

    whippersnapper
    Free Member

    ….Rodney

    bigjim
    Full Member

    geek 😛

    alanf
    Free Member

    What you need to do is sort the dates first into the order you want, i.e. by group. Get the year part of the date and create a ‘Do’ loop from the start year until you reach the next year, maybe something along the lines of

    if start+1 < end then output new row

    whippersnapper
    Free Member

    I’m trying…not clever enough though

    EDIT – that was to Bigjim. Thanks Alan

    robh
    Full Member

    You could sort and use a lag function,
    Data date;
    set orgdata;
    if group =lag(group) then do;
    if year(date) ne year(lag(date) then do;
    date = ’01JAN’||year(lag(date));
    end;
    end;
    else output;
    run;

    or something like that depending on how the data set is structured or sorted.

    whippersnapper
    Free Member

    brilliant, thank you. That should set me off on the right track.

    robh
    Full Member

    Saying that lags and if’s together not a good idea.

    data dataset2;
    format date date9.;
    set dataset1;
    by group date;

    retain prevdate;
    drop prevdate i;

    if first.group then do;
    prevdate = .;
    output;
    end;
    else do;
    output;
    do i = year(prevdate) + 1 to year(date) – 1;
    date = mdy(1, 1, i);
    output;
    end;
    date = mdy(1, 1, i);
    end;
    prevdate = date;
    run;

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘Any SAS programmers about?’ is closed to new replies.