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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: