Mayara Mayara - 4 months ago 8
Linux Question

How to separate periods and copy lines in a file using sas or linux?

My original file is divided by time periods like this:

Period P1 P2 P3 P4 P5 P6 P7
03/01/2013 to 06/01/2013 31.0 16.5 28.7 18.1 23.4 16 12.0
07/01/2013 to 09/01/2013 31.0 18.0 29.3 19.2 24.3 13 3.6


However, the file has many rows and I need those files remain like that:

Period P1 P2 P3 P4 P5 P6 P7
03/01/2013 31.0 16.5 28.7 18.1 23.4 16 12.0
04/01/2013 31.0 16.5 28.7 18.1 23.4 16 12.0
05/01/2013 31.0 16.5 28.7 18.1 23.4 16 12.0
06/01/2013 31.0 16.5 28.7 18.1 23.4 16 12.0
07/01/2013 31.0 18.0 29.3 19.2 24.3 13 3.6
08/01/2013 31.0 18.0 29.3 19.2 24.3 13 3.6
09/01/2013 31.0 18.0 29.3 19.2 24.3 13 3.6


Does anyone know how do it in a practical way using sas or linux?

Tom Tom
Answer

Just use a DO loop. You can use the INTCK() function to know how many iterations and the INTNX() function to calculate the new date values.

Here is you sample data.

data have;
  length Start Stop P1-P7 8;
  informat Start Stop mmddyy.;
  format Start Stop yymmdd10.;
  input start stop p1-p7;
cards;
03/01/2013 06/01/2013 31.0 16.5 28.7 18.1 23.4 16 12.0
07/01/2013 09/01/2013 31.0 18.0 29.3 19.2 24.3 13 3.6
;;;;

Now write a new data step to expand the number of rows.

data want;
 format period yymmdd10.;
 set have ;
 do offset=0 to intck('month',start,stop);
   period = intnx('month',start,offset);
   output;
 end;
run;

Proc Print;