awk array index help

$ cat file.txt
A|X|20
A|Y|20
A|X|30
A|Z|20
B|X|10
A|Y|40

Summing up $NF based on first 2 fields,

$ awk -F "|" 'BEGIN {OFS="|"}
{ sum[$1 OFS $2] += $NF }
END { for (f in sum) print f,sum[f] }
' file.txt

o/p:

A|X|50
A|Y|60
A|Z|20
B|X|10

Works !!

Wondering if we can assign the "$1 OFS $2" part in some variable so that

$ var="$1 OFS $2"

And something like this will work 

$ awk -v x=$var -F "|" 'BEGIN {OFS="|"}
{ sum[x] += $NF }
END { for (f in sum) print f,sum[f] }
' file.txt

Presently its not working. The reason I have posted this : I have to sum based on a lot of fields and I want to make it kind of configurable outside awk. Please.
$ var="$1 OFS $2"

This won't do it, because in this context $1 and $2 are shell's positional parameters, ( possibly null values ), and OFS is just a string with no special value, something that you didn't mean to do.

You can try something like the following...

awk -v x=1 -v y=2  -F'|' '{ sum[ $x FS $y ] += $NF }  END { for (f in sum) print f, sum[f] }'  OFS='|' file

A|X|50
A|Y|60
A|Z|20
B|X|10

... and assign whatever integer values to x and y.

Rubin,
My question was something like this.

I am trying to sum a few fields based on first 4 fields, something like this

.....
{  A1[$1 OFS $2 OFS $3 OFS $4 ] += $7;A2[$1 OFS $2 OFS $3 OFS $4 ] += $8;A3[$1 OFS $2 OFS $3 OFS $4 ] += $9 ..................

Since I have to repeat "$1 OFS $2 OFS $3 OFS $4" part everytime for all the fields I want to sum up, I just wanted to make it part of a variable, please help.

awk 'BEGIN{OFS=FS=","}
{
key=sprintf("%s"OFS"%s"OFS"%s",$1,$2,$3)
a1[key]+=$4
a2[key]+=$5
}
END{
for(i in a1)
{
	print i" "a1"  "a2
}
}' filename

Thank you very much summer_cherry.Its really helpful.

Could you please help me in making it a kind of configurable i.e.

Something like:

X will contain the fields based on which Y fields needs to be summed up; in the example you put above
X="1,2,3"
Y="4,5"
Thank you in advance.