作者imaltar (..)
站内Statistics
标题Re: [程式] sas资料选取问题
时间Thu Jun 24 15:21:41 2010
※ 引述《tew (咖啡王子)》之铭言:
: : data test;input comp_id date yymmdd8. a;
: : format date yymmddn8.;
: : cards;
: : 4212 20090415 0.15
: 1101 20090415 0.14
: : 3232 20090416 1.23
: 1101 20090416 1.12
: : 1101 20090417 2.33
: 1101 20090418 1.66
: : 9232 20090418 1.66
: : 8422 20090512 2.36
: 1101 20090512 2.33
: : 1102 20090612 1.42
: : 6473 20090613 2.42
: : ;
: : run;
: 如果资料形式是这样
: 下面的巨集如何抓出
: 1101 20090417以前的资料
: 我找不出您考量这部分的语法之处
: 当然 最主要是 我弄不懂原问者的问题
: 你可以再试试看
: : %macro date(comp_id=,n=);
: : data _null_;set test(where=(comp_id in (&comp_id.)));
: : call symput('date',date);run;
: : data test&n.;set test(keep=date);comp_id=&comp_id.;if date <= &date.;run;
: : %mend;
: : %date(comp_id=1101,n=1);
: : %date(comp_id=1102,n=2);
: : data test;set test1 test2;
: : proc sort data=test;by comp_id date;run;
: : comp_id就是公司名,n=1表示产生test1这个资料,内容是comp_id=1101时日期小於
: : 20090417的结果
: : 最後的test合并test1 test2就是你想要的资料
根据t大设定的资料
data test;input comp_id date yymmdd8. a;
format date yymmddn8.;
date1=put(date,yymmddn8.);
cards;
4212 20090415 0.15
1101 20090415 0.14
3232 20090416 1.23
1101 20090416 1.12
1101 20090417 2.33
1101 20090418 1.66
9232 20090418 1.66
8422 20090512 2.36
1101 20090512 2.33
1102 20090612 1.42
6473 20090613 2.42
;
run;
公司名有重复,那以以下的巨集执行
%macro date(comp_id=,n=,time=);
data _null_;set test;if comp_id in (&comp_id.) and date1 in (&time.);
call symput('date',date);run;
data test&n.;set test;comp_id1=&comp_id.;if date <= &date.;run;
%mend;
%date(comp_id=1101,n=1,time=20090417);
output是
comp_id date a date1 comp_id1
4212 20090415 0.15 20090415 1101
1101 20090415 0.14 20090415 1101
3232 20090416 1.23 20090416 1101
1101 20090416 1.12 20090416 1101
1101 20090417 2.33 20090417 1101
comp_id是原本的公司名,comp_id1就是根据的公司名
在巨集中限定日期是20090417,所以就可以把小於此日期的所有资料抓出来
当然这巨集可以改成不用一一输入公司名和日期就可以得到结果的形式
只是我懒得想了XD
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.43.125.210
1F:推 richubby:谢谢i高手!!! 我会再试试看的^^ 06/24 20:58