何时在 SAS 中使用 IF 或 %IF [英] When to use IF or %IF in SAS

查看:105
本文介绍了何时在 SAS 中使用 IF 或 %IF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SAS 的新手,很难弄清楚什么时候应该使用简单的 If-Then-else,什么时候应该使用 %IF-%THEN-%ELSE.作为下面的示例代码:

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);

我正在尝试获取第一行(标题)并采用参数 fdate.根据这个参数的值,我对后面的输入行进行了不同的解析.但这似乎不起作用,因为只有第二个输入部分运行(总是在输出中获取参数rectype").

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?

推荐答案

我看到您将 C++ 作为标签之一,而您才刚刚开始使用 SAS.因此,我将尝试针对您的背景提供一个答案.

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.

了解 SAS 宏命令与 DATA 步或多个同名过程中的命令之间区别的最简单方法,例如 %ifif, 是将 SAS 宏命令视为等效于 C/C++ 预处理器 (CPP) 指令.CPP 和 SAS MAcro 都是宏语言,虽然它们不是完全相同的语言,但它们有两个重要且最初令人困惑的特征:它们是文本处理器;它们在处理主代码之前作为单独的步骤执行.

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.

这种近似在某些地方会失效,但作为具有 C/C++ 背景的 SAS 初学者,这是一个很好的起点.

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.

这篇关于何时在 SAS 中使用 IF 或 %IF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆