DATA Step, Macro, Functions and more

Adding additional rows based on time variable

Reply
Occasional Contributor
Posts: 6

Adding additional rows based on time variable

[ Edited ]

I'm trying to create additional rows based on time variable.

from time=0 to time=1, additional rows to be created with an increment of 0.1

from time=1 to time=3, additional rows to be created with an increment of 0.5.

 

 

 

Please find the input and output datasets below.

 

DATA HAVE;
INPUT ID TIME D1 D2 E1;
DATALINES;
1 -0.25 . . 0
1 0 . . 1
1 3 . . 0
1 672.25 . . 0
2 0 . . 0
2 0 . . 1
2 3 21300 9.966 0
2 669.5 2760 7.923 0
2 670.0833 . . 1
2 673.25 31400 10.355 0
2 1509.1667 3760 8.232 0
2 1509.1667 . . 1
2 1512.1667 24100 10.09 0
2 2181.4167 4170 8.336 0
2 2181.5 . . 1
2 2184.5333 26900 10.2 0
2 2853.5 . . 1
2 3525.7917 . . 1
2 3981.5 9310 9.139 0
;

DATA WANT;
INPUT ID TIME D1 D2 E;
DATALINES;
1 -0.25 . . 0
1 0 . . 1
1 0.2 . . 0
1 0.4 . . 0
1 0.6 . . 0
1 0.8 . . 0
1 1 . . 0
1 1.5 . . 0
1 2 . . 0
1 2.5 . . 0
1 3 . . 0
1 672.25 . . 0
2 0 . . 0
2 0 . . 1
1 0 . . 1
1 0.2 . . 0
1 0.4 . . 0
1 0.6 . . 0
1 0.8 . . 0
1 1 . . 0
1 1.5 . . 0
1 2 . . 0
1 2.5 . . 0
2 3 21300 9.966 0
2 669.5 2760 7.923 0
2 670.0833 . . 1
2 673.25 31400 10.355 0
2 1509.1667 3760 8.232 0
2 1509.1667 . . 1
2 1512.1667 24100 10.09 0
2 2181.4167 4170 8.336 0
2 2181.5 . . 1
2 2184.5333 26900 10.2 0
2 2853.5 . . 1
2 3525.7917 . . 1
2 3981.5 9310 9.139 0
;

Super User
Super User
Posts: 7,942

Re: Adding additional rows based on time variable

Something like:

data have;
  input id time d1 d2 e1;
datalines;
1 -0.25 . . 0
1 0 . . 1
1 3 . . 0
1 672.25 . . 0
2 0 . . 0
2 0 . . 1
2 3 21300 9.966 0
2 669.5 2760 7.923 0
2 670.0833 . . 1
2 673.25 31400 10.355 0
2 1509.1667 3760 8.232 0
2 1509.1667 . . 1
2 1512.1667 24100 10.09 0
2 2181.4167 4170 8.336 0
2 2181.5 . . 1
2 2184.5333 26900 10.2 0
2 2853.5 . . 1
2 3525.7917 . . 1
2 3981.5 9310 9.139 0
;
run;

data want (drop=i);
  set have;
  if 0 <= time <= 1 then do;
    do i=time to 1 by 0.1;
      time=i;
      output;
    end;
  end;
  else if 1 < time <= 3 then do;
    do i=time to 3 by 0.5;
      time=i;
      output;
    end;
  end;
  else output;
run; 
Occasional Contributor
Posts: 6

Re: Adding additional rows based on time variable

@RW9: Thanks. In your code the increment from time=0 to time=1 by 0.1 works but not the time=1 to time=3 by 0.5. not sure why?

Super User
Super User
Posts: 7,942

Re: Adding additional rows based on time variable

There is only one record in your test data which is within 1 and 3 and because that row is 3, do i=3 to 3 by 0.5 doesn't do anything.  

Ask a Question
Discussion stats
  • 3 replies
  • 98 views
  • 0 likes
  • 2 in conversation