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

Hi,

suppose I have the following table:

companyyearcond1cond2
A200612
A200812
A201034
B20001122
B201000

If 2 adjacent years for the same company have the SAME conditions, then all the years (if they are missing) bw these adjacent years should be created also having those conditions - in this case for comp A create year 2007 (1,2).

If adjacent years for the same company have different conditions then keep as is.

So the want table is like this

companyyearcond1cond2
A200612
A200712
A200812
A201034
B20001122
B201000

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Daniel,

What you want to accomplish only requires incorporating one condition of the code that data_null_ suggested. e.g.:

data need;

   set have end=eof;

   by company cond1 cond2 notsorted;

   if not eof then set have(firstobs=2 keep=year rename=year=nextyear); 

   if not last.cond2 then do year=year to nextyear-1;

      output;

   end;

   else output;

   drop nextyear;

run;

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

Daniel,

What you want to accomplish only requires incorporating one condition of the code that data_null_ suggested. e.g.:

data need;

   set have end=eof;

   by company cond1 cond2 notsorted;

   if not eof then set have(firstobs=2 keep=year rename=year=nextyear); 

   if not last.cond2 then do year=year to nextyear-1;

      output;

   end;

   else output;

   drop nextyear;

run;

Haikuo
Onyx | Level 15

An alternative to looking ahead technique. This is looking-back.

data have;

      input company$  year cond1 cond2;

     cards;

A    2006 1    2

A    2008 1    2

A    2010 3    4

A    2011 3    4

B    2000 11   22

B    2010 0    0

;

data want;

     set have;

     by company year;

     _lagY=lag(year);

     _lagC1=lag(cond1);

     _lagC2=lag(cond2);

     if first.company then

           do;

                i=year;

                output;

           end;

     else

           do;

                if cond1=_lagC1 and cond2=_lagC2 then

                     do;

                           do i=_lagY+1 to year;

                                output;

                           end;

                     end;

                else

                     do;

                           i=year;

                           output;

                     end;

           end;

           drop year _:;

           rename i=year;

run;

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 834 views
  • 3 likes
  • 3 in conversation