|
To generate the desired the data set, one only needs the first and last observation for each id. So as long as a data set contain such information. It can be created automatically.
The follow will generate codes as,
NOTE: CALL EXECUTE generated line.
1 + data b;
2 + ID=1;
3 + do year=1998 to 2001;output;end;
4 + ID=2;
5 + do year=1994 to 2001;output;end;
6 + ID=3;
7 + do year=1997 to 2000;output;end;
8 + run;
*********************the output****************;
Obs ID year
1 1 1998
2 1 1999
3 1 2000
4 1 2001
5 2 1994
6 2 1995
7 2 1996
8 2 1997
9 2 1998
10 2 1999
11 2 2000
12 2 2001
13 3 1997
14 3 1998
15 3 1999
16 3 2000
data a;
input ID year;
cards;
1 1998
1 2001
2 1994
2 1995
2 1999
2 2001
3 1997
3 1999
3 2000
;
data _null_;
retain y0;
set a end=end;
by id;
if _n_=1 then call execute('data b;');
if first.id then do;
y0=year;
call execute(cat('ID=',ID,';'));
end;
if last.id then do;
call execute(cat('do year=',y0,' to ', year, ';output;end;'));
end;
if end then call execute('run;');
run;
proc print;run;
|