我在运行以下代码时遇到错误。 &CNT
为50
,&vars
中包含列名。
每列都是1
到100
的一些值。我想选择每个列并检查以下条件(%if
语句),创建新变量并为其分配值(例如free
,partially free
和not free
)
option mlogic mprint;
%macro analysis();
DATA Q2;
SET Q1;
%do i=1 %to &CNT.;
%let segs =%scan(&VARS.,&i.," ");
%IF &SEGS.<=2.5 %THEN &SEGS._R="FREE";
%ELSE %IF (&SEGS.>2.5 AND &SEGS.<5.5) %THEN &SEGS._R="PARTLY FREE";
%ELSE %IF &SEGS.>=5.5 %THEN &SEGS._R="NOT FREE";
/*%PUT &segs.;*/
%end;
RUN;
%MEND;
%analysis();
这是我得到的输出:
SAS LOG ERROR:
MPRINT(ANALYSIS): DATA Q2;
MPRINT(ANALYSIS): SET Q1;
MLOGIC(ANALYSIS): %DO loop beginning; index variable I; start value is 1; stop value is 56; by value
is 1.
MLOGIC(ANALYSIS): %LET (variable name is SEGS)
MLOGIC(ANALYSIS): %IF condition &SEGS.<=2.5 is FALSE
MLOGIC(ANALYSIS): %IF condition (&SEGS.>2.5 AND &SEGS.<5.5) is FALSE
MLOGIC(ANALYSIS): %IF condition &SEGS.>=5.5 is TRUE
MLOGIC(ANALYSIS): %PUT &segs.
yr1960
MLOGIC(ANALYSIS): %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(ANALYSIS): %LET (variable name is SEGS)
MLOGIC(ANALYSIS): %IF condition &SEGS.<=2.5 is FALSE
MLOGIC(ANALYSIS): %IF condition (&SEGS.>2.5 AND &SEGS.<5.5) is FALSE
MLOGIC(ANALYSIS): %IF condition &SEGS.>=5.5 is TRUE
***NOTE: Line generated by the macro variable "SEGS".
1 yr1961_R
--------
22
答案 0 :(得分:1)
您在宏处理器内部与数据步骤内部混淆IF条件。我想这就是你想要的。
%macro analysis();
DATA Q2;
SET Q1;
%do i=1 %to &CNT.;
%let segs =%scan(&VARS.,&i.," ");
IF &SEGS.<=2.5 THEN &SEGS._R="FREE";
ELSE IF (&SEGS.>2.5 AND &SEGS.<5.5) THEN &SEGS._R="PARTLY FREE";
ELSE IF &SEGS.>=5.5 THEN &SEGS._R="NOT FREE";
/*%PUT &segs.;*/
%end;
RUN;
%MEND;
%analysis();
宏为您编写代码。您正在将变量Name与常量值进行比较(使用字符串排序,不能少),而不是使用数字将变量值与常量值进行比较。