אחת מהפונקציות הכי נפוצות ב – SQL היא count, שכשמה, כן היא, מחזירה לנו את מספר הרשומות בכל קבוצת group by. למרות שלא נראה שצריך דוגמא אביא אחת בכל זאת:
proc sql ;
select
type,
count(*) as Records
from
SASHELP.CARS
group by
type;
quit;
הכוכבית שאנחנו מעבירים כפרמטר לפונקציה הזאת מנחה אותה לספור את השורות בטבלה. אפשר להביא במקום הכוכבית כל שדה אחר בטבלה אבל יש להיזהר מדבר אחד – פונקציית ה – count לא כוללת בספירה ערכים חסרים:
proc sql ;
select
type,
count(*) as Records,
count(cylinders) as Cyliners /* Note the Sports type*/
from
SASHELP.CARS
group by
type;
quit;
עוד שימוש נפוץ ב – count הוא לספירת הערכים השונים הקיימים בשדה מסויים ע"י הוספת המילה distinct בתוך הפונקציה לפני שם העמודה:
proc sql ;
select
type,
count(*) as Records,
count(distinct make) as Makes
from
SASHELP.CARS
group by
type;
quit;
גם במקרה הזה פונקציית ה – count לא סופרת ערכים חסרים. אנחנו יכולים להשתמש בתכונה זאת ליצירה של חישובים מורכבים יותר בצורה פשוטה וקלה. למשל, כדי לספור את כמות היצרנים בכל קטגוריה עם רכבים שעולים יותר מ – 30,000 דולר:
proc sql ;
select
type,
count(*) as Records,
count(distinct make) as Makes,
count(distinct case when msrp>30000 then make else '' end) as Expensive_Make
from
SASHELP.CARS
group by
type;
quit;
חגי