SAS中文论坛

标题: 如何用macro来自动生成文件命 [打印本页]

作者: shiyiming    时间: 2011-5-5 10:00
标题: 如何用macro来自动生成文件命
data a;
input sc $ pt map;
cards;
a 1 62
b 3 9
b 2 7
c 1 12
d 2 6
e 1 9
f 3 7
g 0 4
h 3 83
i 1 2
。。。
;
run;
我有个data如上所示,现在想得到n个data,把具有相同SC值的record存在一起,并起名:
a_a      (if sc='a'),
a_b      (if sc='b'),
a_c      (if sc='c'),
a_d      (if sc='d'),
...

谢谢!
作者: shiyiming    时间: 2011-5-5 12:57
标题: Re: 如何用macro来自动生成文件命
[code:21h1g9wf]data a;
    input sc $ pt map;
cards;
a 1 62
b 3 9
b 2 7
c 1 12
;
proc sql noprint;
    create table _temp as select distinct sc from a;
    select cats('ds_',sc) into :dslist separated by ' ' from _temp;
quit;
data _null_;
    set _temp end=last;
    if _n_=1 then call execute("data &dslist; set a;");
    call execute(cat('if sc=',quote(trim(sc)),' then output ds_',trim(sc),';'));
    if last then call execute('run; proc delete data=_temp; run;');
run;[/code:21h1g9wf]
作者: shiyiming    时间: 2011-5-5 22:11
标题: Re: 如何用macro来自动生成文件命
so great!!!
3x a lot.
作者: shiyiming    时间: 2011-5-6 11:07
标题: Re: 如何用macro来自动生成文件命
我自己用MACRO写了一段程序,却无法运行,请高手指点一二。
谢谢!!!

data a;
input sc $ pt map;
cards;
a 1 62
b 3 9
b 2 7
c 1 12
;
run;
%macro na;
proc sql;
select count(*) into :n from a;
select sc into :va separated by ' ' from a;
quit;
%do i=1 %to &n.;
proc sql;
create table a_&va. as
select * from a
where sc=%scan(&va.,&i.);
quit;
%end;
%mend;
%na
作者: shiyiming    时间: 2011-5-6 11:09
标题: Re: 如何用macro来自动生成文件命
上面有个笔误,以下面的为准。
谢谢!!!


data a;
input sc $ pt map;
cards;
a 1 62
b 3 9
c 2 7
d 1 12
;
run;
%macro na;
proc sql;
select count(*) into :n from a;
select sc into :va separated by ' ' from a;
quit;
%do i=1 %to &n.;
proc sql;
create table a_&va. as
select * from a
where sc=%scan(&va.,&i.);
quit;
%end;
%mend;
%na
作者: shiyiming    时间: 2011-5-14 13:21
标题: Re: 如何用macro来自动生成文件命
你把va %put,看看输出,就应该会发现一个问题的
其实程序报错就能发现问题所在




欢迎光临 SAS中文论坛 (http://mysas.net/forum/) Powered by Discuz! X3.2