Hello
For each customer ID I have 4 binary variables (with values 1/0) .
Each binary variable represent reason for failure.
It might happen that there are multiple reasons for failure .
My task is to create a new variable called "Vector" that will concatenate reasons for failure.
Expected values will be :
For ID=1 will get Null value
For ID=2 will get 'Reason A ,Reason B'
For ID=3 will get 'Reason B ,Reason D'
For ID=4 will get 'Reason D'
and so on
What is the way to do it please?
I know to concatenate but in this case need conditional concatenate
Proc format ;
value Reason1FFF
1='Reason A'
0='No Failure'
;
Run;
Proc format ;
value Reason2FFF
1='Reason B'
0='No Failure'
;
Run;
Proc format ;
value Reason3FFF
1='Reason C'
0='No Failure'
;
Run;
Proc format ;
value Reason4FFF
1='Reason D'
0='No Failure'
;
Run;
Data have;
Format Reason1 Reason1FFF. Reason2 Reason2FFF. Reason3 Reason3FFF. Reason4 Reason4FFF.;
Input ID Reason1 Reason2 Reason3 Reason4;
Cards;
1 0 0 0 0
2 1 1 0 0
3 0 1 0 1
4 0 0 0 1
5 0 0 0 0
6 1 1 1 1
7 0 0 1 1
8 0 0 0 0
9 1 0 0 0
;
Run;
Then call catx conditionally or change the formats to return missing instead of "no failure".
One way to go:
Proc format;
value Reason1FFF
1='Reason A'
other=' '
;
value Reason2FFF
1='Reason B'
other=' '
;
value Reason3FFF
1='Reason C'
other=' '
;
value Reason4FFF
1='Reason D'
other=' '
;
run;
data have;
format reason1 reason1fff. reason2 reason2fff. reason3 reason3fff. reason4 reason4fff.;
input id reason1 reason2 reason3 reason4;
cards;
1 0 0 0 0
2 1 1 0 0
3 0 1 0 1
4 0 0 0 1
5 0 0 0 0
6 1 1 1 1
7 0 0 1 1
8 0 0 0 0
9 1 0 0 0
;
run;
data want;
set have;
reason=catx(',',
put(reason1,reason1fff.),
put(reason2,reason2fff.),
put(reason3,reason3fff.),
put(reason4,reason4fff.)
);
run;
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.