Hi,
I have a dataset like below.
data have;
input firmID $ Year Value;
datalines;
A 1995 1
A 1996 1
A 1997 1
A 1998 1
B 1997 2
B 1998 2
B 1999 2
;
run;I would like to add one more year for each firm while keeping the value the same. The expected output is below.
data have;
input firmID $ Year Value;
datalines;
A 1995 1
A 1996 1
A 1997 1
A 1998 1
A 1999 1
B 1997 2
B 1998 2
B 1999 2
B 2000 2
;
run;
One way:
data have; input firmID $ Year Value; datalines; A 1995 1 A 1996 1 A 1997 1 A 1998 1 B 1997 2 B 1998 2 B 1999 2 ; run; data want; set have; by firmid; if last.firmid then do; output; year=year+1; output; end; else output; run;
Assumes the data is sorted by firmid and year. If not actually sorted by Firmid use:
by notsorted firmid;
but all of the firmid values better be one after the other.
An explicit OUTPUT statement writes to the data set when encountered and overrides the default "at the bottom of the data step". So you need the Else Output to get the unmodified output. The first output inside the do block writes the last record, then modifies the variable and writes the modified record.
/* untested */
proc sort data=have;
by firmid year;
run;
data want;
set have;
by firmid year;
output:
if last .firmid then do;
year=year+1;
output;
end;
run;
data have;
input firmID $ Year Value;
One way:
data have; input firmID $ Year Value; datalines; A 1995 1 A 1996 1 A 1997 1 A 1998 1 B 1997 2 B 1998 2 B 1999 2 ; run; data want; set have; by firmid; if last.firmid then do; output; year=year+1; output; end; else output; run;
Assumes the data is sorted by firmid and year. If not actually sorted by Firmid use:
by notsorted firmid;
but all of the firmid values better be one after the other.
An explicit OUTPUT statement writes to the data set when encountered and overrides the default "at the bottom of the data step". So you need the Else Output to get the unmodified output. The first output inside the do block writes the last record, then modifies the variable and writes the modified record.
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 lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.