BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mandyosu01
Obsidian | Level 7

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
A2000
A2001
B1998

 

Data want:

Company YearNew 
A 1997
A 1998
A 1999
A20002000
A 2001
A 2002
A 1998
A 1999
A 2000
A20012001
A 2002
A 2003
B 1995
B 1996
B 1997
B19981998
B 1999
B 2000


Thank you so much in advance!





1 ACCEPTED SOLUTION

Accepted Solutions
smantha
Lapis Lazuli | Level 10
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;

View solution in original post

3 REPLIES 3
smantha
Lapis Lazuli | Level 10
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;
r_behata
Barite | Level 11
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;
PGStats
Opal | Level 21

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;
PG
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1126 views
  • 2 likes
  • 4 in conversation