For the sake of simplification, let's say S1 is the segmentation grid question. There are 5 items in S1, so they would be S1_r1 to S1_r5. Each can take a value from 1 to 5 (grid scale). For the segmentation algorithm, we need to calculate each segment score using formulas like these below (multiplying the answer value by a numeric coefficient and adding them across the question items) and save the scores in the dataset as independent variables:

Seg1= (S1_r1*0.11)+ (S1_r2*0.12) +(S1_r3*0.13)+ (S1_r4*0.14)+ (S1_r5*0.15)

Seg2= (S1_r1*0.21)+ (S1_r2*0.22) +(S1_r3*0.23)+ (S1_r4*0.24)+ (S1_r5*0.25)

Seg3= (S1_r1*0.31)+ (S1_r2*0.32) +(S1_r3*0.33)+ (S1_r4*0.34)+ (S1_r5*0.35)

Seg4= (S1_r1*0.41)+ (S1_r2*0.42) +(S1_r3*0.43)+ (S1_r4*0.44)+ (S1_r5*0.45)

Seg5= (S1_r1*0.51)+ (S1_r2*0.52) +(S1_r3*0.53)+ (S1_r4*0.45)+ (S1_r5*0.55

Then we need to compare the scores from Seg1, Seg2, Seg3, Se4, Seg5 and save a value from 1 to 5 in a flag variable, we call Segment for each respondents. So if Seg1 has the highest score value of all, then the assigned value would be 1 in the Segment variable. If Seg2 has highest score, then the assigned value would be 2, and so on.

What would be the code to set something like this in an efficient way? The actual segmentation question has 22 items, so I'm worried this will slow down or crash the survey while trying to assign the segment values)