You have a dataset named 'SALES' with columns 'REGION', 'PRODUCT', and 'AMOUNT'. You want to create a new dataset named 'HIGH SALES' containing only the records where 'AMOUNT is greater than 1000, and only the 'REGION' and 'PRODUCT' columns should be included. Which SAS code snippet would correctly achieve this?
Correct Answer: D
The correct answer is option D. It uses the 'output' statement to write only those records that meet the condition 'AMOUNT > 1000' to the new dataset 'HIGH_SALES'- The 'keep=REGlON PRODUCT' clause ensures that only the specified columns are included in the output dataset. Option A uses a semicolon instead of 'then' after the 'if statement, which is incorrect syntax- Options B and C incorrectly use a semicolon after 'if statement and do not include the 'keep' option- Option E includes 'drop' option, which will remove the 'AMOUNT' column from the output dataset, while the question asks for 'REGION' and 'PRODUCT' to be included.
Question 2
You have a dataset with a variable 'AMOUNT' containing numeric values with decimals. You need to create a new variable 'ROUNDED AMOUNT' by rounding the 'AMOUNT' values to the nearest hundredth using the ROUND function. Which code snippet achieves this correctly?
Correct Answer: B
The ROUND function in SAS takes two arguments: the value to be rounded and the number of decimal places to round to. To round to the nearest hundredth, you need to specify '2' as the second argument, indicating two decimal places. Option B correctly uses '2' as the second argument to the ROUND function, achieving the desired rounding to the nearest hundredth. Other options use incorrect values for the decimal places argument, resulting in rounding to different precision levels.
Question 3
You have a dataset with a variable 'DATE STR' storing dates in the format 'YYYY-MM-DD'. You want to create a new variable 'DATE NUM' storing the date in numeric format (YYYYMMDD). Which code snippet correctly achieves this transformation?
Correct Answer: A
The correct option is A- The INPUT function is used to convert a character string into a numeric date value. The format 'YYMMDD8.' specifies the input format as 'YYYY-MM-DD' and the output format as numeric date. Option B is incorrect as it uses the 'DATE9_' format which is for a different date format. Options C and E use incorrect input formats. Option D simply assigns the character string to the numeric variable, which is incorrect.
Question 4
You have a dataset 'EMPLOYEES' with 'ID', 'NAME', 'SALARY', and 'DEPARTMENT' variables. You need to create a new dataset 'HIGH SALARY DEPT' containing only employees who have a salary higher than the average salary of their respective department. Which combination of statements would achieve this?
Correct Answer: B,D
Both options B and D are correct and will achieve the desired result. Option B uses PROC SQL to achieve the same outcome. It creates a new table 'HIGH SALARY DEPT' by selecting all employees from 'EMPLOYEES' where their salary is greater than the average salary for their respective department This is accomplished using a subquery within the WHERE clause. Option D uses the DATA step with a subquery within the WHERE clause to select only the employees who meet the condition- The subquery uses a SELECT statement with AVG(SALARY) grouped by the DEPARTMENT to compute the average salary for each department It is then compared to the employee's salary to determine if it's higher. Option A is incorrect because the WHERE statement cannot use the aggregate function AVG in this way within the DATA step. Option C is also incorrect because it tries to use the SELECT statement within an IF statement which is not allowed in SAS- Option E is the same as Option A and incorrect for the same reason- The WHERE clause is applied during the data step's input phase, selectively reading only the observations that meet the specified conditions.
Question 5
You have a dataset with customer purchase records, including their customer ID, purchase date, and purchase amount. You want to calculate the average purchase amount for each customer but only for the purchases made in the last three months. Using the DATA step, how would you modify the code to achieve this, taking advantage of BY group processing with FIRST. and LAST. variables?
Correct Answer: D
Option D is the correct answer because it correctly initializes the 'avg_amount' variable to zero for each customer's first record, it only considers purchases within the last three months, and it calculates the average by dividing the accumulated purchase amount by the count of transactions within the three-month window. Options A, B, C, and E either incorrectly initialize the variables or do not correctly compute the average within the specific time frame.