Hello SAS users,
I want to create a dataset that shows three years before and two years after a certain year for each of the observations in my sample. Also there are some overlapped years in newly created column, and I need to keep all overlap years presented separately.
Data have:
| Company | Year |
| A | 2000 |
| A | 2001 |
| B | 1998 |
Data want:
| Company | Year | New |
| A | 1997 | |
| A | 1998 | |
| A | 1999 | |
| A | 2000 | 2000 |
| A | 2001 | |
| A | 2002 | |
| A | 1998 | |
| A | 1999 | |
| A | 2000 | |
| A | 2001 | 2001 |
| A | 2002 | |
| A | 2003 | |
| B | 1995 | |
| B | 1996 | |
| B | 1997 | |
| B | 1998 | 1998 |
| B | 1999 | |
| B | 2000 |
Thank you so much in advance!
data Final;
set input_data;
start_yr=year-3;
end_yr=year+2;
year2=year;
do new = start_yr to end_yr;
if year != new then year=year2;
else year=' ';
output;
end;
drop year2 start_yr end_yr;
run;
data Final;
set input_data;
start_yr=year-3;
end_yr=year+2;
year2=year;
do new = start_yr to end_yr;
if year != new then year=year2;
else year=' ';
output;
end;
drop year2 start_yr end_yr;
run;
Data have;
input Company $ Year;
cards;
A 2000
A 2001
B 1998
;
run;
data want(rename=(year_=year));
set have;
do new=(Year-3) to (year +2);
if new=year then
year_=year;
else year_=.;
output;
end;
drop year;
run;
Adapted from @r_behata :
Data have;
input Company $ Year;
datalines;
A 2000
A 2001
B 1998
;
data want(rename=(year=_year));
set have;
do new = _year-3 to _year +2;
if new = _year
then year = _year;
else call missing(year);
output;
end;
drop _year;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.