Which iterative DO statement is invalid?
Answer : A
In SAS, iterative DO statements are used to repeat a block of statements a specified number of times. These DO statements have a specific syntax that includes initializing a variable, setting an ending value, and specifying an increment (or decrement). The syntax generally follows the pattern: DO variable = start TO end BY increment;
Let's evaluate each option provided:
A) Do 100 to 1200 by 100; This statement is syntactically incorrect because it lacks a variable to iterate over. An iterative DO loop must specify a variable that will take on each value in the specified range. The correct form should be something like do i = 100 to 1200 by 100;.
B) Do num = 1.1 to 1.9 by 0.1; This statement is valid. It initializes the variable num at 1.1 and increments by 0.1 until it reaches 1.9. This is a typical use of the iterative DO loop for non-integer increments.
C) Do year = 2000 to 2016 by 2; This statement is also valid. It initializes year at 2000 and increments by 2, going through values like 2002, 2004, etc., up to and including 2016. This is a standard use for iterating over years or other sequentially numbered items.
D) Do reverse = 10 to 1 by -1; This statement is valid. It initializes reverse at 10 and decrements by 1 until it reaches 1. Using negative increments is a legitimate approach for counting downwards.
Reference
SAS 9.4 Statements: Reference, 'DO Statement.'
SAS Support: DO Loop Documentation
Understanding how to properly format DO loops in SAS is fundamental for effectively managing repetitive tasks in your data analysis. Mistakes like those seen in option A can lead to syntax errors, preventing your code from executing. Always ensure that your loops are correctly structured and that each component of the loop is clearly defined.
Which statement is true about SAS program syntax?
Answer : C
In SAS program syntax, character strings are indeed case sensitive when enclosed in quotation marks. If you compare two character strings of the same letters but different cases, SAS will treat them as different values. For example, 'Hello' is not the same as 'hello'.
Option A is incorrect because the ampersand (&) is not used for comments in SAS; it is used for macro variable references. Option B is incorrect because global statements such as LIBNAME and options do not require a RUN statement to execute. Finally, option D is incorrect because SAS can process steps with multiple statements on the same line, provided that they are correctly separated by semicolons.
Reference
SAS 9.4 Language Reference: Concepts, 'SAS Language Elements and Syntax.'
Which step reads the SASHELP. BASEBALL data set and creates the temporary data set CATCHERS?
Answer : D
The step that reads the SASHELP.BASEBALL data set and creates the temporary data set CATCHERS is:
data catchers;
set sashelp.baseball;
where position='C';
run;
This DATA step creates a new data set named catchers by reading in data from sashelp.baseball and selecting only those observations where the variable position has the value 'C', which stands for catchers.
Which assignment statement uses the SUBSTR function to extract the four-digit year from the value of date?
data days;
date="02Apr2019";
insert-statement-here
run;
Answer : A
In SAS, the SUBSTR function is used to extract a substring from a character string. The function syntax is SUBSTR(string, position, length), where:
string is the variable or string literal you want to extract the substring from.
position is the starting position of the substring within string.
position starts counting at 1 in SAS, not 0 as in some other languages.
length is the number of characters to extract.
For the value of date provided ('02Apr2019'), we want to extract the year, which is the four characters at the end of the string.
Here's how each option would work given the string:
A) year=substr(date, 7, 4); This starts at the 7th character of the string ('2019') and extracts 4 characters, which correctly represents the year.
B) year=substr(date, 4, 6); This starts at the 4th character ('Apr2019') and would extract 6 characters, which gives us 'Apr201', not just the year.
C) year=substr(date, 6, 4); This starts at the 6th character ('r2019') and would extract 4 characters, resulting in 'r201', which is not correct.
D) year=substr(date, 4, 7); This starts at the 4th character and would extract 7 characters, resulting in 'Apr2019', which is the whole string from the 4th character to the end, not just the year.
The correct answer is A, as it extracts the four-digit year from the end of the string.
The SAS documentation on the SUBSTR function, which details how to use it for extracting parts of strings.
SAS programming tutorials that often provide examples of common functions like SUBSTR for string manipulation
Given the following SAS program:

What footnotes appear for the second PROC PRINY report?
Answer : D
In SAS, footnotes are set using the footnote statement and they will appear on all subsequent output until they are either changed or cleared. Based on the second image provided with the SAS code, the footnote for the second PROC PRINT report is set immediately before it runs.
The code sets footnote1 as 'Created by HR' and footnote2 as 'Confidential' initially. However, before the second PROC PRINT step, footnote2 is redefined as 'Draft - Do Not Distribute'. Since footnote1 is not redefined or cleared, it is no longer in effect for the second report.
Therefore, the only footnote that appears for the second PROC PRINT report is what is defined for footnote2 at that point in the code, which is 'Draft -- Do Not Distribute'. That's why the correct answer is D.
SAS 9.4 documentation for the FOOTNOTE statement: SAS Help Center: FOOTNOTE Statement
Which line contains a syntax error?

Answer : A
In the provided code snippet, Line 3 contains a syntax error. The keep statement is used incorrectly here; the correct keyword to use is where in order to filter the dataset. The correct line should be something like:
where Make = 'Honda';
So, the keep statement on Line 3 is not properly used, and also there appears to be a missing quotation mark at the end of 'Honda' which should be closed before the semicolon.
SAS documentation for the where statement.
Given the STUDENTS data set below:

What will be the values for First. State and Last. State for Ellen's observation?
Answer : D
When using a BY statement in a DATA step, SAS creates two temporary variables for each BY variable: FIRST.variable and LAST.variable. These are used to indicate the beginning and end of each BY group.
For Ellen's observation:
First.State=1 because this is the first occurrence of the state 'OH' in the dataset when sorted by the state variable, which makes it the beginning of a new group for 'OH'.
Last.State=0 because Ellen is not the last observation
for the state 'OH' group. In the given dataset snippet, there appears to be only one observation for the state 'OH', but since the data is sorted by state, and we don't see another 'OH' below Ellen, we can deduce that Ellen's observation is not the last 'OH' observation in the entire dataset. If there were no more 'OH' observations following Ellen's, then Last.State would be 1.
The FIRST.variable is set to 1 for the first observation in each BY group and 0 for all other observations in the group. Conversely, LAST.variable is set to 1 for the last observation in each BY group and 0 otherwise. Since we're assuming that the dataset is larger than the snippet shown, and the data continues beyond what is visible, we must assume there are more observations for 'OH' unless indicated otherwise.
Reference
'SAS 9.4 Language Reference: Concepts.' SAS Institute Inc.
'BY-Group Processing' in 'SAS Programming 1: Essentials' course material, SAS Institute.