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

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

3 REPLIES 3
DavePrinsloo
Pyrite | Level 9
/* 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;

 

 

ballardw
Super User

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.

dapenDaniel
Obsidian | Level 7
Thanks!!!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 859 views
  • 0 likes
  • 3 in conversation