I have a data set(call DATASET) as this (about 100000 observations):
X | Y | Z |
---|---|---|
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 |
I would like to do is to add one blank row above each row where Z=1.
to get this data set
X | Y | Z |
---|---|---|
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 |
Can anybody help and I would be very thankfull.
Best Silas, London
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
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;
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
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.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.