Here is a simple data step solution in a simple logic. Assume data is sorted by id date and the variable date is a sas numerical variable.
k=1; <---- 1 is lower bound
do s=k to 3; <-- 3 is upper bound.
data t1;
input id date quan price;
datalines;
1 1 10 2
1 2 9 2
2 1 8 2
2 2 7 2
2 3 6 2
3 2 5 2
;
run;
data t1;
retain k;
set t1;
by id;
if first.id then k=1;
do s=k to date-1;
quan_new=.; price_new=.;
output;
end;
quan_new=quan ; price_new=price;
output;
k=date+1;
if last.id then do;
do s=k to 3;
quan_new=.; price_new=.;
output;
end;
end;
drop date quan price k;
rename s=date;
/* Here is another way, it use do-loop and sort */
/* Try the following code to see if it is faster */
/* than merge and hash table for this special problem */
data test; /* create the sample data set */
do ID = 1000001 to 1100000;
do date = ceil(ranuni(123)*10) to ceil(ranuni(123)*10);
quan = ceil(ranuni(123)*100);
price = round(ranuni(123)*100,0.01);
output;
end;
end;
run;
proc sort data=test; /* the sorting is required if real data was not sorted */
by id date; run;
data test1;
set test;
by id;
if first.id then do; output;
if date ^= 1 then do;
do i = date-1 to 1 by -1;
date = i; call missing(quan, price);
output; /* fill the missing date */
end; /* end of loop for fill missing date */
end; /* end of loop for data^=1 */
end; /* end of loop for first.id */
if not last.ID then output;
if last.ID then do; output;
if date ^=10 then do;
do i = date+1 to 10;
date = i; call missing(quan, price); output;
end;
end;
end;
drop i;
run;
proc sort data=test1 out = result nodupkey;
by id date; run;
proc print data=result(obs=100); title 'result'; run;
Yes, you are right. It must be taken into account.
Sometime sorting use more time than computing.
However, for this case the hash table may be too large.