SAS 基本问题:为什么是宏?

发布于 2024-09-28 22:31:28 字数 80 浏览 1 评论 0原文

与 SAS 过程相比,SAS MACRO 的独特性和优势是什么?如果宏的目的是减少针对不同输入重复任务的工作量,那么这不能通过编写为过程来完成吗?

What is the uniqueness and advantage of SAS MACROs over SAS procedures? If macro's purpose is to reduce the effort to repeat tasks for different inputs, can't this be done by written as procedures too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北方的韩爷 2024-10-05 22:31:28

SAS 用户确实能够创建自己的 SAS PROC。他们必须获得 SAS/Toolkit 和 C 语言或其他语言代码的许可。

SAS 有许多不同的部分。宏是(或曾经是)SAS 中最通用的工具(可能是唯一的),可以将(几乎所有但不是所有)部件组合在一起。考虑到宏(语言)是多么原始和奇怪,这有点有趣但却是事实。

SAS users do have the ability to create their own SAS PROC's. They have to license SAS/Toolkit and code in C or in some other languages.

SAS has many different parts. Macro is (or used to be) the most (and probably the only) universal tool in SAS that can put (almost any but not every) parts together. Given how primitive and strange the macro (language) is, this is a bit funny but true.

亢潮 2024-10-05 22:31:28

这有点像问,当我们拥有 UNIX 中可用的所有程序时,为什么还要有 Perl、Python、ksh、bash 等。宏是一种脚本语言,它为用户提供了极大的灵活性并可以控制他们希望 SAS 执行的操作。您还可以轻松生成动态代码。现有的过程在输入内容和输出内容方面相当严格。 Proc FCMP 向用户定义的函数开放了数据步骤,但这仍然无法替代宏语言所带来的灵活性。

It's kind of like asking why do we have Perl, Python, ksh, bash, etc. when we have all the programs that are available in UNIX. Macros are the scripting language that gives users a great deal of flexibility and control over what they want SAS to do. You can easily generate dynamic code as well. Existing procs are fairly rigid in what they can take as input and what you get as output. Proc FCMP has opened up the data step to user defined functions, but this is still no substitute for the flexibility that you get with the macro language.

浮光之海 2024-10-05 22:31:28

宏有很多用途和高级功能,但我主要只是使用它们,这样我就可以尝试忠实于 干原则。如果您发现自己经常重复代码块,您可能需要这样做。例如,我一直将数据集导出到 Excel。如果我最终在代码中重复执行几次,我只需将代码放入宏中:

%macro export_set_excel(data,path,filename,sheetname);
    PROC EXPORT DATA= &data. OUTFILE= "&path.&data." DBMS=EXCEL REPLACE;
      SHEET="&sheetname."; 
      NEWFILE=YES;
    RUN;
%mend export_set_excel;

然后,当我在代码中进行时。我制作了一个数据集,我想导出它,我可以这样写:

%export_set_excel (data=MyDataSet, path=C:\Temp\, filename=mydata.xlsx, sheetname=exportedData);

这是一个简单的行,我必须在一个文件中写入 30 次,而不是必须写入 7 或 8 行 30 次。它可以更轻松地追踪错误,如果我想更改导出方式的某些内容,我只需在一处进行更改即可。

我不仅仅将它用于导出,但这应该可以让您有一个大概的了解。

如果你熟悉其他语言,你会发现 sas 宏语言相当缺乏和麻烦,但它绝对比没有好。

There are lots of uses and advanced features of macros, but I mainly just use them so I can try to stay true to the DRY Princicple. If you find yourself in a situation where you are repeating blocks of code frequently, you might want to do this. For example, I export datasets to excel all the time. If I end up doing it over a few times in my code, I just put the code in a macro:

%macro export_set_excel(data,path,filename,sheetname);
    PROC EXPORT DATA= &data. OUTFILE= "&path.&data." DBMS=EXCEL REPLACE;
      SHEET="&sheetname."; 
      NEWFILE=YES;
    RUN;
%mend export_set_excel;

Then as I'm going along in my code. I make a dataset, and I want to export it, I can just write:

%export_set_excel (data=MyDataSet, path=C:\Temp\, filename=mydata.xlsx, sheetname=exportedData);

That is one simple line I have to write 30 times in one file instead of having to write 7 or 8 lines 30 times. It makes it easier to track down mistakes, and if i want to change something about how I'm exporting, i just have to change it in one place.

I use it for a lot more than just exporting, but that should give you a general idea.

If you are familiar with other languages, you will find the sas macro language pretty lacking and cumbersome, but it is most definitely better than nothing.

贪恋 2024-10-05 22:31:28

大多数 SAS 过程对数据执行特定操作。宏允许对 SAS 代码块进行运行时条件处理,例如使用 DO 循环对不同数据集重复过程调用,或者根据文件是否存在来决定运行数据步骤。

虽然这些代码对于新手来说似乎很笨重,但一旦您学习了 BASE SAS,实际上就相对容易上手,因为语法几乎相同(只是添加了 %s 和 @s 来增加风味)。

Most of the SAS procedures perform specific operations on data. Macros allow run-time conditional processing of blocks of SAS code, such as repeating a procedure call on different data sets using a DO loop, or decidng to run a data step based on whether a file exists.

While the code seems clunky to newcomers, it is actually relatively easy to pick up once you learn BASE SAS, since the syntax is nearly identical (with a sprinkling of %s and @s for flavor).

怪我入戏太深 2024-10-05 22:31:28

您可以在没有许可工具包的情况下创建自定义宏 - 我记得似乎没有多少人使用 SAS Toolkit 来执行自定义过程。

宏有助于使用自定义参数重复运行程序,并且对于变量操作和创建过程非常有用,

过程通常是输出设备。宏也可以帮助重复的数据步骤操作。

这是一些代码

在此处输入图像描述

you can create a custom macro without licensing toolkit- and to my recall not many people seem to use SAS Toolkit for custom procedures.

macros help run programs repeatedly with customized parameters and are invaluable for variable manipulation and creation

procs are generally output devices. macros can help with repeated data step manipulation as well.

here is some code

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文