将 proc freq 与重复的 ID 变量一起使用

Using proc freq with repeated ID variables

我想使用 proq freq 来计算某人在特定日期食用的食物类型的数量(fint 变量)。我的数据是长格式的,对于不同的食物类型和不同的访谈日期,有重复的 idno。但是 SAS 挂起并且不 运行 代码。我有超过300,000 datalines.Is 还有其他方法吗?

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

你的数据结构我有点不清楚,不过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;

尝试一个过程 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;