Use Prepend instead of PrependTo.
Prepend[#, header] & /@ {data1, data2, data3, data4}
(* { {{A,B},{1,1},{1,1}},
{{A,B},{2,2},{2,2}},
{{A,B},{3,3},{3,3}},
{{A,B},{4,4},{4,4}} } *)
The difference is that PrependTo not only creates a new list with a prepended element but it also assigns the result back to the variable in the first argument:
myList = {1, 2, 3};
PrependTo[myList, "abc"]
(* {"abc", 1, 2, 3} *)
myList
(* {"abc", 1, 2, 3} *)
Contrast this to Prepend, which does not update the variable:
myList = {1, 2, 3};
Prepend[myList, "abc"]
(* {"abc", 1, 2, 3} *)
myList
(* {1, 2, 3} *)
The first argument to PrependTo must be an assignable variable:
PrependTo[{1, 2, 3}, "abc"]
(* PrependTo::rvalue:
{1,2,3} is not a variable with a value, so its value cannot be changed. *)
... but Prepend makes no such demand:
Prepend[{1, 2, 3}, "abc"]
(* {"abc", 1, 2, 3} *)
For this reason, Prepend is more suitable for use with higher-level functions such as Map.
Responding to the Updated Question
If we wish to update the original variables, we need to prevent the evaluation of the individual variables. For example:
Scan[Function[Null, PrependTo[#, {"A", "B"}], HoldFirst], Hold[data1, data2, data3, data4]]
data1
(* {{"A","B"},{1,1},{1,1}} *)
data2
(* {{'A","B"},{2,2},{2,2}} *)
data3
(* {{"A","B'},{3,3},{3,3}} *)
data4
(* {{"A","B"},{4,4},{4,4}} *)
Note the use of Hold to prevent the variable list from being evaluated and the use of the HoldFirst attribute on the pure function for the same purpose.
A Plague Of (and On) Held Expressions
All of this holding seems very complicated for such a simple operation. This is unfortunately a common occurrence when working with held expressions. Since the first argument to PrependTo must be held, our mapped operator must hold that argument as well. Which, in turn, means that the list over which we wish to iterate must be held as well. And so on up the chain.
Held expressions are contagious, and once we want to start iterating over them they spread like the plague. In this simple example, there were two prospects for error: if we had omitted the Hold or HoldFirst. But in more complex programs the chance for so-called "evaluation leaks" to spring up grows quickly.
All of the built-in destructive operators (like PrependTo, Set, SetDelayed etc) require held arguments. In other languages, programs involving destructive operators are harder to reason about on account of their side-effects (I found the bug but... the original values are now overwritten and lost. whoops). But in Mathematica, destructive operators have the additional complication of potential evaluation leaks and hold-contagion.
This is why the Mathematica documentation stresses the transformative and functional programming styles -- precisely to avoid such complexity.
In the case at hand, the desired list can be created directly in a functional style:
Thread[{"data1","data2","data3","data4"} -> Prepend[header] /@ {data1,data2,data3,data4}]
(* { "data1" -> {{"A", "B"}, {1, 1}, {1, 1}}
, "data2" -> {{"A", "B"}, {2, 2}, {2, 2}}
, "data3" -> {{"A", "B"}, {3, 3}, {3, 3}}
, "data4" -> {{"A", "B"}, {4, 4}, {4, 4}}
} *)
There is no need to overwrite the original variables. (in earlier versions of Mathematica the operator mapped may need to be written as Prepend[#, header]&)
Prependinstead ofPrependTo. The latter tries to reset the value of a symbol given as its argument, and that fails when you try to reset the value of a list, which is not a symbol. See the Details section of the docs forPrependTo. As a side note, you should also get a number of errors during your evaluation, which have a pretty good hint at why your original code does not work. If you don't see them, you might want to check that you haven't silenced those messages somehow. – MarcoB Mar 08 '17 at 00:02Prependbut how to make it work with/@(in pure function form) ? – Leo1215 Mar 08 '17 at 00:11