Hello
I have a raw data that contain 6 observations for each customer (during follow up period of 6 months).
For each customer there are 3 fields: ID, month, Ind
Ind is a binary variable that get value 1 if customer in fail status and 0 otherwise.
For all customers the value of var "ind" in first month is 0 (customer start is status with no failure)
The task is to create a new binary variable that will receive value 1 in first position where customer is in failure
Expected values for customer1 are:
1 1801 0 0
1 1802 0 0
1 1803 1 1
1 1804 1 0
1 1805 0 0
1 1806 1 0
Expected values for customer2 are:
2 1801 0 0
2 1802 1 1
2 1803 1 0
2 1804 1 0
2 1805 1 0
2 1806 1 0
Can anyone suggest a code that create the new field?
Data raw_tbl;
input ID date Ind;
cards;
1 1801 0
1 1802 0
1 1803 1
1 1804 1
1 1805 0
1 1806 1
2 1801 0
2 1802 1
2 1803 1
2 1804 1
2 1805 1
2 1806 1
;
run;
Data raw_tbl;
input ID date Ind;
cards;
1 1801 0
1 1802 0
1 1803 1
1 1804 1
1 1805 0
1 1806 1
2 1801 0
2 1802 1
2 1803 1
2 1804 1
2 1805 1
2 1806 1
;
run;
data want;
set raw_tbl;
by ID;
retain found;
if first.id then found=0;
flag=0;
if Ind ne 0 and found=0 then do;
flag=Ind;
found=1;
end;
drop found;
run;
Data have;
input ID date Ind;
cards;
1 1801 0
1 1802 0
1 1803 1
1 1804 1
1 1805 0
1 1806 1
2 1801 0
2 1802 1
2 1803 1
2 1804 1
2 1805 1
2 1806 1
;
run;
proc sql;
create table want as
select *,(min(date)=date)*(ind=1) as new
from have
group by id,ind
order by id,date;
quit;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.