В таблице частот указано, сколько раз каждая отдельная комбинация значений переменных встречается в наборе данных.Каждая комбинация также известна как «корзина».Количество бинов в таблице частот можно назвать «количеством элементов» или числом различных значений.
Существует много способов создания таблицы частот в 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
;