统计SAS中发生事件的次数

统计SAS中发生事件的次数,sas,Sas,我在SAS中有一个表,上面有日期、公司名称和行业类别1:49 是否有一些简单的代码来计算每个行业在每个日期有多少家公司 因此,行业类别是我需要计算的。计算此行业类别在每个日期出现的次数。频率表列出了变量值的每个不同组合在数据集中出现的次数。每个组合也称为“箱”。频率表中的存储箱数可以称为“基数”,也可以称为不同值的数量 在SAS中生成频率表的方法有很多 Proc FREQ是简单分组的常见起点 然而,问题是 在每个日期,每个行业有多少家公司 这意味着得到一个子级别的基数计数。SQL可以在单个查询中

我在SAS中有一个表,上面有日期、公司名称和行业类别1:49

是否有一些简单的代码来计算每个行业在每个日期有多少家公司


因此,行业类别是我需要计算的。计算此行业类别在每个日期出现的次数。

频率表列出了变量值的每个不同组合在数据集中出现的次数。每个组合也称为“箱”。频率表中的存储箱数可以称为“基数”,也可以称为不同值的数量

在SAS中生成频率表的方法有很多

Proc FREQ是简单分组的常见起点

然而,问题是

在每个日期,每个行业有多少家公司

这意味着得到一个子级别的基数计数。SQL可以在单个查询中实现这一点:

**** simulate data begin;
data companies;
  do companyId = 1 to 1000;
    industryId = ceil(49*ranuni(123));
    output;
  end;
run;

data have;
  format date yymmdd10.;
  do date = '01-jan-2016'd to '31-dec-2018'd;
    if weekday(date) in (1,7) then continue; * no activity on weekend;
    do _n_ = 1 to 50; * upto 50 random 'events' of random companies;
       if ranuni(123) < 0.60 then continue;
       if ranuni(123) < 0.05 then leave;
       eventId+1;
       point = ceil(1000*ranuni(123));
       set companies point=point;
       output;
    end;
  end;
  stop;
run;
**** simulate data end;

* number of companies within industry (way #1);
* use sub-select to compute the cardinality of company with respect to date/industry;

proc sql;
  create table counts1 (label="Number of companies per date/industry") as
  select 
    date
  , industryId
  , count (distinct companyId) as number_of_companies
  from 
    (
      select date, industryId, companyId, count(*) as number_of_company_events_on_date
      from have
      group by date, industryId, companyId
    )
  group by date, industryId
  ;

* number of companies within industry (way #2);
* use catx to construct the sub-level combination (bins) to be distinctly counted;

 create table counts1B as
 select
   date
 , industryId
 , count (distinct catx(':',industryId,companyId)) as number_of_companies
 group by date, industryId 
 ;

* bonus: just number of industries (ignoring companies);

  create table counts2 (label="Number of industries per date") as
  select 
    date
  , count (distinct industryId) as number_of_industries
  from have
  group by date
  ;

* bonus: disjoint counts of each category (company industry hierarchical relationship ignored);

  create table counts3 (label="Counts for industry and company by date") as
  select 
    date
  , count (distinct industryId) as number_of_industries
  , count (distinct companyId) as number_of_companies
  from have
  group by date
  ;

PROC FREQ是获得该答案的最简单方法

proc freq data=have;
  tables date*industry / list missing;
run;

这将是该行业在给定日期出现的次数。如果每个日期、行业、公司组合只有一个观察值,那么它也是该日期该行业公司数量的计数。

除了Proc freq,您还可以使用First。最后。这个问题的概念

Proc sort data=companies;
by date Industry_category;
run;

Data companies(drop= company_names);
set companies;
by date Industry_category;
If first.Industry_category then count=1;
else count+1;
if last.Industry_category;
run;
`