[英]How do I use a function within a put statement? SAS
I am trying to print a statement to the SAS Log alerting the user that certain programs did not run. 我正在尝试向SAS Log打印一条语句,警告用户某些程序没有运行。 To do this, I am printing to the SAS log using a put statement: 为此,我使用put语句打印到SAS日志:
%let step1 = My program;
%let rec= cats(&step1.);
data _null_;
put "** &rec. did not run";
run;
The put statement prints: "** cats(My program) did not run". put语句打印:“** cats(我的程序)没有运行”。 Does anyone know why the cats function is not executing? 有谁知道为什么猫的功能没有执行? I am guessing it has something to do with macro code being executed before SAS statements, but I am not sure how to make this work. 我猜它与在SAS语句之前执行的宏代码有关,但我不知道如何使这项工作。
EDIT 编辑
Thanks Reeza and Tom. 谢谢Reeza和汤姆。 Now I understand why this wasn't working - I have to use %sysfunc to use a function outside of a data step, and I can't use functions inside a put statement. 现在我理解为什么这不起作用 - 我必须使用%sysfunc来使用数据步骤之外的函数,并且我不能在put语句中使用函数。
Tom - you asked for a more complete example: 汤姆 - 你问了一个更完整的例子:
I am trying to print a statement to the SAS log so that the user can easily see which programs ran and which were skipped. 我正在尝试将语句打印到SAS日志中,以便用户可以轻松查看已运行的程序以及跳过的程序。 I am trying to accomplish that doing the following: 我正在尝试完成以下操作:
data _null_;
array steps(*) $ step1-step7;
do i=1 to dim(steps);
if symget(cats('run',i)) = 'N' then
put "** The program "symget(cats('run',i)" did not run";
end;
run;
I thought if I could figure out how to make a function execute inside the put statement, this code would work. 我想如果我能弄清楚如何在put语句中执行函数,这段代码就行了。 However, that is impossible. 但是,这是不可能的。 I should be able to get the variable to change with "i", though. 不过,我应该可以使用“i”来改变变量。
For example, when i is 1, the put statement should be, "The program file1.sas did not run". 例如,当i为1时,put语句应为“程序file1.sas未运行”。
When i is 2, the put statement should be, "The program file2.sas did not run", and so on. 当我是2时,put语句应该是“程序file2.sas没有运行”,依此类推。
I tried using: 我试过用:
put "** The program &&step&i did not run";
but SAS is not able to recognize my variables. 但SAS无法识别我的变量。
Hopefully this provides a little more context! 希望这提供了更多的背景!
You can add %SYSFUNC() to use functions outside a data step, but in this case, the CATS doesn't add any value to your process. 您可以添加%SYSFUNC()以使用数据步骤之外的函数,但在这种情况下,CATS不会向您的流程添加任何值。
%let step1 = My program;
%let rec= %sysfunc(cats(&step1.));
data _null_;
put "** &rec. did not run";
put "** &step1. did not run";
run;
Output: 输出:
293
294
295 %let step1 = My program;
296 %let rec= %sysfunc(cats(&step1.));
297
298 data _null_;
299 put "** &rec. did not run";
300 put "** &step1. did not run";
301 run;
** My program did not run
** My program did not run
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
If you are running a macro then just use a %DO
loop. 如果您正在运行宏,则只需使用%DO
循环。
%do i=1 %to 7 ;
%if (N=&&run&i) %then %put NOTE: The program &&step&i did not run;
%end;
Otherwise just use a data step variable and print that. 否则只需使用数据步变量并打印即可。
data _null_;
do i=1 to 7 ;
if symget(cats('run',i)) = 'N' then do;
length program $100 ;
program=symget(cats('step',i));
put 'NOTE: The program ' program 'did not run';
end;
end;
run;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.