SAS宏写入带有char和num参数的文本文件

SAS宏写入带有char和num参数的文本文件,sas,sas-macro,Sas,Sas Macro,我试图编写一个宏,使用参数值创建一个文本文件。我知道所有SAS参数都作为文本传递,所以我需要将文本转换为数字。正在为此使用输入,但仍会出现语法错误。谢谢你的帮助。多谢各位 代码: %macro test(n_var); data _null_; file"c:/temp/test.txt" TERMSTR=crlf; put ; put "(numeric variable passed = "input(&n_var,8.)")"; put ;

我试图编写一个宏,使用参数值创建一个文本文件。我知道所有SAS参数都作为文本传递,所以我需要将文本转换为数字。正在为此使用输入,但仍会出现语法错误。谢谢你的帮助。多谢各位

代码:

%macro test(n_var);

  data _null_;

    file"c:/temp/test.txt" TERMSTR=crlf;

    put ;
    put "(numeric variable passed = "input(&n_var,8.)")";
    put ;

  run;

%mend;

%test(n_var=100);
SYMBOLGEN:  Macro variable N_VAR resolves to 100
NOTE: Line generated by the macro variable "N_VAR".
39         100
           ___
           22
           76
MPRINT(TEST):   put "(numeric variable passed = "input(100,8.)")";
MPRINT(TEST):   put ;
MPRINT(TEST):   run;

ERROR 22-322: Syntax error, expecting one of the following: a name, arrayname, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  

ERROR 76-322: Syntax error, statement will be ignored.
日志:

%macro test(n_var);

  data _null_;

    file"c:/temp/test.txt" TERMSTR=crlf;

    put ;
    put "(numeric variable passed = "input(&n_var,8.)")";
    put ;

  run;

%mend;

%test(n_var=100);
SYMBOLGEN:  Macro variable N_VAR resolves to 100
NOTE: Line generated by the macro variable "N_VAR".
39         100
           ___
           22
           76
MPRINT(TEST):   put "(numeric variable passed = "input(100,8.)")";
MPRINT(TEST):   put ;
MPRINT(TEST):   run;

ERROR 22-322: Syntax error, expecting one of the following: a name, arrayname, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.  

ERROR 76-322: Syntax error, statement will be ignored.
所有SAS宏符号(又名变量)都是文本。SAS宏参数是文本

您的用例可能不需要将文本转换为数字

考虑:

%let x = 100;
data _null_;
  my_num_var = &x;
run;
宏变量(或者更好地理解为“符号”)的分辨率为字母1 0 0,但与要解释为SAS代码的文本有关。数据步骤编译器根据它所看到的行推断my_num_var是数字

  my_num_var = 100;
在某些用例中,您可能希望测试宏参数是否可以解释为数值。此时,这样的用例可能超出了您的需要

输入函数是特殊的数据步函数之一,不能通过
%sysfunc
函数在SAS宏中使用。当您必须在数据步骤之外以“纯”方式“输入”值时,您需要通过
%sysfunc
调用
INPUTN
inputnutc
函数

%let evaluatedRepresentation = %sysfunc(inputn(&x,best8.));
输入n的数值计算将转换为文本并分配给符号
evaluatedRepresentation

如果您无法控制宏的调用者,在宏中您可以使用符号和求值,则更安全的方法是通过
SUPERQ
进行求值,以中断代码注入和其他anamolies

%let evaluatedRepresentation = %sysfunc(inputn(%superq(x),best8.));

我不明白。如果您正在写入文本文件,那么您只是在写入文本。传递给宏的值看起来是数字还是文字有什么区别?