Question : Which one of the following is true of the RETAIN statement in a SAS DATA step program? 1. It can be used to assign an initial value to _N_ . 2. It is only valid in conjunction with a SUM function. 3. Access Mostly Uused Products by 50000+ Subscribers 4. It adds the value of an expression to an accumulator variable and ignores missing values.
Explanation: It adds the value of an expression to an accumulator variable and ignores missing values. The RETAIN statement - is a compile-time only statement that creates variables if they do not already exist - initializes the retained variable to missing before the first execution of the DATA step if you do not supply an initial value - has no effect on variables that are read with SET, MERGE, or UPDATE statements. First part "It adds the value of an expression to an accumulator variable."
"RETAIN variable specifies the name of the accumulator variable, which contains a numeric value. Tips:The variable is automatically set to 0 before SAS reads the first observation. The variable's value is retained from one iteration to the next, as if it had appeared in a RETAIN statement. To initialize a sum variable to a value other than 0, include it in a RETAIN statement with an initial value.
RETAIN expression is any SAS expression. Tips:The expression is evaluated and the result added to the accumulator variable. SAS treats an expression that produces a missing value as zero. " - Sas help and documentation
Question : A raw data file is listed below: --------10-------20-------30 1901 2 1905 1 1910 6 1925 . 1941 1 The following SAS program is submitted and references the raw data file above: data coins; infile 'file-specification'; input year quantity;
run; Which one of the following completes the program and produces a non-missing value for the variable TOTQUANTITY in the last observation of the output data set?
z is missing. Missing Values: When Missing Values are Generated by SAS
Expression cannot ignore missing value, but the SUM function and statement can.
A is the correct one, when we put the statement totquantity + quantity , it initializes to zero in the first iteration and adds the quantity and gets the fist totquantity. and so forth.
retain will not work in this data set. if you put the retain statement it will stop once reads the missing value the totquantity will be missing value ( . )
Question : A raw data file is listed below: --------10-------20-------30 squash 1.10 apples 2.25 juice 1.69 The following SAS program is submitted using the raw data file above: data groceries; infile 'file-specification'; input item $ cost;
run; Which one of the following completes the program and produces a grand total for all COST values?
Explanation: : sum is a function which means it will need brackets () so A is not the correct answer. 1 has incorrect syntax and will not compile. The idea of "sum cost" has some merit in the context of a proc print situation, but used in an assignment statement like this, it's just garbage.
There no such variable as grandtot, so program will give syntax error, so option B and D is not the correct answer.
2 will actually perform sum(.,cost) for each iteration... assigns this result (=cost) to grandtot, so the result is that grandtot equals the newly read value of cost each time... because for each iteration grandtot is reset to missing, in the absence of a 'retain' SAS has no memory of the previous value. It compiles alright, but it doesn't produce the goods.
3 output grandtot; does NOT mean output the value of grandtot to the groceries dataset. It means send the output to a particular dataset called "grandtot". Since this dataset doesn't in fact exist, as it wasn't declared in the data statement at the top, this is a compile time error.
4 is the correct option because retain statement is creating a new variable and initializing it with zero. Sum function will add cost to grandtot for each observation and as grandtot is retained it will carry forward the added value.