SAS recursively resolves everything that starts with a "%" or a "&". For a %let statement it stores everything to the right of the equal sign into the macro variable as a string (macro variables are always strings).
For "%let j= &j. + 1;" it does the following:
- Resolve the "&j" so that you're left with "%let j= 1 + 1;";
- Since SAS doesn't see anything starting with a % or a & to the right of the equal sign it sets j to "1 + 1".
In order to increment j you need to change the line to "%let j=%eval(&j + 1);". So the steps become the following:
- Resolve the innermost "%" or "&" which is "&j", so the line becomes "%let j=%eval(1 + 1);".
- Resolve the "%eval(1 + 1)" so the line becomes "%let j=2;".
- Set j to "2".
Also the syntax for getting the value of a variable where the name is defined by another variable is &&n&j so &(n&j) wouldn't work.
Having said that, an easier way to do what you're trying to do is the following:
%do j=1 %to %sysfunc(countw(&sdate1));
proc sql;
select distinct compno into : n&j separated by ' '
from compno_date_list where sdate=%scan(&sdate1, &j);
quit;
%put &&n&j;
%end;
One thing to note is if you're processing a large dataset you might want to try to avoid going over it for each date by either using multiple output data sets in a data step or using hash objects if possible since this seems like a very time consuming task.