I have my data like below:
data abc;
input alt_acc$ acc x;
datalines;
2123 001 1
2234 001 0
2345 001 0
Y123 002 4
Y345 002 7
Y513 002 0
Z123 002 6
L123 003 0
L234 003 1
;
run;
I want output like below:
-------------------------------
alt_acc Acc x
2123 001 1
2234 001 0
Y123 002 4
Y345 002 7
Y513 002 0
L123 003 0
I want the records till my x value is zero for all accounts(acc).
data abc; input alt_acc$ acc x; datalines; 2123 001 1 2234 001 0 2345 001 0 Y123 002 4 Y345 002 7 Y513 002 0 Z123 002 6 L123 003 0 L234 003 1 ; run; data want; set abc; by acc; retain is_zero; if first.acc then is_zero=0; if is_zero=0; if x=0 then is_zero=1; drop is_zero; run;
data abc;
input alt_acc$ acc x;
datalines;
2123 001 1
2234 001 0
2345 001 0
Y123 002 4
Y345 002 7
Y513 002 0
Z123 002 6
L123 003 0
L234 003 1
;
run;
data want;
set abc;
by acc;
retain _x;
if first.acc then _x =.;
if _x = . ;
if x=0 then _x=x;
drop _x;
run;
data abc; input alt_acc$ acc x; datalines; 2123 001 1 2234 001 0 2345 001 0 Y123 002 4 Y345 002 7 Y513 002 0 Z123 002 6 L123 003 0 L234 003 1 ; run; data want; set abc; by acc; retain is_zero; if first.acc then is_zero=0; if is_zero=0; if x=0 then is_zero=1; drop is_zero; run;
If zero occurs in X variable for an account, we need before record also for that account in the output
Looks like a case for the do until loop!
data want2;
do until(last.acc);
set abc;
by acc;
if not zerofound then
output;
if x=0 then
zerofound=1;
end;
drop zerofound;
run;
Y123 002 4
Y345 002 7
Above are not capturing
yes they are. Do proc print of want2 dataset.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.