Splitting file according to range specified in another file

Hi all,

I would like to split a big data file according to range specified in another file.
The 1st column in the data file has to be matched with the second column of range file.The second column of the data file must lie in between the range specified by columns 3 and 4 of range file. The outputs must have the row names as 1st column of range file. >= and <= conditions are to be used. Please help.

Data file

Range File

Outputs 2 files
G1.txt

G2.txt

awk 'NR==FNR{a[$1]=$2;lower[$1]=$3;higher[$1]=$4;next}{for (i in a) if (a==$1&&$2>=lower&&$2<=higher) print $0 > i".txt"}' range data