Search code examples
copysasiterationoutputdo-loops

SAS: Construct Do Until by Copying Last Row


I have yearly data that ends at 2013, but I'd like to use that year and copy it for 2014, 2015, and ultimately 2016 (whatever the current year of data we have). Using the test data below:

DATA TEST;
   INPUT YEAR Q1 Q2 Q3 Q4;
   DATALINES;
   2012 20 30 40 50
   2013 21 29 43 47
   ;
RUN;

I would ultimately want a data set that has:

2012 20 30 40 50
2013 21 29 43 47
2014 21 29 43 47
2015 21 29 43 47
2016 21 29 43 47

This is just a quick example as my data is much larger and more complex, but is there a quick DO UNTIL and OUTPUT method to copy that last row as many times as needed and make YEAR+1 each iteration? Essentially, copy the last row until YEAR equals the latest year on file.


Solution

  • You just need to detect the end of the data. Either use the END= option or if your data is grouped use LAST. flag from BY group processing.

    data want ;
      set have end=eof;
      output;
      if eof then do year=year+1 to 2016;
        output;
      end;
    run;