Search code examples
duplicatessasmacros

how do i collapse multiple rows of data into a single row in sas


im having a hard time dealing with duplicate observations. currently, i have this output:

incorrect_output

however, i need this output (below) so i can have accurate end of year counts.

correct_output

can someone please help me create code to collapse these rows without losing data. thaanks in advance.


Solution

  • For this kind of operation, I like to use one of my favorite SAS trick: the data step update statement.
    Assumption is that the data is already sorted.

    data have;
        input id var1$ var2$ var3;
        datalines;
    1 . . 100
    1 a . .
    1 . b .
    2 c . .
    2 . d 200
    ;
    run;
    
    data want;
    update have(obs=0) have;
    by id;
    run;
    
    id var1 var2 var3
    1   a    b   100
    2   c    d   200