SAS中文论坛

标题: [学习贴] 根据SET的变量值输出到不同文件中去,目标文件不定. [打印本页]

作者: shiyiming    时间: 2004-12-3 23:52
标题: [学习贴] 根据SET的变量值输出到不同文件中去,目标文件不定.
继续学习MACRO中
大家看看。
data ok;
input id;
cards;
2
5
6
9
3
;
run;
我要set ok,然后把每一条记录output到相应的 out2 out5 out6 out9 out3中去。
我用了一个笨办法解决了。不知道大家有好办法没有。
没在这里发过新贴,今天开
作者: shiyiming    时间: 2004-12-4 03:30
标题: 抛砖引玉,没考虑missing和重复纪录
data ok;
input id;
cards;
2
5
6
9
3
;
run;
%macro createout(line,id ) ;
data out&id ;
set ok(firstobs=&line obs=&line);
run;
%mend createout;
data _null_;
set ok;
call execute('%createout('||trim(put(_n_,8.)) ||','||trim(put(id,8.)) ||')');
run;
作者: shiyiming    时间: 2004-12-4 04:03
标题: 考虑missing 和重复纪录可能要这样(如果能排序的话)
data ok;
input id;
cards;
2
5
6
5
9
.
9
3
;
run;

%macro createout(fobs,lobs,id ) ;
data out&id ;
set ok(firstobs=&fobs obs=&lobs);
run;
%mend createout;

proc sort data=ok;
by id;
run;

data _null_;
set ok(where=(id is not missing));
by id;
retain fobs lobs;
if first.id then fobs=_n_;
if last.id then lobs=_n_;
else return;
if last.id then
call execute('%createout('||trim(put(fobs,8.)) ||','||trim(put(lobs,8.))||',' ||trim(put(id,8.)) ||')');
run;
作者: shiyiming    时间: 2004-12-4 09:42
标题: 还要努力啊
我运行了一下你的程序。
结果好象不对。
比如,我的out2中的记录应该是一条id=2的记录
可是你的是缺失值。
你的out3里面是2
你的out5里面是3,5
....
这些都不符合条件
可能你还得想一下。
作者: shiyiming    时间: 2004-12-4 09:58
标题: 想优化一下程序还没优化成,还是改回去吧
data _null_;
[u:618bb]set ok ;
if id ne . ;[/u:618bb]
by id;
retain fobs lobs;
if first.id then fobs=_n_;
if last.id then lobs=_n_;
else return;
if last.id then
call execute('%createout('||trim(put(fobs,8.)) ||','||trim(put(lobs,8.))||',' ||trim(put(id,8.)) ||')');
run;
作者: shiyiming    时间: 2004-12-4 10:42
标题: 再等
嗯,对了。不过和我的做法不一样。
看来我们想法和思维方向还是有区别的
我最后的输出是在一个DATA步中完成的,所以要多两个技术难点。
呵呵,也好,多点经验。多了点认识。
再看看有没有别人用别的方法吧
作者: shiyiming    时间: 2004-12-5 10:31
标题: 可否把您的程序贴一下
或者说一下技术难点在那里




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