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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 778 views
  • 0 likes
  • 3 in conversation