使用带有重复ID变量的proc freq

时间:2016-04-12 22:42:43

标签: sas frequency proc

我想使用proq freq来计算某人在特定日期消费的食物类型数量(fint变量)。我的数据是长格式的,重复的idno表示不同的食物类型和不同的访谈日期。但是,SAS挂起并且不运行代码。我有超过300,000个数据线。还有另一种方法吗?

proc freq;  
  tables idno*fint*foodtype / out=countft;  
run;

2 个答案:

答案 0 :(得分:0)

我对你的数据结构有点不确定,但是proc也可以计算。 假设每个人有多个日期,每个日期有多种食物类型,您可以使用:

data dataset;
set dataset;
count=1;
run;
proc means data=dataset sum;
class idno fint foodtype;
var count;
output out=countft sum=counftpday;
run;

/* Usually you only want the lines with the largest _type_, so keep going here */

proc sql noprint;
select max(_type_) into :want from countft;
quit;  /*This grabs the max _type_ from output file */

data countft;
set countft;
where _type_=&want.;
run;

答案 1 :(得分:0)

尝试proc sql:

proc sql;
create table want as
select distinct idno, fint, foodtype, count(*) as count
from have
order by 1, 2, 3;
quit;

更糟糕的情况是,在数据步骤中进行排序和计数。

proc sort data=have; 
by idno fint foodtype;
run;

data count;
set have;
by idno fint foodtype;
if first.foodtype then count=1;
else count+1;
if last.foodtype then output;
run;