BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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;
  

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 646 views
  • 4 likes
  • 3 in conversation