Sas 另一个表中的多个计数查询
我有一个关于SAS的问题。我们有一个由犯罪构成的表Sas 另一个表中的多个计数查询,sas,Sas,我有一个关于SAS的问题。我们有一个由犯罪构成的表犯罪(每行一个犯罪,包含更多信息,如刑法条款或日期) 每年我们都会收到一份调查问卷,以填写特定类别(如欺诈、杀人等)的犯罪数量。不幸的是,本问卷中的类别与我们自己的类别(刑法条款)不同。所以我们必须定义,哪些文章属于哪个调查类别。这些信息存储在名为定义的at表中 在下面的示例中,有两篇文章(110、111)可概括为“盗窃”,三篇文章可概括为“攻击”: Category Article Query ——————————————————————
犯罪
(每行一个犯罪,包含更多信息,如刑法条款或日期)
每年我们都会收到一份调查问卷,以填写特定类别(如欺诈、杀人等)的犯罪数量。不幸的是,本问卷中的类别与我们自己的类别(刑法条款)不同。所以我们必须定义,哪些文章属于哪个调查类别。这些信息存储在名为定义
的at表中
在下面的示例中,有两篇文章(110、111)可概括为“盗窃”,三篇文章可概括为“攻击”:
Category Article Query
———————————————————————-
Theft 110 select count(*) from crimes where article = 110
Theft 111 select count(*) from crimes where article = 111
Robbery 130 select count(*) from crimes where article = 113
Assault 140 select count(*) from crimes where article = 140
Assault 141 select count(*) from crimes where article = 141
Assault 146 select count(*) from crimes where article = 146
在我们的数据库crimes
中,我们还有一个专栏“article”。现在的目标是了解有多少犯罪
因此,我希望有一个这样的表,该表给出了每个类别的犯罪数量(表定义中定义的类别):
现在的问题是:获得所需结果的最佳方法是什么?我认为,您可以使用此proc-sql
内部联接
:
proc sql;
create table want as
select d.category,count(*) as count
from crimes c
inner join definitions d
on d.Article = c.Article
group by
d.category;
quit;
如果定义表中的文章
和类别
之间存在关系,并且犯罪
表中定义了文章
的一个犯罪有一行,那么它将起作用。我认为,您可以使用这个过程sql
内部联接
:
proc sql;
create table want as
select d.category,count(*) as count
from crimes c
inner join definitions d
on d.Article = c.Article
group by
d.category;
quit;
如果定义表中的文章
和类别
之间存在关系,并且犯罪
表中定义了文章
的一个犯罪有一行,那么它将起作用。一种方法是将定义
表转换为可读入proc格式
,然后使用自定义格式对组进行求和。这可以使以后的代码更易于使用。要创建自定义和动态格式,您只需要以下变量:开始
,结束
,标签
,类别
,以及fmtname
如果您忘记了每个变量代表什么,请检查此项,或者简单地制作一个虚拟格式,并使用cntlout=
选项读取输出表
data fmt;
length fmtname $32.
label $25.
;
set definitions end=eof;
fmtname = 'articlecat'; /* Format name */
type = 'C'; /* Format type. C = Character */
start = article; /* Start lookup value */
end = article; /* End lookup value */
label = category; /* Custom format name to apply to start/end */
output;
/* Optional: Give a label for unknown articles using the special HLO variable */
if(eof) then do;
HLO = 'O'; /* Special variable representing 'other' */
call missing(start, end);
label = 'Unknown';
output;
end;
keep start end fmtname type label hlo;
run;
/* Save the format */
proc format cntlin=fmt;
run;
现在,您可以在程序中的任何其他位置使用您的格式,而无需对定义文件进行任何连接
proc sql noprint;
create table want as
select put(category, $articlecat.) as Category
, count(*) as Count
from crimes
group by calculated Category
;
quit;
一种方法是将定义
表转换为可读入proc格式
的自定义格式,然后使用自定义格式对组进行求和。这可以使以后的代码更易于使用。要制作自定义动态格式,您只需要以下变量:开始
,结束
,标签
、类别
和fmtname
如果您忘记了每个变量代表什么,请检查此项,或者简单地制作一个虚拟格式,并使用cntlout=
选项读取输出表
data fmt;
length fmtname $32.
label $25.
;
set definitions end=eof;
fmtname = 'articlecat'; /* Format name */
type = 'C'; /* Format type. C = Character */
start = article; /* Start lookup value */
end = article; /* End lookup value */
label = category; /* Custom format name to apply to start/end */
output;
/* Optional: Give a label for unknown articles using the special HLO variable */
if(eof) then do;
HLO = 'O'; /* Special variable representing 'other' */
call missing(start, end);
label = 'Unknown';
output;
end;
keep start end fmtname type label hlo;
run;
/* Save the format */
proc format cntlin=fmt;
run;
现在,您可以在程序中的任何其他位置使用您的格式,而无需对定义文件进行任何连接
proc sql noprint;
create table want as
select put(category, $articlecat.) as Category
, count(*) as Count
from crimes
group by calculated Category
;
quit;