msgbartop
List for SAS fans and programmer
msgbarbottom

18 4月 11 创建SAS宏变量的几类方法及举例


SAS里面除了变量,还有宏变量,其用途也非常广泛。创建宏变量的方法最早有shiyiming总结,翻了翻Rick Aster的Professional SAS Programming Shortcuts – Over 1,000 Ways To Improve Your SAS Programs,发现里面并没有总结这个问题,有点失望。

这里转载并补充姚志勇的SAS书里面的内容,使得更加完整和充实,便于大家以后方便选择使用,一共有四类方法:

1、通过直接赋值或通过宏函数创建宏变量 最基本最常用的

%let mv = 100;

%let dsid=%sysfunc(open(sashelp.class));

%let nvars=%sysfunc(attrn(&dsid,nvars));
%let nobs=%sysfunc(attrn(&dsid,nobs));
%let dsid=%sysfunc(close(&dsid));
%put &nvars.;
%put &nobs.;

2、通过data步接口子程序call symputx与call symput(两者有区别)

a, 创建单个宏变量

1
2
3
4
5
6
7
8
9
10
call symput('x', x);
run;
data _null_;
set sashelp.class nobs=obs;
call symputx('m1',obs);
call symput('m2',obs);
Stop;
run;
%put &m1.;
%put &m2.;

b, 为某变量的每个值创建一个宏变量

data _null_;
set sashelp.class;
suffix=put(_n_,5.);
call symput(cats(‘Name’,suffix), Name);
run;

c, 为表的每个值创建一个宏变量

data _null_;
set sashelp.class;
suffix=put(_n_,5.);
array xxx{*} _numeric_;
do i =1 to dim(xxx);
call symput(cats(vname(xxx),suffix),xxx);
end;
array yyy{*} $ _character_;
do i =1 to dim(yyy);
call symput(cats(vname(yyy),suffix),yyy);
end;
run;

3、proc sql方法 这个用法有很多的灵活性

a. 通过SQL过程用变量值创建一个宏变量

proc sql noprint;
select distinct sex
into :list_a separated by ‘ ‘
from sashelp.class;
quit;
%put &list_a.;

b.通过SQL过程创建多个宏变量

proc sql noprint;
select nvar,nobs
into:nvar , :nobs
from dictionary.tables
where libname = ‘SASHELP’ and memname = ‘CLASS’;
quit;
%put &nvar.;
%put &nobs.;

c. 通过contents和sql过程用变量名创建宏变量

proc contents data=sashelp.class out=con_class;
run;
proc sql noprint;
select name,put(count(name),5.-l)
into :clist separated by ‘ ‘,:charct
from con_class
where type=2;
quit;
%put &clist.;
%put &charct.;

d.通过SQL过程用宏变量创建宏变量列表

proc sql noprint;
select name
into :clist1-:clist999
from dictionary.columns
where libname = ‘SASHELP’ and memname = ‘CLASS’;
quit;
%put &clist1.;
%put &clist2.;

e.通过SQL过程用变量值创建宏变量列表

proc sql noprint;
select count(distinct sex)
into :n
from sashelp.class;
select distinct sex
into :type1 – :type%left(&n)
from sashelp.class;
quit;
%put &n.;
%put &type1.;

4、使用call set call set是我们处理对照表数据最强的武器,不但灵活方便,而且性能上是最优的。

1
2
3
4
5
6
7
8
9
10
%macro doit;
%let id=%sysfunc(open(sashelp.class));
%let NObs=%sysfunc(attrn(&id,NOBS));
%syscall set(id);
%do i=1 %to &NObs;
%let rc=%sysfunc(fetchobs(&id,&i));
%put # # # Processing &Height # # #;
%end;
%let id=sysfunc(close(&id));
%mend;

参考:

1,SAS中文论坛 几种给宏变量赋值的方法 by shiyiming 

2,姚志勇 SAS编程与数据挖掘商业案例 2010年出版 pp171-173.

ps:谢谢SAS中文论坛ID”天性爱好者“提供出处。


原创文章: ”创建SAS宏变量的几类方法及举例“,转载请注明: 转自SAS资源资讯列表

本文链接地址: http://saslist.net/archives/122


Reader's Comments

  1.    

    call set,这个得学习一下!!

    Reply to this comment
  2.    

    请问一下
    e.通过SQL过程用变量值创建宏变量列表

    proc sql noprint;
    select count(distinct sex)
    into :n
    from sashelp.class;
    select distinct sex
    into :type1 – :type%left(&n)
    from sashelp.class;
    quit;
    %put &n.;
    %put &type1.;
    这个如何调用最后一个type的值。
    比如n=10,但是不要用&type10.如何调用,请指教,谢谢。

    Reply to this comment
  3.    

    我也知道可以。我的问题是,但你不知道n是多少的时候,你如何引用。要用到n的。

    Reply to this comment
    •    

      还是没看懂你的问题。n不是已经赋值了么 ? 你的问题好像是,“我要引用一个宏变量,但是我不知道变量名,请问怎么引用这个变量 ?” 如果你不懂SAS的话,建议你先学些基础的东西。

      Reply to this comment
  4.    

    …….博主,貌似是你没理清别人问的东西,还让人家去学基础的东西!

    我估计他的意思是这样:
    比如,一个数据集,对于某个属性,有很多个值。
    虽然我们已经赋值给n了。但是如果没有%put &n.;这个语句,你知道n是多少吗?!

    那么,如果你要做一个循环之类的,你怎么调用这个所谓的n值,比如说,你要遍历某个属性的值,你如何做。我感觉他的意思应该是这样的。

    proc sql noprint;
    select count(distinct sex)
    into :n
    from sashelp.class;
    select distinct sex
    into :type1 – :type%left(&n)
    from sashelp.class;
    quit;
    %put &n.;这句删掉,然后他想知道typen是什么?你如何写出来?
    %put &&type&n.;如果你写这样子,我表示这个是程序跑不来的。当然,博主可能学了SAS基础知识,可能在某些书本写着是这样引用的,但这就是跑不出。请问博主能解决么?

    Reply to this comment

Leave a Comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据