BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

For each customer I have follow up of 5 periods.

I want to create a new column with information of number of periods it took to customer to arrive failure.

For example:

customer1: 3

customer2: 1
customer3: 2

customer4: 0

How can I calculate it please?

 

Data RawData;
input id Follow1  Follow2 Follow3 Follow4 Follow5;
cards;
1 0 0 1 1 1
2 1 1 1 0 0
3 0 1 0 1 1
4 0 0 0 0 0
;
run;
2 REPLIES 2
Kurt_Bremser
Super User

Another nice case for transposing to long format:

Data RawData;
input id Follow1  Follow2 Follow3 Follow4 Follow5;
cards;
1 0 0 1 1 1
2 1 1 1 0 0
3 0 1 0 1 1
4 0 0 0 0 0
;
run;

proc transpose data=rawdata out=trans (where=(col1 ne 0));
by id;
var follow:;
run;

data want;
merge
  rawdata
  trans (in=b)
;
by id;
if first.id;
if b
then first_failure = input(substr(_name_,7,1),1.);
else first_failure = 0;
keep id follow: first_failure;
run;

proc print data=want noobs;
run;

Result:

                                                              first_
id    Follow1    Follow2    Follow3    Follow4    Follow5    failure

 1       0          0          1          1          1          3   
 2       1          1          1          0          0          1   
 3       0          1          0          1          1          2   
 4       0          0          0          0          0          0   
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Something easy like:

data rawdata;
  input id follow1 follow2 follow3 follow4 follow5;
cards;
1 0 0 1 1 1
2 1 1 1 0 0
3 0 1 0 1 1
4 0 0 0 0 0
;
run;

data want;
  set rawdata;
  want=whichn(1,of follow:);
run;
  

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 2 replies
  • 1004 views
  • 4 likes
  • 3 in conversation