Search code examples
sqlsasproc

Combining Rows with Multiple Columns by ID in SAS EG


I have a table that has an ID column and multiple DATE columns. Each ID row is repeated each time it has data in a date column. I am looking to combine the rows so that there is a single row for each ID, with the data for all date columns populated.

Image of my current output Image of my current output.

Image of the output I am looking for. Image of the output I am looking for.

I have tried transposing and sorting the data, then running PROC SQL with a group by clause for ID_COLUMN


Solution

  • Let me show you one of my favorite SAS trick to perform this kind of operation.
    Assumption is that the data is already sorted by id_column

    data want;
    update have(obs=0) have;
    by id_column;
    run;
    

    Let's take this minimal reproducible example (next time give your input with a table instead of a picture)

    id_column  date1   date2    date3
      id_1    51.29405   .        .
      id_1       .       .     50.84405
      id_2    71.99253   .        .
      id_2       .       .     63.62253
      id_2       .    93.4383     .
    
    data want;
    update have(obs=0) have;
    by id_column;
    run;
    
    id_column   date1    date2    date3
      id_1    51.29405     .     50.84405
      id_2    71.99253  93.4383  63.62253