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

I have been struggling with this.  I have sequential data that is missing some rows...and I need to fill them in.

 

I have:

ColA  ColB  ColC  Diff

A           B       C      1

A           B       C      2

A           B       C      4

D           E       F      1

D           E       F      3

D           E       F       4

 

I want:

ColA  ColB  ColC  Diff

A           B       C      1

A           B       C      2

A           B       C      3

A           B       C      4

D           E       F      1

D           E       F      2

D           E       F      3

D           E       F      4

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

See this:

data have;
input ColA $ ColB $ ColC $ Diff;
cards;
A B C 1
A B C 2
A B C 4
D E F 1
D E F 3
D E F 4
;
run;

data want (drop=ldif);
set have;
by cola colb colc;
ldif = lag(diff);
if not first.colc then do diff = ldif + 1 to diff;
  output;
end;
else output;
run;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

It's tough to generalize from your example.

 

Do you always want 4 rows? Or do you want the number of rows to be flexible based on the input data?

--
Paige Miller
Jarious
Calcite | Level 5

Flexible ..the length could go on for a while.  I wanted to show a simple example.  I have many missing rows and need to fill them in with the information from the previous row.

Kurt_Bremser
Super User

See this:

data have;
input ColA $ ColB $ ColC $ Diff;
cards;
A B C 1
A B C 2
A B C 4
D E F 1
D E F 3
D E F 4
;
run;

data want (drop=ldif);
set have;
by cola colb colc;
ldif = lag(diff);
if not first.colc then do diff = ldif + 1 to diff;
  output;
end;
else output;
run;
Astounding
PROC Star

I think this is what you are asking for:

 

data want;

set have;

prior_a = lag(a);

prior_b = lag(b);

prior_c = lag(c);

skip = dif(diff);

if skip <= 1 then output;

else do;

   temp_a = a;

   temp_b = b;

   temp_c = c;

   a = prior_a;

   b = prior_b;

   c = prior_c;

   do diff = (diff - skip + 1) to (diff - 1);

      output;

   end;

   a = temp_a;

   b = temp_b;

   c = temp_c;

   output;

end;

drop prior_: temp_: skip;

run;

 

It's untested, so if it needs a little tweaking just ask.

novinosrin
Tourmaline | Level 20

@Jarious What about the possibility of :

 

I have:

ColA  ColB  ColC  Diff

A           B       C      1

A           B       C      2

A           B       C      4

D           E       F      1

D           E       F      3

D           E       F       4

T           U       V       1 /*is this possible ?*/

T           U       V       5/*is this possible ?*/

 

novinosrin
Tourmaline | Level 20
data have;
input ColA $ ColB $ ColC $ Diff;
cards;
A B C 1
A B C 2
A B C 4
D E F 1
D E F 3
D E F 4
T U V 1 
T U V 5
;
run;

proc sort data=have out=_have ;
by ColA ColB ColC descending diff;
run;

data want;
set _have;
by ColA ColB ColC;
if first.ColA and first.Colb  and first.Colc then  do diff=1 to diff;
output;
end;
else return;
run;
Ksharp
Super User
data have;
input ColA $ ColB $ ColC $ Diff;
cards;
A B C 1
A B C 2
A B C 4
D E F 1
D E F 3
D E F 4
;
run;
data want;
 merge have have(rename=(cola=_cola colb=_colb colc=_colc diff=_diff) firstobs=2);
 output;
 if cola=_cola and colb=_colb and colc=_colc  then do;
  do i=diff+1 to _diff-1;
   diff=i;output;
  end;
 end;
 drop _: i;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 7 replies
  • 881 views
  • 1 like
  • 6 in conversation