简体   繁体   中英

Group by date using proc sql in SAS

I want to sum up all values for the same cost centre per each year in SAS. I tried with Group By function in Proc SQL.

PROC SQL

    CREATE TABLE incident_data AS

        SELECT   Cost_Centre_Dim.Cost_Centre_Name,
                 Sum(Count*Amount) AS TOT,
                 OCC_DATE format= Year4. 
        FROM     Incidents,
                 Incident_Type,
                 Cost_Centre_Dim 
        WHERE    Incidents.Incident_Code = Incident_Type.Incident_Code
        AND      Incidents.Cost_Centre_ID = Cost_Centre_Dim.Cost_Centre_ID
        GROUP BY Cost_Centre_Dim.Cost_Centre_Name,
                 OCC_DATE 
        ORDER BY [Cost Centre Dim].[Cost Centre Name];

QUIT;
RUN;

The output looks like below:

2012  34.41  ACCI2 
2012  34.23  ACCI2 
2014  25.71  INFR1 
2014  27.82  INFR1 
2014  22.26  INFR2 
2014  20.97  INFR2 
2013  22.64  ACCI4 
2013  19.29  ACCI4 
2013  18.26  ACCI4 
2014  35.82  ACCI4 
2015  97.81  INFR3 
2015  44.04  INFR3 
2014  57.09  INFR3 

I want my output to show 1 line for 1 type of incident per year with the add up amount. Sth like this:

2012  68.64  ACCI2  
2014  53.53  INFR1  
2014  43.23  INFR2  
... 

Any advice is really appreciated

Try converting OCC_DATE to text like this: put(OCC_DATE, year4) as MyOCC_DATE

I think the original code is only displaying the date in the format you assigned but it is still the actual numerical value of OCC_DATE. If you change it to character in the format you want then it should do the grouping correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM