宏变量:未解析的表观符号引用

时间:2014-05-08 09:25:51

标签: sas sas-macro

我在以下(部分)宏中有一个未解析的宏变量的问题:

DATA _NULL_;
  SET TempVarFormat END=Last;
  LENGTH FormatValues $10000;
  RETAIN FormatValues;
  IF &OnlyNumeric = 1 THEN
    FormatValues = CATX(" ",FormatValues,STRIP(LookUpValue)||
    " = "||CATQ("A",TRIM(LookupDescription)));
  ELSE
    FormatValues = CATX(" ",FormatValues,CATQ("A"
    ,LookUpValue)||" = "||CATQ("A"
    ,TRIM(LookupDescription)));
  Test = STRIP(FormatValues);
  PUT Test /* To test buildup of variable */;
  IF Last THEN CALL SYMPUT('FormatValuesM',STRIP(FormatValues));
  IF Last THEN CALL SYMPUT('DataCollectionFK',DataCollectionFK);
RUN;
/* Make format with PROC FORMAT*/
%IF &OnlyNumeric = 1 %THEN %DO;
  PROC FORMAT LIB=WORK;
  VALUE DC&DataCollectionFK.A&AttributeFK.Format &FormatValuesM;
  RUN;
%END;
%ELSE %IF &OnlyNumeric = 0 %THEN %DO;
  PROC FORMAT LIB=WORK;
  VALUE $DC&DataCollectionFK.A&AttributeFK.Format &FormatValuesM;
  RUN;
%END;

我收到以下警告

Apparent symbolic reference FORMATVALUESM not resolved.

如果我查看日志& DataCollectionFK 已解决但& FormatValues不是

PROC FORMAT LIB=WORK; VALUE DC170A570Format &FormatValuesM;

有人可以建议吗?这让我疯了。

我在没有STRIP()函数的情况下对它进行了测试,并用PUT替换了CALL SYMPUT以查看变量是否被赋值。一切正常。

日志副本(根据评论中的要求)

4         +
      DATA _NULL_; SET TempVarFormat END=Last; LENGTH

5         + FormatValues $10000; RETAIN FormatValues; IF 1 = 1 THEN FormatValues = CATX(" 
",FormatValues,STRIP(LookUpValue)|| " = "||CATQ("A",TRIM(LookupDescription))); ELSE 
FormatValues = CATX(" ",FormatValues,CATQ("A" ,LookUpValue)||" = "||CATQ("A" ,TRIM
6         +(LookupDescription))); Test = STRIP(FormatValues); PUT Test ; IF Last THEN CALL 
SYMPUT('DataCollectionFK',DataCollectionFK); IF Last THEN CALL SYMPUT('FormatValuesM',Test); 
RUN;

NOTE: Numeric values have been converted to character values at the places given by: 
      (Line):(Column).
      6:107   
1 = "Ja"
1 = "Ja" 0 = "Nee"
NOTE: There were 2 observations read from the data set WORK.TEMPVARFORMAT.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

6         +
        PROC FORMAT LIB=WORK;  VALUE DC170A1483Format &FormatValuesM;  RUN;;

NOTE: Format DC170A1483FORMAT is already on the library.
NOTE: Format DC170A1483FORMAT has been output.
NOTE: PROCEDURE FORMAT used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

MPRINT LOG

MPRINT(CONSTRUCTVARIABLEFORMAT):   DATA TestDataSetFormat;
MPRINT(CONSTRUCTVARIABLEFORMAT):   SET TempVarFormat END=Last;
MPRINT(CONSTRUCTVARIABLEFORMAT):   LENGTH FormatValues $10000;
MPRINT(CONSTRUCTVARIABLEFORMAT):   RETAIN FormatValues;
MPRINT(CONSTRUCTVARIABLEFORMAT):   IF 1 = 1 THEN FormatValues = CATX(" 
",FormatValues,STRIP(LookUpValue)|| " = "||CATQ("A",TRIM(LookupDescription)));
MPRINT(CONSTRUCTVARIABLEFORMAT):   ELSE FormatValues = CATX(" ",FormatValues,CATQ("A" 
,LookUpValue)||" = "||CATQ("A" ,TRIM(LookupDescription)));
MPRINT(CONSTRUCTVARIABLEFORMAT):   Test = STRIP(FormatValues);
MPRINT(CONSTRUCTVARIABLEFORMAT):   PUT Test ;
MPRINT(CONSTRUCTVARIABLEFORMAT):   IF Last THEN CALL 
SYMPUT('DataCollectionFK',DataCollectionFK);
MPRINT(CONSTRUCTVARIABLEFORMAT):   IF Last THEN CALL SYMPUT('FormatValuesM',Test);
MPRINT(CONSTRUCTVARIABLEFORMAT):   RUN;
MPRINT(CONSTRUCTVARIABLEFORMAT):   PROC FORMAT LIB=WORK;
WARNING: Apparent symbolic reference FORMATVALUESM not resolved.
MPRINT(CONSTRUCTVARIABLEFORMAT):   VALUE DC170A1483Format &FormatValuesM;
MPRINT(CONSTRUCTVARIABLEFORMAT):   RUN;

编辑更多尝试: 问题在于,由于某种原因,宏变量在datastep期间没有获得值。在运行宏之前加载具有空值的macro变量 脚本没有给出错误。但是变量被解析为一个空变量。

删除IF Last THEN部分,也不会改变结果。

1 个答案:

答案 0 :(得分:2)

当然,使用cntlin=的{​​{1}}选项传递包含相关格式名称,开头,结尾,标签值的数据集更容易/更简单......

一个简单的例子......

/* Create dummy format data */
data formats ;
  fmtname = 'MYCHARFMT' ;
  type = 'C' ;
  do n1 = 'A','B','C','D','E' ;
    start = n1 ;
    label = repeat(n1,5) ;
    output ;
  end ;
  fmtname = 'MYNUMFMT' ;
  type = 'N' ;
  do n2 = 1 to 5 ;
    start = n2 ;
    label = repeat(strip(n2),5) ;
    output ;
  end ;
  drop n1 n2 ;
run ;

/* dummy data looks like this... */
 fmtname     type    start    label

MYCHARFMT     C        A      AAAAAA
MYCHARFMT     C        B      BBBBBB
MYCHARFMT     C        C      CCCCCC
MYCHARFMT     C        D      DDDDDD
MYCHARFMT     C        E      EEEEEE
MYNUMFMT      N        1      111111
MYNUMFMT      N        2      222222
MYNUMFMT      N        3      333333
MYNUMFMT      N        4      444444
MYNUMFMT      N        5      555555

/* Build formats from dataset */
proc format cntlin=formats library=work ; run ;

您可以在格式数据集中定义其他几个字段,以满足低/高/缺失值,范围等。

请参阅SAS文档> http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473464.htm