BookmarkSubscribeRSS Feed
PierreYvesILY
Pyrite | Level 9

dear SAS experts,

 

I have to calculate then sums and means on a year to date Basis of a large amount of data. I use a Format suggested by Paige Miller and that works perfectly well:

 

* Format für die Berechnung der Durchschnitte für n Monate ;
proc format;
value ytd (multilabel) 
1							='Jan'
1,2							='Feb' 
1,2,3						='Mar'
1,2,3,4						='Apr'
1,2,3,4,5					='Mai'
1,2,3,4,5,6					='Jun'
1,2,3,4,5,6,7				='Jul'
1,2,3,4,5,6,7,8				='Aug'
1,2,3,4,5,6,7,8,9			='Sep'
1,2,3,4,5,6,7,8,9,10		='Oct'
1,2,3,4,5,6,7,8,9,10,11		='Nov'
1,2,3,4,5,6,7,8,9,10,11,12	='Dec'
; 
run;

My data are in a dataset with lots of variables - I'll post only Pictures of a limited view.

To calculate the YTD sums I used the following Code:

* Berechnung der monatlichen Durchschnitte ;
proc summary data=Dashboard_Primaerdaten;
class mr_group marktregion_bt nlbez_bt mbrbez_bt filbez_bt jahr ;
class monat/mlf;
var ORB: NGS: DrK: ;
types () 
jahr 
jahr*monat 
jahr*monat*mr_group
jahr*monat*mr_group*marktregion_bt 
jahr*monat*mr_group*marktregion_bt*nlbez_bt 
jahr*monat*mr_group*marktregion_bt*nlbez_bt*mbrbez_bt 
jahr*monat*mr_group*marktregion_bt*nlbez_bt*mbrbez_bt*filbez_bt
;
output out=FIL3_bis (drop=_:) sum=;
format monat ytd.;
run;

and got the following CORRECT results:

SAS_Bild_0421_1.PNG

 

To calculate the YTD means, I used the following Code:

* Berechnung der monatlichen Durchschnitte ;
proc summary data=Dashboard_Primaerdaten;
class mr_group marktregion_bt nlbez_bt mbrbez_bt filbez_bt jahr ;
class monat/mlf;
var ORB: NGS: DrK: ;
types () 
jahr 
jahr*monat 
jahr*monat*mr_group
jahr*monat*mr_group*marktregion_bt 
jahr*monat*mr_group*marktregion_bt*nlbez_bt 
jahr*monat*mr_group*marktregion_bt*nlbez_bt*mbrbez_bt 
jahr*monat*mr_group*marktregion_bt*nlbez_bt*mbrbez_bt*filbez_bt
;
output out=FIL3_bis (drop=_:) mean=;
format monat ytd.;
run;

and I got the following results, which are NOT correct:

 

SAS_Bild_0421_2.PNG

Obviously, the values of ORB_Anzahl_Akt are false ; the value for Jahr = 2000 and Monat = ' ' should be 332913 / 12 for instance, instead of 31.0119

What is the Problem in the Code?

How can I fix this?

 

Regards,

PY

7 REPLIES 7
PaigeMiller
Diamond | Level 26

When you ask for the mean, you get sum/N (that's how the mean is defined) which is 332913/N, and not 332913/12. You probably ought to do the calculation of 332913/12 in a DATA step.

--
Paige Miller
FreelanceReinh
Jade | Level 19

Hello @PierreYvesILY,

 

Just as a side note: Perhaps you aren't really happy with the alphabetical sort order of the month names in your PROC SUMMARY output (making it hard to recognize that the values are cumulative YTD sums). You can fix this by adding two more options to the CLASS statement for the month variable:

class monat / mlf order=data preloadfmt;
PierreYvesILY
Pyrite | Level 9
very good tip, thanks a lot!
mkeintz
PROC Star

Instead of MONAT, consider using a YTD analog, via an intermediate data set view:

 

data vneed/ view=vneed;
  set Dashboard_Primaerdaten;
  do ytd_group=1 to monat; output; end;
run;

proc summary data=vneed;
  class mr_group marktregion_bt nlbez_bt mbrbez_bt filbez_bt jahr ytd_group ;
  ... 
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
PierreYvesILY
Pyrite | Level 9
I'll try this and send you feedback
PierreYvesILY
Pyrite | Level 9

hello Reinhardt,

 

the proposed Code doesn't Change anything to my previous (false) results.

This is not the right way, unfortunately.

PierreYvesILY
Pyrite | Level 9
sorry, this post was for mkeintz

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 683 views
  • 5 likes
  • 4 in conversation