Re: [程式] sas資料選取問題
※ 引述《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
推
06/24 20:58, , 1F
06/24 20:58, 1F
討論串 (同標題文章)