BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Amr_Alaaeldin
Obsidian | Level 7

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 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Amr_Alaaeldin
Obsidian | Level 7
Thanks For Quick Reply

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 3 replies
  • 1558 views
  • 4 likes
  • 3 in conversation