Split a single file into several others basing on the last column

Hi folks,
Happy new year.
I have a file 'filename' that i wd like to split basing on the contents in the last column.
The 'filename' content looks like

256772744788,9,11
256772744805,9,11
256772744792,9,11
256775543055,10,12
256782625357,9,12
256772368953,10,13
256772627735,10,13
256782853530,9,14
256782676830,9,28
256392752548,9,37
256782610403,9,38
256775590939,9,38

and i would like output files like 'file11', 'file12', 'file13', 'file 14', 'file28' etc with content

[cat file11]

256772744788,9,11
256772744805,9,11
256772744792,9,11

[cat file38]
256782610403,9,38
256775590939,9,38

I used this for loop but only created empty files.
[for (( i = 11; i <= 115; i++ )); do grep ',$i' cugnums.csv > file$i;done]

The last column has integer values from 11 to 115

Something like this?

awk -F, '{print > "file" $3}' OFS="," file

Solution using awk:

$cat split.awk
#! /usr/bin/awk

{
str="test -f file"$3;
print str
rval=system(str);
if (rval == 1){
        str="echo "$0 "> file"$3
        system(str);
}
else {
        str="echo "$0 ">> file"$3
        system(str);
}
}

Execution:

$awk -F ',' -f split.awk inputfilename

This script creates files (file11, file12 etc.) in the current directory.

use NF will be better.

awk -F, '{print > "file" $NF}' OFS="," cugnums.csv

---------- Post updated at 07:25 PM ---------- Previous update was at 07:23 PM ----------

Franklin52's script is best solution.

But if you still want to use your own code, fix as below:

for (( i = 11; i <= 115; i++ )); do grep ",$i$" cugnums.csv > file$i;done