--------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?
A. totquantity + quantity;
B. totquantity = sum(totquantity + quantity);
C. totquantity 0;
sum totquantity;
D. retain totquantity 0;
totquantity = totquantity + quantity;
Click Comment link to get answer
A is the correct answer. :-)
ReplyDeleteD is the correct answer
ReplyDeleteD is incorrect because the 4th record of the data for the variable quantity has a missing value so since t\you have used a sum operator the subsequent values of total quantity will also be missing.
DeleteD is the correct answer.
ReplyDelete(Sum variable should be initialized to 0)
Please do not change the question to get the answer you know. Please.
DeleteTry this:
ReplyDeletedata aa;
x=.;
y=3;
z= x+y;
run;
z is missing.
A is correct.
Only functions can ignore missing values and give a result. The expressions with missing values result in missing values. Here none of the options seem to be correct.
ReplyDeleteSorry I am mistaken. The correct answer is A.
ReplyDeleteD
ReplyDeletea
ReplyDeletesee SAS help & document:
ReplyDeleteMissing Values: When Missing Values are Generated by SAS
Expression cannot ignore missing value, but the SUM function and statement can.
Answer A is correct
ReplyDeleteHere's the output for 'A'. All others gave missing values.
Obs year quantity totquantity
1 1901 2 2
2 1905 1 3
3 1910 6 9
4 1925 . 9
5 1941 1 10
the answer is def d.
ReplyDeleteno need to initialize with zero ..
Deletethis is a default
Ans is A
data coins;
ReplyDeleteinfile datalines;
input year quantity;
retain totquantity 0; ---> optional
totquantity + quantity;
datalines;
1901 2
1905 1
1910 6
1925 .
1941 1
;
run;
output:
Obs year quantity totquantity
1 1901 2 2
2 1905 1 3
3 1910 6 9
4 1925 . 9
5 1941 1 10
cheers,
ferrat
By the way the answer is definitly A!
ReplyDeleteJust try by yourself ;-)
ferrat
When I run in SAS, it appears that A and D produce the results of the non-missing values (per ferrat's above).
ReplyDeleteThe RETAIN will treat a missing value as a zero; it seems here it is being explicitly written in code to ensure this occurs...I would be concerned the 'A' may not always give valid results regarding a missing value?
Any other comments?
Ans is "D",he asked last observation in o/p data set
ReplyDeleteyes the question is abt LAST observation AND for TOTQUANTITY variable=> run the code for A and D and you will see A is the answer.
ReplyDeleteYes the answer is "D",the question says what is
ReplyDeletelast non-missing observation.
A is correct.I cheked all options B,C,D are giving missing values in the last obs.
ReplyDeleteThe answer is A.
ReplyDeleteBoth A and D work. All you need is the sum statement:
ReplyDeletetotquantity + quantity;
Adding the retain statement:
retain totquantity 0;
Does nothing. The sum statement already retains values, and the '0' here is also unnecessary. So, this question isn't exactly great with two right answers... (Well, unless you consider a useless line of code a wrong answer.)
ans i A, it retains the values by itself, no retain command needed..
ReplyDeleteplease any one suggest me correct answer..
ReplyDeleteA is the correct one, when we put the statement
ReplyDeletetotquantity + 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 ( . )
sakar
is D wrong b/c it will be like,
ReplyDelete0+2=2
0+1=1
0+6=6
0+0=missing value
0+1=1
b/c the data step will be ran for each observation?
Please correct me if i'm wrong,
Thx in advance.
Not b/c retain totquantity as 0.
DeleteIt's wrong b/c when you move on to the fourth ob, quantity=. , and then you get totquantity=totquantity+.=., that's the problem
ur explanation is great! some of the previous ones dont help as much. Thank you a lot.
DeleteBut when missing value appears in totquantity+quantity; it would change the result to missing as in totquantity=totquantity +quantity??
Deleteif it encounters a missing value in totqty+qty it doesn't result as a missing value in the o/p whereas if there is a missing value in totqty=totqty+qty it will result as a missing value in the o/p..hope it helps
DeleteA is correct ans i tried it.
ReplyDeleteA is the correct answer.
ReplyDeletedata coins;
infile 'FILESPEC'
input year quantity;
totquantity + quantity;
run;
Output:
Obs year quantity totquantity
1 1901 2 2
2 1905 1 3
3 1910 6 9
5 1941 1 10
Reason:
" RETAIN Statement compared to SUM Statement
Syntax
variable+expression;
Arguments
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.
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.
Oops.Deleted a line in output by mistake. This is the output:
ReplyDeleteObs year quantity totquantity
1 1901 2 2
2 1905 1 3
3 1910 6 9
4 1925 . 9
5 1941 1 10
I am so confusd with this question ,as per my understanding function sum returns the sum of non-missing arguments whereas + operator return missing value if any argument is missing....in that case answer shud be b.
ReplyDeleteplease provide correct explanation
Here is what I think--
Deletetotquantity + quantity; is a sum statement.
What you are thinking is correct but that should have an expression like a + b = c
Here this is not the case hence sum statement will also count missing values "treating them as 0"
The answer is A and not D
ReplyDeletedata sample;
input a $ quantity;
datalines ;
1901 2
1905 1
1910 6
1925 .
1941 1
run;
data sample2;
set sample;
retain totquantity 0;
totquantity = totquantity + quantity;
totquantity2 + quantity;
run;
A. totquantity + quantity correct
ReplyDeleteOR
B or D should be modifide to get the correct answer
retain totqauntity 0;
totquantity = sum(totquantity, quantity)
no since there is missing value cannot use SUM your modifications for B and D will not work
DeleteBut missing value translates to 0, no?
DeleteSum statement:
ReplyDeletevariable+expression;
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.
all are dumbos a and d both r correct......................
ReplyDeleteYou are a dumbo. d is not correct, becuase totquantity = totquantity + quantity would produce missing values after it encounters missing value for quantity.
DeleteGo read SAS document first properly and then call others dumbo.
totquantity + quantity; is sum statement that is A.(the write answer).
ReplyDeletetotquantity = sum(totquantity , quantity); is sum function that is not the syntax of B.
These both work because these can avoid missing values.
But retain totquantity 0;
totquantity = totquantity + quantity; is sum operation and cannot avoid missing value.That is why D is wrong,not because of retain statement.
Please correct me if I have understood it wrong.
i agree
DeleteA
ReplyDeleteANSWER IS A, THIS WILL GIVE YOU MISSING DATA ON THE 4rth and 5th line
ReplyDeletedata coins;
infile datalines;
input year quantity;
retain totquantity 0;
totquantity=totquantity + quantity;
datalines;
1901 2
1905 1
1910 6
1925 .
1941 1
;
run;
proc print data=coins;
run;
A
ReplyDeleteA
ReplyDeleteNice reading, This is an informative information, thanks for sharing this blog.
ReplyDeleteSAS Training in Bangalore