gpt4 book ai didi

sas - 如何在 SAS 中绘制简单的线图

转载 作者:行者123 更新时间:2023-12-04 22:39:20 25 4
gpt4 key购买 nike

我的数据结构如下(这些只是样本数据,因为原始数据是 secret 的)

id | crime   | location | crimedate
------------------------------
1 | Theft | public | 2019-01-04
1 | Theft | public | 2019-02-06
1 | Theft | public | 2019-02-20
1 | Theft | private | 2019-03-10
1 | Theft | private | 2019-03-21
1 | Theft | public | 2019-03-01
1 | Theft | private | 2019-03-14
1 | Theft | public | 2019-06-15
1 | Murder | private | 2019-01-04
1 | Murder | private | 2019-10-20
1 | Murder | private | 2019-11-18
1 | Murder | private | 2019-01-01
1 | Assault | private | 2019-03-19
1 | Assault | private | 2019-01-21
1 | Assault | public | 2019-04-11
1 | Assault | public | 2019-01-10
… | … | … | …

我的目标是创建一个线图(时间序列图),显示三项犯罪的数量在一年中的变化情况。因此,我想在 x 轴上显示月份(1-12),在 y 轴上显示每个月的犯罪数量。应该有两行(每个位置一行)。

我从这个代码开始:
DATA new;
SET old;
month=month(datepart(crimedate));
RUN;

PROC sgplot DATA=new;
series x=month y=no_of_crimes / group=location;
run;

但我不知道如何汇总每个月的犯罪数量。任何人都可以给我一个提示吗?我一直在互联网上寻找解决方案,但通常示例只使用已经聚合的数据。

最佳答案

SG 例程将聚合 VBAR 的 Y 轴值。或 HBAR陈述。显示在 SERIES 中的相同聚合信息语句必须来自先验聚合计算,使用 Proc SUMMARY 很容易完成.

此外,要在单独的视觉对象中绘制每个犯罪的计数,您需要 BY CRIME声明,或 Proc SGPANELPANELBY crime .

犯罪日期时间值不必向下转换为日期值,您可以使用适当的 datetime程序中的格式,它们将根据格式化的值自动聚合。

一些模拟犯罪数据的例子:

data have;
do precinct = 1 to 10;
do date = '01jan2018'd to '31dec2018'd;
do seq = 1 to 20*ranuni(123);
length crime $10 location $8;
crime = scan('theft,assault,robbery,dnd', ceil(4*ranuni(123)));
location = scan ('public,private', ceil(2*ranuni(123)));
crime_dt = dhms(date,0,0,floor('24:00't*ranuni(123)));
output;
end;
end;
end;
drop date;
format crime_dt datetime19.;
run;

* shorter graphs for SO answer;
ods graphics / height=300px;

proc sgplot data=have;
title "VBAR all crimes combined by location";
vbar crime_dt
/ group=location
groupdisplay=cluster
;

format crime_dt dtmonyy7.;
run;

proc sgpanel data=have;
title "VBAR crime * location";
panelby crime;
vbar crime_dt
/ group=location
groupdisplay=cluster
;

format crime_dt dtmonyy7.;
run;

proc summary data=have noprint;
class crime_dt crime location;
format crime_dt dtmonyy7.;
output out=freqs;
run;

proc sgplot data=freqs;
title "SERIES all crimes,summary _FREQ_ * location";
where _type_ = 5;
series x=crime_dt y=_freq_ / group=location;
xaxis type=discrete;
run;

proc sgpanel data=freqs;
title "SERIES all crimes,summary _FREQ_ * crime * location";
where _type_ = 7;
panelby crime;
series x=crime_dt y=_freq_ / group=location;
rowaxis min=0;
colaxis type=discrete;
run;

enter image description here
enter image description here
enter image description here
enter image description here

关于sas - 如何在 SAS 中绘制简单的线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57655329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com