Suppose you have
- a dataset HAVE with one observation per ID, containing ID and value (V),
- a dataset INCR with one observation per ID, containing ID and number of incrementations (N),
both sorted by ID.
Example:
data have(drop=n) incr(drop=v);
input id v n;
cards;
1 855 2
2 101 0
3 998 4
;
Then you can merge the two datasets by ID and in the same DATA step create the additional observations with the incremented V values for each ID:
data want(drop=n);
merge have incr;
by id;
output;
do n=1 to n;
v=mod(v,1000)+1;
output;
end;
run;
Result:
id v
1 855
1 856
1 857
2 101
3 998
3 999
3 1000
3 1
3 2
(Highlighted observations were newly inserted.)
If the number of incrementations (N) was already contained in dataset HAVE (rather than in a separate dataset), a simple
set have;
would replace the MERGE and BY statements and HAVE would not need to be sorted by ID.
To obtain a dataset consisting of the newly created observations alone, just delete the first of the two OUTPUT statements.