/* Chapter 3: More Building Blocks */

/* Jump to: Chapter 2    Table of Contents    Chapter 4 */

/*

*/

/* Chapter 3: Introductory Section */

/* Jump to: Next Section */

PROC SQL;                                                                       
CREATE TABLE teens AS                                                           
SELECT       name AS FName,                                                     
             age                                                                
FROM         sashelp.class                                                      
WHERE        age>12                                                             
;                                                                               
QUIT;                                                                           

/* Jump to: Top of Section     Top of Chapter */

/* 3.1: Combining Summary Statistics with Original Detail */

/* Jump to: Next Section */

PROC FREQ DATA=teens NOPRINT;                                                   
TABLES age / OUT=cohorts(DROP=percent RENAME=(count=Many) );                    
RUN;                                                                            
                                                                                
PROC SORT DATA=teens OUT=sorted;                                                
BY age;                                                                         
RUN;                                                                            
                                                                                
DATA detail_and_counts;                                                         
MERGE sorted cohorts;                                                           
BY age;                                                                         
RUN;                                                                            
                                                                                
PROC SORT DATA=detail_and_counts;                                               
BY fname;                                                                       
RUN;                                                                            
                                                                                
PROC SQL;                                                                       
CREATE TABLE detail_and_counts AS                                               
SELECT       fname,                                                             
             age,                                                               
             COUNT(*) AS Many                                                   
FROM         teens                                                              
GROUP BY     age                                                                
ORDER BY     fname                                                              
;                                                                               
QUIT;                                                                           

/* Jump to: Top of Section     Top of Chapter */

/* 3.2: Summary Statistics Based on Distinct Values */

/* Jump to: Next Section */

PROC MEANS DATA=teens MEAN MAXDEC=3;                                            
VAR age;                                                                        
RUN;                                                                            
                                                                                
PROC FREQ DATA=teens NOPRINT;                                                   
TABLES age / out=freq2means(KEEP = age);                                        
RUN;                                                                            
                                                                                
PROC MEANS DATA=freq2means MEAN MAXDEC=3;                                       
VAR age;                                                                        
RUN;                                                                            
                                                                                
PROC SQL;                                                                       
SELECT       MEAN(         age)                                                 
              LABEL =   'Weighted' FORMAT=8.3,                                  
             MEAN(DISTINCT age)                                                 
              LABEL = 'Unweighted' FORMAT=8.3                                   
FROM         teens                                                              
;                                                                               
QUIT;                                                                           
                                                                                
PROC SQL;                                                                       
SELECT       DISTINCT MEAN (age) LABEL='DISTINCT MEAN (age)'                    
FROM         teens                                                              
;                                                                               
QUIT;                                                                           
                                                                                
PROC SQL;                                                                       
SELECT       age                                                                
FROM         teens                                                              
;                                                                               
QUIT;                                                                           

/* Jump to: Top of Section     Top of Chapter */

/* 3.3: Preprocessing the Source with Inline Views */

/* Jump to: Next Section */

PROC FREQ DATA=teens NOPRINT ORDER=FREQ;                                        
TABLES age / OUT=highlow(DROP=percent RENAME=(count=Many) );                    
RUN;                                                                            
                                                                                
DATA largest;                                                                   
SET highlow;                                                                    
IF many < LAG(many) THEN STOP;                                                  
RUN;                                                                            
                                                                                
PROC SQL;                                                                       
CREATE TABLE temp AS                                                            
SELECT       age,                                                               
             count(*) AS Many                                                   
FROM         teens                                                              
GROUP BY     age                                                                
;                                                                               
                                                                                
CREATE TABLE largest AS                                                         
SELECT       *                                                                  
FROM         temp                                                               
HAVING       many = MAX(many)                                                   
;                                                                               
QUIT;                                                                           
                                                                                
PROC SQL;                                                                       
CREATE TABLE largest AS                                                         
SELECT       *                                                                  
FROM         ( SELECT       age,                                                
                            count(*) AS Many                                    
               FROM         teens                                               
               GROUP BY     age                                                 
             )                                                                  
HAVING       many = MAX(many)                                                   
;                                                                               
QUIT;                                                                           

/* Jump to: Top of Section     Top of Chapter */