1. The temporary variable used within the (for:) macro only exists within the scope of the macro's associated hook, and that hook no longer exists after the (for:) macro is finished creating your links, and this is why you are getting that error message.
Using a story variable instead of a temporary variable would not solve this issue because variables contained within the associated hook of macros that require end-user interaction (like the (link:) macro does) are not evaluated until after the interaction occurs, and at the time of that interaction the variable will equal whatever it did at the end of the (for:) macro's looping.
To get around this issue you generally use a (print:) macro to dynamically create the macro & associated hook you need to include the value in.
(set: $array to (array: 1,2,3,4,5))
(for: each _item, ...$array)[
(print: "(link: 'item: " + (text: _item) + "')[(set: $var to " + (text: _item) + ")(goto: 'Next')]")
]
2. The (set:) macro is assigning a clone of the original data-map object to the $youritem variable, so the object referenced in the $itemlist variable and the object referenced in the $youritem are no longer the same object even though the two objects have the 'same' value. Cloning of an object also occurs when (set:) is used to modify a property/element of an object.
The following example demonstrate this cloning process and the disassociation of the two copies of the original data-map object:
{
(set: $itemlist to (ds:
(dm: "name", "item1", "value", 0, "useable", false),
(dm: "name", "item2", "value", 0, "useable", false)
))
(for: each _item , ...$itemlist)[
(if: _item's name is "item1")[
(set: $youritem to _item)
]
]
}
(set: $youritem's value to 1)
itemlist: (print: $itemlist)
youritem value: (print: $youritem's value)
To get around this issue I suggest that instead of assign an object reference to the $youritem variable that you assign an identifier to the original data-map stored within the $itemlist variable, this will require you to change the data-type of the collection object being used within $itemlist from a data-set to one that allows referencing it's contents via either an index or a property name. (eg. an Array or a (Data-)Map)