%Let 语句对于这类事情不是很灵活。
但是, CALL SYMPUT可以在数据步骤的中间使用来设置宏变量,并且可能会有所帮助。
怎么样:
data myReg;
INPUT Name $;
DATALINES;
Alex
Alex
Ben
Ben
Ben
Calvin
Calvin
Calvin
Calvin
;
run;
proc sort data=myReg; by name;run;
data MakeSomeMacroVars;
set myReg end=LastRow;
by name;
length Allnames $30000;*Variable for space separated list of names;
retain AllNames ' ';
cnt+1;
if last.name THEN DO;
*Create a macro variable NUM_Alex or similar, with value equal to cnt;
CALL SYMPUT (cats('NUM_',name),cats((put(cnt,12.))));
cnt=0;
*Add name to space separated list;
Allnames = catx(" ",AllNames, name);
END;
if LastRow THen do;
Call Symput ('AllNames',cats(compbl(AllNames)));
END;
RUN;
%Put Alex has --&NUM_Alex--; * --> 2;
%Put Ben has --&NUM_Ben--; * --> 3;
%Put List of names is: --&AllNames--; * --> Alex Ben Calvin;