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;
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
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;
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!
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.