First, a few notes about the technical points in the other answer - ie, "Where the problem is not directly coming from, although both are examples of poor coding."
&i is indeed accessible here, although I would suggest it is poor style to use it the way you do. Macro variables that are relied upon in interior macros should be defined as macro parameters; that makes it clear where they came from. However, technically, this isn't wrong, see this:
%macro caller;
%do i=1 %to 5;
%called;
%end;
%mend;
%macro called;
%put &i;
%mend called;
%caller;
However, it would be better to make i a paramter, such as %macro called(i=);
, to make your macro more clear, and more reusable.
Second, lack of quotes is in fact not the direct problem, although again it does point to an issue and is a solution in a way. SAS does convert the numerics in that to a character value - otherwise you'd get a very different error message; however, it does so in a way that is not helpful. The most similar implementation of what you did is to add compress
around it. That is because the problem is how SAS converts numerics to text; &i
is a number (1
in your example). It needs to be converted to "1"
, and instead it is converted using best12.
to " 1"
. That is a problem.
hh.output (dataset: compress('_'||&i||put(year, best.-L))) ;
That works. A better implementation would be to intentionally convert to a character value. Macro parameters are very easy to convert: just add " "
around them.
hh.output(dataset: cats('_',"&i.",year);
cats
strips all spaces off, and makes the -L
unnecessary. It would work just as well with &i
, although it's certainly better to add quotes.
I would add that you might consider why you're subsetting these. I don't think there's anything conceptually wrong with doing so, but odds are if you are doing something by year, you can use by year
and get away with not subsetting them - keeping them in one dataset per treatment group (and perhaps even by group year
?). Further, you might be able to do this in fewer steps. What are you going to do, finally? Let's say you had one dataset for each group/year. What code would you then run? It may be that you can write that in one or a few steps without breaking out 48x14 datasets, which is probably not efficient. If you're interested in finding out, start a new question with the details of what you'd like to do with just a pair of datasets.