Batch scripting

Trying to understand how batch passes by reference and
uses the %~# where # is a number.
Tried following the examples in several tutorials but
whatever I do seems to fly in the face of the those
examples. Trying to do this without getting into
use of disabling of expansion.
The test input file is:
XXXX any text at all
Space separated text for n-number of lines
YYYY
Where XXXX is the first lines unique id and YYYY is
always the same last line for all text sets.
The file is meant to be read line by line.
Here is what I have so far and output seems to indicate
a problem with the "set" command in passing values.
This is on a Windows 8.1 system.

You are using "set /a" incorrectly. Where you are not evaluating anything, just setting initial conditions, use "set". "set /a knt=knt+1" is the only correct use because you want it to evaluate - add 1 to knt - although it's quicker to do "set /a knt+=1".
You have not used a "goto :EOF" before your label "doit". This will cause the batch file to loop weirdly.

if you want to look into "passing by reference" the main source I have found are the example subroutines of Ritchie Lawrence. Here are the Date and Time ones. They have a tiny calling routine at the top of each.

Since he has compressed out lots of spaces, you need to read them Vewwy Carefuwwy.