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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.