2016-02-26 305 views
0

我正在計算年平均值和最高和最低溫度的標準偏差,。我的代碼如下:SAS PROC SQL計算平均值時出錯

PROC SQL; 
create table new2 as 
    select date,SUM(max_temp) as max_temp,SUM(min_temp) as min_temp, 
SUM(precip) as precip 
    from WORK.ROCH_DATA 
    group by date; 

create table average2 as 
    select year(date) as year,(date) as year2,avg(max_temp) as maxavg, 
avg(min_temp) as minavg, avg(precip) as avgprecip, std(max_temp) as 
stdmaxtemp, 
    std(min_temp) as stdmintemp,std(precip) as stdprecip 
    from new2 
    group by year; 
QUIT; 

我的代碼最終吐在這樣的東西上;

enter image description here

它重複YEAR2和年份列(我只想要一個用正確的一年,即1916.1917),並保持了一年重新計算。我怎麼才能讓我看看我的數據中50年的單一計算? (即排除重新計算)

+0

問題是這樣的代碼:'年(日期)爲一年,(日期)作爲year'。給兩列命名是沒有意義的。 –

+0

我已經糾正了,但現在它提出了另一個問題。更新一個問題的時刻。 –

+1

刪除您的第一個proc sql,並在您的work.ROCH表中指向第二個。 – Reeza

回答

1

由於這是您第4次提出同樣的問題,這裏有一個答案,但我沒有看到其他幾個人出了什麼問題。 使用proc方法的兩種方法。您可以使用輸出或ODS OUTPUT,我已經編碼了兩者,但它不是必需的。您的最終結果是3個數據集,應該有你想要的數據,可能有不同的格式。

proc means data=roch_data noprint stackods; 
class date; 
format date year4.; 
var max_temp min_temp precip; 
output out=summary_proc_mean1 mean= std= /autoname; 
ods output summary=summary_proc_mean2; 
run; 

使用PROC SQL

PROC SQL; 
create table average2 as 
    select year(date) as year, 
    avg(max_temp) as maxavg, 
    avg(min_temp) as minavg, 
    avg(precip) as avgprecip, 
    std(max_temp) as stdmaxtemp, 
    std(min_temp) as stdmintemp, 
     std(precip) as stdprecip 
    from roch_data 
    group by year; 
QUIT;