Base SAS 50

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?
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

36 comments:

  1. A is the correct answer. :-)

    ReplyDelete
  2. Anonymous7:38 PM

    D is the correct answer

    ReplyDelete
  3. Anjali7:39 PM

    D is the correct answer.
    (Sum variable should be initialized to 0)

    ReplyDelete
  4. MySASWorld7:56 PM

    Try this:
    data aa;
    x=.;
    y=3;
    z= x+y;
    run;

    z is missing.

    A is correct.

    ReplyDelete
  5. praveen2:40 PM

    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.

    ReplyDelete
  6. praveen2:43 PM

    Sorry I am mistaken. The correct answer is A.

    ReplyDelete
  7. Anonymous12:31 PM

    see SAS help & document:

    Missing Values: When Missing Values are Generated by SAS

    Expression cannot ignore missing value, but the SUM function and statement can.

    ReplyDelete
  8. Answer A is correct
    Here'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

    ReplyDelete
  9. proudy9:01 AM

    the answer is def d.

    ReplyDelete
    Replies
    1. Anonymous3:54 AM

      no need to initialize with zero ..
      this is a default
      Ans is A

      Delete
  10. Anonymous5:33 AM

    data coins;
    infile 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

    ReplyDelete
  11. Anonymous3:52 PM

    By the way the answer is definitly A!
    Just try by yourself ;-)
    ferrat

    ReplyDelete
  12. Anonymous7:08 AM

    When I run in SAS, it appears that A and D produce the results of the non-missing values (per ferrat's above).

    The 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?

    ReplyDelete
  13. Ans is "D",he asked last observation in o/p data set

    ReplyDelete
  14. Anonymous2:36 PM

    yes 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.

    ReplyDelete
  15. Anonymous1:41 PM

    Yes the answer is "D",the question says what is
    last non-missing observation.

    ReplyDelete
  16. Anonymous12:07 PM

    A is correct.I cheked all options B,C,D are giving missing values in the last obs.

    ReplyDelete
  17. Anonymous3:16 PM

    The answer is A.

    ReplyDelete
  18. Both A and D work. All you need is the sum statement:

    totquantity + 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.)

    ReplyDelete
  19. Anonymous6:35 AM

    ans i A, it retains the values by itself, no retain command needed..

    ReplyDelete
  20. Anonymous5:15 PM

    please any one suggest me correct answer..

    ReplyDelete
  21. Anonymous5:35 PM

    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 ( . )
    sakar

    ReplyDelete
  22. Anonymous3:33 AM

    is D wrong b/c it will be like,
    0+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.

    ReplyDelete
    Replies
    1. Anonymous4:41 PM

      Not b/c retain totquantity as 0.
      It's wrong b/c when you move on to the fourth ob, quantity=. , and then you get totquantity=totquantity+.=., that's the problem

      Delete
  23. A is correct ans i tried it.

    ReplyDelete
  24. A is the correct answer.

    data 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.

    ReplyDelete
  25. Oops.Deleted a line in output by mistake. This is the output:

    Obs year quantity totquantity

    1 1901 2 2
    2 1905 1 3
    3 1910 6 9
    4 1925 . 9
    5 1941 1 10

    ReplyDelete
  26. Anonymous6:15 AM

    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.
    please provide correct explanation

    ReplyDelete
  27. Anonymous9:53 AM

    The answer is A and not D

    data 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;

    ReplyDelete
  28. A. totquantity + quantity correct

    OR

    B or D should be modifide to get the correct answer
    retain totqauntity 0;
    totquantity = sum(totquantity, quantity)

    ReplyDelete
    Replies
    1. Anonymous3:59 AM

      no since there is missing value cannot use SUM your modifications for B and D will not work

      Delete
    2. Anonymous2:10 AM

      But missing value translates to 0, no?

      Delete
  29. Anonymous8:34 PM

    Sum statement:
    variable+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.

    ReplyDelete
  30. all are dumbos a and d both r correct......................

    ReplyDelete