BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Srinivas1516
Fluorite | Level 6

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). 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

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;
Ksharp
Super User
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;
Srinivas1516
Fluorite | Level 6

If zero occurs in X variable for an account, we need before record also for that account in the output

tarheel13
Rhodochrosite | Level 12

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;
Srinivas1516
Fluorite | Level 6

Y123 002 4
Y345 002 7

 

 

Above are not capturing

tarheel13
Rhodochrosite | Level 12

yes they are. Do proc print of want2 dataset. 

tarheel13_0-1656422617350.png

 

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1166 views
  • 1 like
  • 4 in conversation