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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1044 views
  • 4 likes
  • 3 in conversation