我的想法:先求出(3)的结果,利用它再求1和2
我的伪代码:求(3)结果:
for i= 1 to 12
for k=0 to 15 /*age group*/
select count(1) as number ,i as month , k as age_group, 'A' as region
where month=i , int(age/5)=k, region='a',index(id,'21') ne 0;
end for // for k
end for // for i
.....same code to other regions like B C D...
proc format;
value age 1-5='1-5' 6-10='6-10'
11-15='11-15' 16-20='16-20'
21-25='21-25' 26-30='26-30';
;
data tem;
input id $ age month region $;
age1=put(age,age.);
cards;
121 18 1 A
121 17 1 B
121 16 2 A
121 17 2 A
150 30 5 B
;run;
proc sql;
create table result1 as
select age1 as age,region,month,count(1) as number from tem
where index(id,'21')>0 group by age1,region,month;
quit;
proc print;run;作者: shiyiming 时间: 2004-4-13 13:05 标题: proc freq 过程实现。 /**********************************************************/
proc format;
value age
1 - 5='1'
6 -10='2'
10-15='3'
16-20='4'
21-25='5'
26-30='6'
31-35='7'
;
data tem;
input id $ age month region $;
age_1=put(age,age.);
cards;
121 18 1 A
122 17 1 B
125 15 2 A
150 30 5 B
121 14 5 A
121 9 4 B
121 19 1 B
121 17 1 A
;
run;
/********************************************************/
proc freq data=tem noprint;
table age_1*region*month/norow nocol nopercent out=tem1;
where index(id,'21') ne 0;
run;作者: shiyiming 时间: 2004-4-13 19:11
假设数据源是这样的:
data source;
input id age month region $;
cards;
121 18 1 A
121 17 1 B
121 16 2 A
121 17 2 A
150 30 5 B
;
run;
造个format从1到100,每5个一段:
data format;
length start end label $10;
fmtname = "age_grp";
type = "N";
do i = 1 to 100 by 5;
start = trim(left(i));
end = trim(left(i + 4));
label = trim(start)||"-"||trim(end);
output;
end;
start = "other";
end = start;
label = start;
output;
drop i;
run;
proc format cntlin=format;
run;
用Proc Summary把 region*month*age month*age region 三个层次的汇总一次生成:
proc summary data=source(where=(index(trim(left(id)),'21')>0)) chartype;
class region month age;
types region*month*age month*age region;
output out=summary;
format age age_grp.;
run;
打印结果:
proc print data=summary(keep=region age month _freq_ _type_ where=(_type_="111"));
run;