Search code examples
sasretain

how to use retain statement in SAS to populate missing data?


I have a dataset which has multiple rows of data for a given person, but only the first row of the person's information contains their name. The rest of the rows of that person's data have the name field missing. I think I can use the retain statement to populate the name, but nothing I try works.

Here is an example of the dataset structure I am working with:

    data test;
    input id $ value ;
    datalines;
    Bob 100
    .   200
    .   300
    Jim 475
    .   250
    .   300
    ;
    run;

Solution

  • I think the problem is that technically id is not missing in those rows, it equals ., even though when reading datalines with input statement you get empty id. Try this:

    data test;
      input id $ value;
    
      /*store not empty ID in different retained variable*/
      retain current_id;
      if not missing(id) then current_id=id;
      else id=current_id;
    
    datalines;
    Bob 100
    .   200
    .   300
    Jim 475
    .   250
    .   300
    ;
    run;