the aim of this Question is Counting number of Delinquency during last 12 month But the trick is how to start cumulative count from right to left when find 1 and stop Counting when find 0 Then start counting again when it finds 1
like row number 1 result part .
example :
Data test ;
input A1 A2 A3 A4 A5 A6 A7 A8;
Datalines ;
1 0 1 1 0 1 1 1
1 1 0 0 0 1 1 0
1 1 1 1 1 1 0 0
0 0 0 1 0 1 1 1
1 0 1 1 1 0 0 0
0 1 0 1 0 1 1 1
;
Run ;
The result should be :
B1 B2 B3 B4 B5 B6 B7 B8
1 0 2 1 0 3 2 1
2 1 0 0 0 2 1 0
6 5 4 3 2 1 0 0
0 0 0 1 0 3 2 1
1 0 3 2 1 0 0 0
0 1 0 1 0 3 2 1
Your help is highly appreciated
Just to add to @Kurt_Bremser, you don't actually need the counter, just go in reverse from second to end one and then add next cell to the current:
Data test ;
input A1 A2 A3 A4 A5 A6 A7 A8;
Datalines ;
1 0 1 1 0 1 1 1
1 1 0 0 0 1 1 0
1 1 1 1 1 1 0 0
0 0 0 1 0 1 1 1
1 0 1 1 1 0 0 0
0 1 0 1 0 1 1 1
;
Run ;
data want;
set test;
array vals{*} a:;
do i=dim(vals)-1 to 1 by -1;
if vals{i}=1 then vals{i}=vals{i+1}+1;
end;
run;
Declare array for the A and B variables.
Set a counter variable to zero.
In a do loop that counts down from dim(array) to 1, do
- if A(i) is zero, set counter to zero
- if A(i) is 1, add 1 to counter
- set B(i) to counter
Just to add to @Kurt_Bremser, you don't actually need the counter, just go in reverse from second to end one and then add next cell to the current:
Data test ;
input A1 A2 A3 A4 A5 A6 A7 A8;
Datalines ;
1 0 1 1 0 1 1 1
1 1 0 0 0 1 1 0
1 1 1 1 1 1 0 0
0 0 0 1 0 1 1 1
1 0 1 1 1 0 0 0
0 1 0 1 0 1 1 1
;
Run ;
data want;
set test;
array vals{*} a:;
do i=dim(vals)-1 to 1 by -1;
if vals{i}=1 then vals{i}=vals{i+1}+1;
end;
run;
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.