When to use IF or %IF in SAS

时光毁灭记忆、已成空白 提交于 2019-12-22 10:07:37

问题


I am new to SAS and having a hard time figuring out when should the simple If-Then-else and when should %IF-%THEN-%ELSE should be used. As an example code below:

%let inFile = %scan(&sysparm, 1, " ");
%macro read_data(infile);
data want;
infile "&infile" LRECL=1000;
retain fdate;
if _n_ = 1 then do;
  input Agency $ Status $ Num $ fdate sdate;
end;
else do;
   %if fdate < 20130428  %then
   input
   @1   poolno                  $6.
   @7   factor                  9.8 @;
   %else
   input
   @1   rectype                 $1
   @3   poolno                  $6.
   @9   factor                  9.8 @;

   @18 pfactor                 9.8;
output;
end;
drop Agency Status Num sdate;
run;
proc print data=want;
run;
%mend read_data;
%read_data(&inFile);

I am trying to get the first line(header) and taking the parameter fdate. Based on the value of this parameter, I am parsing the subsequent input lines differently. But this does not seem to work, as only the second input part runs (always getting parameter 'rectype' in output).

Any suggestions as what i might be doing wrong?


回答1:


I see you have C++ as one of your tags, and that you are just getting started with SAS. So I'll try to provide an answer specific to your background.

The easiest way to understand the distinction between SAS Macro commands and the commands in the DATA step or in several procs with the same name, like %if vs. if, is to think of SAS Macro commands as equivalent to C/C++ pre-processor (CPP) directives. CPP and SAS MAcro are both macro languages, and although they are not exactly the same kind of language, they share two important and initially confusing characteristics: they are text processors; and they are executed as a separate step before the main code is processed.

There are places where this approximation breaks down, but as a beginner to SAS with background in C/C++, it is a good place to start.




回答2:


The macro statement %if is compiled before any data step statement. That means you are generally not able to use a data step variable in your logic expression. When the macro processor is compiling the macro statement, data step variable doesn't exist yet.




回答3:


In the example above, the %IF condition is based on a datastep variable/value. This should indicate it can be achieved using a datastep 'if' rather than a %IF.

You have already received an answer to this in your previous question, https://stackoverflow.com/a/15341502/108797




回答4:


If you do %if fdate < 20130428 SAS compares literals fdate and 20130428 not the value od fdate and 20130428.

If you had a macro variable named fdate you would do %if &fdate < 20130428.

In your case fdate is a variable in a dataset, so use if not %if, but it seems like you are trying to create a data step with a macro so just using if will probably not work in this case (depends on what are you trying to get).



来源:https://stackoverflow.com/questions/15362884/when-to-use-if-or-if-in-sas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!