• 首页
  • 关于
  • 存档
  • 文章
  • 链接
  • 标签

益药,益友-益药博客

专注于新药临床试验的数据管理与统计分析,关注医药行业健康发展

Feeds
RSS
Atom

上一篇:偶然发现的一些SAS电子书 下一篇:雪还在下

宏变量的引用与解析

2009-10-27 by medleaf

在编写宏程序的时候遇到一个问题,即一个宏变量与其它字母、数字按一定的规则组合成为另一个宏变量,比如
%let mvar = group;
%let &mvar.1 = A;

即上面这条语句定义了一个新的宏变量:group1,值是A,那么这个宏变量如何在程序中调用呢?

在SAS的网站上检索到一篇文献(地址:http://www2.sas.com/proceedings/sugi22/CODERS/PAPER77.PDF),其中有一段:“The macro variable &DSN&N resolves to CLINICS5, &DSN5 resolves to FRED.  The &&DSN&N combination first resolves to a macro variable (&DSN5) which then resolves to a value (FRED) in a second pass. ” 按照这篇文献,上面的问题很简单:引用宏变量group1写成&&mvar.1即可,但在SAS9中,实践证明这样的写法无法解析成为&group1。

后来在人大经济论坛里找到了这个问题的答案:&&&mvar.1。

之后又检索了一篇文章,专门是写这个&&&&&......解析问题的:http://www.listserv.uga.edu/cgi-bin/wa?A2=ind0706b&L=sas-l&P=43316

主要内容如下:

--------------------------------

Consider:

%let a=b; /* n=1 */
%let b=c; /* n=2 */
%let c=d; /* n=3 */
%let d=e; /* n=4 */

Problem: Give the value of D in terms of A.

&d is e
&c is d so &&&c is e
&b is c so &&&b is d so &&&&&&&b is e
&a is b so &&&a is c so &&&&&&&a is d so &&&&&&&&&&&&&&&a is e

General rule:

Two &'s resolve to 1 and the expression is re-scanned.

Corr:

2**n - 1 &'s needed

Now that we have the base of understanding, here is the code.

%macro q ( n=1 ) ; %local i ampers m ; %do i = 1 %to &n ; %local x&i ; %let x&i = x%eval(&i+1) ; %put x&i = x%eval(&i+1) ; %end ;

%let m = %eval(2**&n-1); %let ampers = %qsysfunc(repeat(%nrstr(&),&m-1)) ; %put amperlen=%length(&ampers) ; %put %superq(ampers)x1 = >>>%unquote(&ampers.x1)<<< ; %mend q ;

%q(n=15)

Don't try to go above 15 because the number of ampersands is too great to hold in a "text expression" in the macro language.

Is this an important program? No, but any macro programmer should know how to manipulate ampersands and how they are resolved, so it is an important question to ask for understanding.

On the other hand, in my first macro class I asked the students to derive the corollary from the general principle. I soon learned that programmers don't count above 3.

A search for "multiple ampersands" at http://lexjansen.com/sugi/ turned up 67 papers, many of which look relevant to some degree.

--------------------------------

原来,多个&解析的规则是2**n - 1,对于最开始的那个问题,n=2,则需要&&&才能解析为我们要的宏变量。

tags: sas, sas宏程序

Posted in 生物统计 | 144 views

Related

  • 偶然发现的一些SAS电子书 by medleaf
  • Hello Word!-SAS统计报表宏程序sas2word介绍之一 by medleaf
  • 开始SAS报表宏程序的重新编写工作 by medleaf
  • 药审中心即将成立生物统计室 by medleaf
  • 生物统计在新药开发中的价值 by medleaf

评论 [1]

  1. on 2009-11-10 05:05 #asteriod

    留个言,我也关注sas在医学领域的应用,http://blog.sina.com.cn/asteriod2008 能交换个链接吗?

Leave a Reply

  • 订阅到:

    • 订阅到iGoogle或Google Reader   订阅到鲜果   订阅到抓虾   订阅到飞鸽   订阅到Bloglines   订阅到我的雅虎   订阅到NetVibes   订阅到Newsgatar  使用RSS邮天下订阅
  • 最新文章:


    • 新年快乐!我的博客地址变更了
    • 雪还在下
    • 宏变量的引用与解析
    • 偶然发现的一些SAS电子书
    • Hello Word!-SAS统计报表宏程序sas2word介绍之一
    • 开始SAS报表宏程序的重新编写工作
  • 分类:

    • 生物统计
    • 临床试验
    • 电脑与网络
    • 数据管理
    • 随笔
    • 观点
    • 新药研究
  • 最新留言:


    • agri521 (偶然发现的一些SAS电子书)
    • asteriod (宏变量的引用与解析)
    • q23r23r (新药研发,质量比数量更重要)

益药,益友-益药博客 © 2007 All Rights Reserved.

MistyLook made TXP-ready by Textpattern Templates