SAS 基本问题:为什么是宏?
与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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.
这有点像问,当我们拥有 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.
宏有很多用途和高级功能,但我主要只是使用它们,这样我就可以尝试忠实于 干原则。如果您发现自己经常重复代码块,您可能需要这样做。例如,我一直将数据集导出到 Excel。如果我最终在代码中重复执行几次,我只需将代码放入宏中:
然后,当我在代码中进行时。我制作了一个数据集,我想导出它,我可以这样写:
这是一个简单的行,我必须在一个文件中写入 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:
Then as I'm going along in my code. I make a dataset, and I want to export it, I can just write:
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.
大多数 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).
您可以在没有许可工具包的情况下创建自定义宏 - 我记得似乎没有多少人使用 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