如何打印(并导出到文件)每月和每周的平均值?数据存储在库中,表格如下:
Obs. Date Value
1 08FEB2016:00:00:00 29.00
2 05FEB2016:00:00:00 29.30
3 04FEB2016:00:00:00 29.93
4 03FEB2016:00:00:00 28.65
5 02FEB2016:00:00:00 28.40
(...)
3078 08MAR2004:00:00:00 32.59
3079 05MAR2004:00:00:00 32.75
3080 04MAR2004:00:00:00 32.05
3081 03MAR2004:00:00:00 31.82
编辑:我以某种方式设法得到月度数据,但我分别每个月返回平均值。我想把它作为一个结果完成,即Month-Average +将它导出到文件或数据集。而且我仍然不知道如何应对周。
%macro printAvgM(start,end);
proc summary data=sur1.dane(where=(Date>=&start
and Date<=&end)) nway;
var Value;
output out=want (drop=_:) mean=;
proc print;
run;
%mend printAvgM;
%printAvgM('01jan2003'd,'31jan2003'd);
EDIT2:这是我的代码,一步一步:
libname sur 'C:\myPath';
run;
proc import datafile="C:\myPath\myData.csv"
out=SUR.DANE
dbms=csv replace;
getnames=yes;
run;
proc sort data=sur.dane out=sur.dane;
by Date;
run;
libname sur1 "C:\myPath\myDB.accdb";
run;
proc datasets;
copy in=sur out=sur1;
select dane;
run;
data sur1.dane2;
set sur1.dane;
date2=datepart(Date);
format date2 WEEKV11.;
run;
最后一步产生NOTE: SAS variable labels, formats, and lengths are not written to DBMS tables.
,dane2变量的格式为DATETIME19.
。
答案 0 :(得分:1)
好的,它足够小,可以轻松处理。我建议首先使用DATEPART()函数将datetime变量转换为日期变量,然后在PROC MEANS中使用格式。您可以查看WEEKU和WEEKV格式,看看它们是否符合您的需求。下面的代码应该足以让您入门。您可以在没有日期转换的情况下每月执行一次,但我找不到datetime变量的每周格式。
*Fake data generated;
data fd;
start=datetime();
do i=1 to 3000000 by 120;
datetime=start+(i-1)*30;
var=rand('normal', 25, 5);
output;
end;
keep datetime var;
format datetime datetime21.;
run;
*Get date variable;
data fd_date;
set fd;
date_var = datepart(datetime);
date_month = put(date_var, yymon7,);
Date_week = put(date_var, weekv11.);
run;
*Monthly summary;
proc means data=fd_date noprint nway;
class date_var;
var var;
output out=want_monthly mean(var)=avg_var std(var)=std_var;
format date_var monyy7.;
run;
*Weekly summary;
proc means data=fd_date noprint nway;
class date_var;
var var;
output out=want_weekly mean(var)=avg_var std(var)=std_var;
format date_var weekv11.;
run;
将date_var替换为新的每月和每周变量。因为这些是字符变量,所以它们不能正确排序。