Help using Base SAS procedures

Adding rows by condition

Reply
Contributor
Posts: 30

Adding rows by condition

I have a data set(call DATASET) as this (about 100000 observations):

XYZ
aj0
bk1
cl0
dm0
eo1
fp0
gq0
hr0
is1

I would like to do is to add one blank row above each row where Z=1.

to get this data set

XYZ
aj0
bk1
cl0
dm0
eo1
fp0
gq0
hr0
is1

Can anybody help and I would be very thankfull.

Best Silas, London

Trusted Advisor
Posts: 1,137

Re: Adding rows by condition

Posted in reply to silasskovsbo

Please try,

data have;

  infile cards truncover;

  input x :$1. y:$1. z;

cards;

a    j    0

b    k    1

c    l    0

d    m    0

e    o    1

f    p    0

g    q    0

h    r    0

i    s    1

;

proc sort data=have;

  by x y;

run;

data want;

  set have;

  output;

by x y;

if z=1 then output;

run;

data want_;

  set want;

  by x y;

  if first.x and z=1 then call missing(x,y,z);

run;

Thanks,

Jag

Thanks,
Jag
Frequent Contributor
Posts: 106

Re: Adding rows by condition

Posted in reply to silasskovsbo

data have;

input x $ y $ z;

id+1;

cards;

a j 0

b k 1

c l 0

d m 0

e o 1

f p 0

g q 0

h r 0

i s 1

;

proc sort data=have;by descending id;run;

data have;

set have(drop=id);

if z=1 then do; idnew+1;output;

call missing(x,y,z);end;

else;idnew+1;output;

run;

proc sort data=have out=want(drop=idnew);by descending idnew;

run;

Respected Advisor
Posts: 3,156

Re: Adding rows by condition

Posted in reply to silasskovsbo

A typical look-ahead:

data have;

  infile cards truncover;

  input x :$1. y:$1. z;

cards;

a    j    0

b    k    1

c    l    0

d    m    0

e    o    1

f    p    0

g    q    0

h    r    0

i    s    1

;

data want;

  set have;

  set have(firstobs=2 rename=(z=_z) keep=z) have (obs=1 drop=_all_);

  output;

if _z=1 then do; call missing (of _all_); output;end;

drop _z;

run;

Haikuo

Ask a Question
Discussion stats
  • 3 replies
  • 203 views
  • 0 likes
  • 4 in conversation