简体   繁体   English

数据步骤中宏变量的解析

[英]Resolution of Macro Variables in a Data Step

I'm having trouble getting If/Then statements to work correctly with my macro variables inside a datastep. 我在让If / Then语句在数据步骤中与宏变量一起正常工作时遇到麻烦。 I'm writing a macro to handle two different cases: calculating stat tests with no transformation, and then calculating stat tests after a natural log transformation. 我正在编写一个宏来处理两种不同的情况:计算无转换的统计测试,然后在自然对数转换后计算统计测试。 If my data fails the test of normality, I log transform and test again. 如果我的数据未通过正常性测试,则记录转换并再次测试。 If it passes, I set my global flag, log_flag , to 1. I then want to test the status of this flag in data steps in order to correctly handle the transformed (or not) variables. 如果通过,则将我的全局标志log_flag设置为1。然后,我想在数据步骤中测试该标志的状态,以便正确处理已转换(或未转换)的变量。 I've tried variations of the following: 我尝试了以下变化:

Data want;
set have;
if symget("log_flag")=1 then do;
if &log_flag. = 1 then do;
if resolve("log_flag")=1 then do;
test=symget("log_flag");
  if test=1 then do;
end

No matter what I try, the if/then statement is essentially ignored and all code following it is processed as if the if/then were true, even when it is false. 不管我尝试什么,本质上都会忽略if / then语句,并且即使if / then为false,它后面的所有代码也会被视为if / then为true。 I know that the log_flag is being correctly assigned a value of zero because the %if %then statements work and execute correctly in open code. 我知道为log_flag正确分配了零值,因为%if %then语句可以在打开的代码中正常工作并正确执行。 I'm just having trouble getting it to resolve correctly inside a datastep. 我只是很难让它在数据步骤中正确解析。

Please let me know if there is any other information you need to help me figure this out. 请让我知道您是否需要其他信息来帮助我解决这个问题。 Thanks guys! 多谢你们!

The issue you have identified in the comments is that you do not want to generate the SAS code at all. 您在注释中发现的问题是,您根本不想生成SAS代码。 That is what the macro language processor is for. 那就是宏语言处理器的作用。 So use %IF to conditionally generate the code. 因此,请使用%IF有条件地生成代码。

So if you only want to create the variable newvar when the macro variable log_flag is one then you could code it this way. 因此,如果您只想在宏变量log_flag为1时创建变量newvar ,则可以采用这种方式进行编码。

data want ;
  set have ;
%if &log_flag. = 1 %then %do;
  newvar= x*y ;
%end;
run;

So when &log_flag. = 1 因此,当&log_flag. = 1 &log_flag. = 1 you run this code: &log_flag. = 1您运行以下代码:

data want ;
  set have ;
  newvar= x*y ;
run;

And when it isn't you run this code: 如果不是,则运行以下代码:

data want ;
  set have ;
run;

Starting with SAS 9.4 M5 release you can use this in open code, otherwise place it inside a macro definition and execute the macro. 从SAS 9.4 M5版本开始,您可以在开放代码中使用它,否则将其放在宏定义中并执行该宏。

  • SYMGET() will return a character variable. SYMGET()将返回一个字符变量。
  • RESOLVE() will return a character variable, but it needs the & in the parameter. RESOLVE()将返回一个字符变量,但是它需要参数中的&。
  • &log_flag will resolve as numeric &log_flag将解析为数字

You need to treat them correctly depending on your reference method. 您需要根据参考方法正确对待它们。

Here's an example of testing each one independently and then you can test them together via nesting if desired. 这是一个独立测试每个示例的示例,然后您可以根据需要通过嵌套一起对它们进行测试。

%let log_flag=1;
Data want;
set sashelp.class;
if symget("log_flag")='1' then do;
  put "Test #1 is True";
end;

if &log_flag. = 1 then do;
  put "Test #2 is True";
end;


if resolve("&log_flag")="1" then do;
  put "Test #3 is True";
end;

test=symget("log_flag");
if test='1' then do;
  put "Test #4 is True";
end;

run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM