我有一个数据集需要根据动态值吹出一定数量的行。以下面的数据集为例:
DATA HAVE;
LENGTH ID $3 COUNT 3;
INPUT ID $ COUNT;
DATALINES;
A 4
B 3
C 1
D 2
;
RUN;
ID = A需要吹出4行,ID = B需要吹出3行等等。结果数据集看起来像这样(减去我有的一堆其他变量):
A 1
A 2
A 3
A 4
B 1
B 2
B 3
C 1
D 1
D 2
以下代码在某种程度上有效,但我无法动态设置&COUNT.
宏。我试图插入一个CALL SYMPUTX("COUNT",COUNT)
语句,这样当它遍历每一行时,计数就会被放入宏中,并且行会在该行数处被吹动。
** THIS CODE ONLY WORKS IF YOU SET COUNT= TO SOME VALUE **;
%MACRO LOOPOVER();
DATA WANT; SET HAVE;
DO UNTIL(LAST.ID);
BY ID;
%DO I=1 %TO &COUNT.;
COUNT = &I.; OUTPUT;
%END;
END;
RUN;
%MEND;
%LOOPOVER;
** THIS CODE DOESN'T WORK BUT I'M NOT SURE WHY?? **;
%MACRO LOOPOVER();
DATA WANT; SET HAVE;
DO UNTIL(LAST.ID);
BY ID;
CALL SYMPUTX("COUNT",COUNT); /* NEW LINE HERE */
%DO I=1 %TO &COUNT.;
COUNT = &I.; OUTPUT;
%END;
END;
RUN;
%MEND;
%LOOPOVER;
答案 0 :(得分:2)
没有必要使用宏。
data want(rename=(_count=count));
set have;
do i=1 to count;
_count=i;
output;
end;
drop count;
run;