# Categorial variable from numeric score

Hi. I would like to create a variable called “category” which takes the value “Normal” if the score is between 0 and 5, “Mild” if between 10 and 13, “Moderate” if between 14 and 20, “Severe” if between 21 and 27, and “Extremely severe” if it’s over 28. Thank for your help.
I try this code
(2scr1_depression_score_222 <= 9) ? 0 : (2scr1_depression_score_222 >= 10 && 2scr1_depression_score_222 <= 13) ? 1 : (2scr1_depression_score_222 >= 14 && 2scr1_depression_score_222 <= 20) ? 2 : (2scr1_depression_score_222 >= 21 && 2scr1_depression_score_222 <= 27) ? 3 : (2scr1_depression_score_222 >= 28) ? 4

(2 * scr1_depression_score_222 <= 9) ? 0 :
((2 * scr1_depression_score_222 >= 10 && 2 * scr1_depression_score_222 <= 13) ? 1 :
((2 * scr1_depression_score_222 >= 14 && 2 * scr1_depression_score_222 <= 20) ? 2 :
((2 * scr1_depression_score_222 >= 21 && 2 * scr1_depression_score_222 <= 27) ? 3 :
((2 * scr1_depression_score_222 >= 28) ? 4 : -1) // Valeur par défaut si aucune condition n’est remplie
)
)
);
Work fine

You’re almost there. Try something like this:

``````// normal
(2scr1_depression_score_222 <= 9) ? 0 :
// mild
(2scr1_depression_score_222 >= 10 && 2scr1_depression_score_222 <= 13) ? 1 :
// moderate
(2scr1_depression_score_222 >= 14 && 2scr1_depression_score_222 <= 20) ? 2 :
// severe
(2scr1_depression_score_222 >= 21 && 2scr1_depression_score_222 <= 27) ? 3 :
// extreme
(2scr1_depression_score_222 >= 28) ? 4 :
// undefined
-1
``````

Whenever you use the ternary operator, you need 3 parts: if (i.e., part before `?`), then (part after `?` but before `:`, else (part after `:`). Your expression in your initial post was missing the `else` part for the last case.

Use these 3 calculated variables, assuming your calculated score is integer (if not do round it in the first one):

``````score = 2 * scr1_depression_score_222
c=Score.InRange(0,28) ? LT[(int)score].cat : -1 // category
// string corresponding to the category as requested:
s=new[]{"Normal","Mild","Moderate","Severe","Extremely severe"}[(int)c]
``````

The appropriate lookup table LT in this case is:

``````rowcode	cat
0	0
1	0
2	0
3	0
4	0
5	0
6	0
7	0
8	0
9	0
10	1
11	1
12	1
13	1
14	2
15	2
16	2
17	2
18	2
19	2
20	2
21	3
22	3
23	3
24	3
25	3
26	3
27	3
28	4
``````