BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Amy0223
Quartz | Level 8

Hi, I'm not sure how to code this SAS problem below. Any help is greatly appreciated. Thank you!

 

  1. Calculate the average of the two highest salaries for employees who have three salaries and average of two salaries if they have only two salaries. Print the names of employees who have the highest average in their department. 
         Employee_id   Name gender   years   dept     salary01 salary02  salary03;
		 
                 1 Mitchell, Jane A  f    6     shoe     22,450  23,000  26,600
		 2 Miller, Frances T  f  8  appliance      .     32,500  33,000
		 3 Evans, Richard A  m   9  appliance    42,900  43,900  .
		 4 Fair, Suzanne K  f    3  clothing     29,700  32,900 34,500
		 5 Meyers, Thomas D  m  5  appliance     33,700  34,400  37,000
		 6 Rogers, Steven F  m  3    shoe         27,000  27,800  .
		 7 Anderson, Frank F  m  5  clothing      33,000  35,100  36,000
		10 Baxter, David T  m  2     shoe         23,900    .    31,300
		11 Wood, Brenda L  f  3     clothing      33,000  34,000  35,700
		12 Wheeler, Vickie M  f  7  appliance     31,500  33,200  35,600
		13 Hancock, Sharon T  f  1  clothing      21,000   .     22,500
		14 Looney, Roger M    m  10 appliance     42,900 36,200 37,800
		15 Fry, Marie E     f    6  clothing      29,700 30,500 31,200
1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@Amy0223:

In your input sample set, please include only the variables relevant to the task, as in the sample below.

data have ;                                                                                                                                                                                                                                                     
  input id name $ (salary01-salary03) (:comma.) ;                                                                                                                                                                                                               
  cards ;                                                                                                                                                                                                                                                       
 1  Mitchell  22,450  23,000  26,600                                                                                                                                                                                                                            
 2  Miller      .     32,500  33,000                                                                                                                                                                                                                            
 3  Evans     42,900  43,900    .                                                                                                                                                                                                                               
 4  Fair      29,700  32,900  34,500                                                                                                                                                                                                                            
 5  Meyers    33,700  34,400  37,000                                                                                                                                                                                                                            
 6  Rogers    27,000  27,800    .                                                                                                                                                                                                                               
 7  Anderson  33,000  35,100  36,000                                                                                                                                                                                                                            
10  Baxter    23,900    .     31,300                                                                                                                                                                                                                            
11  Wood      33,000  34,000  35,700                                                                                                                                                                                                                            
12  Wheeler   31,500  33,200  35,600                                                                                                                                                                                                                            
13  Hancock   21,000    .     22,500                                                                                                                                                                                                                            
14  Looney    42,900  36,200  37,800                                                                                                                                                                                                                            
15  Fry       29,700  30,500  31,200                                                                                                                                                                                                                            
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data want ;                                                                                                                                                                                                                                                     
  set have ;                                                                                                                                                                                                                                                    
  mean = mean (largest (1, of salary:), largest (2, of salary:)) ;                                                                                                                                                                                              
run ;                                                                

The LARGEST function ignores missing values, so there's no need for any extra "two-three" logic. For the first argument, 1 means "first largest" and 2 means "second largest".

 

Kind regards

Paul D.  

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You could use CALL SORTN to make sure that the two highest values are always in variables salary02 and salary03, and then average salary02 and salary03.

 

Example:

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lefunctionsref&docsetTarget=p...

--
Paige Miller
PaigeMiller
Diamond | Level 26

I don't know why the title was changed by either original author or a moderator, but the title "SAS coding question" is a relatively meaningless title (almost every question in the forum could be titled "SAS coding question"); originally it was "SAS question-how to code average of highest values", a much more meaningful and specific title, that cannot apply to almost every question in the forum.

 

No one benefits from meaningless titles. Everyone benefits from meaningful titles. Whoever changed the title, please change it back.

--
Paige Miller
hashman
Ammonite | Level 13

@Amy0223:

In your input sample set, please include only the variables relevant to the task, as in the sample below.

data have ;                                                                                                                                                                                                                                                     
  input id name $ (salary01-salary03) (:comma.) ;                                                                                                                                                                                                               
  cards ;                                                                                                                                                                                                                                                       
 1  Mitchell  22,450  23,000  26,600                                                                                                                                                                                                                            
 2  Miller      .     32,500  33,000                                                                                                                                                                                                                            
 3  Evans     42,900  43,900    .                                                                                                                                                                                                                               
 4  Fair      29,700  32,900  34,500                                                                                                                                                                                                                            
 5  Meyers    33,700  34,400  37,000                                                                                                                                                                                                                            
 6  Rogers    27,000  27,800    .                                                                                                                                                                                                                               
 7  Anderson  33,000  35,100  36,000                                                                                                                                                                                                                            
10  Baxter    23,900    .     31,300                                                                                                                                                                                                                            
11  Wood      33,000  34,000  35,700                                                                                                                                                                                                                            
12  Wheeler   31,500  33,200  35,600                                                                                                                                                                                                                            
13  Hancock   21,000    .     22,500                                                                                                                                                                                                                            
14  Looney    42,900  36,200  37,800                                                                                                                                                                                                                            
15  Fry       29,700  30,500  31,200                                                                                                                                                                                                                            
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data want ;                                                                                                                                                                                                                                                     
  set have ;                                                                                                                                                                                                                                                    
  mean = mean (largest (1, of salary:), largest (2, of salary:)) ;                                                                                                                                                                                              
run ;                                                                

The LARGEST function ignores missing values, so there's no need for any extra "two-three" logic. For the first argument, 1 means "first largest" and 2 means "second largest".

 

Kind regards

Paul D.  

Amy0223
Quartz | Level 8
Thank you so much! I greatly appreciate your kind help!
Patrick
Opal | Level 21

@Amy0223 

Then may-be mark the answer that helped you the most as solution - and eventually also give likes to answers that were helpful.

By marking an answer as solution your question gets "ticked-off" so people can already see in the overview list that it's resolved. Plus: It's also giving someone the credit for spending time for you and providing a solution.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1709 views
  • 1 like
  • 4 in conversation