ElearnSAS.com

ElearnSAS.com
SAS Learning Platform

Base SAS 33

The SAS data set BANKS is listed below:
BANKS
name rate
FirstCapital 0.0718
DirectBank 0.0721
VirtualDirect 0.0728
The following SAS program is submitted:
data newbank;
do year = 1 to 3;
set banks;
capital + 5000;
end;
run;
Which one of the following represents how many observations and variables will exist in the SAS data set NEWBANK?
A. 0 observations and 0 variables
B. 1 observations and 4 variables
C. 3 observations and 3 variables
D. 9 observations and 2 variables
Click Comment link to get answer

40 comments:

  1. Hi Neha,, can you give explanation for the answer??

    ReplyDelete
    Replies
    1. Anonymous10:13 AM

      There is no output statement with do loop. So do loop consider only last observation in output dataset. If you write a output statement prior to do loop end. then the output will be 3 observation.

      Delete
  2. Anonymous1:53 PM

    U NEED OUTPUT STATEMENT WITHIN A DO LOOP TO GET ALL THE 3 OBSERVATIONS PRINTED...

    ReplyDelete
  3. Anonymous3:52 PM

    1 observation and two variables

    ReplyDelete
  4. Ans is B

    The four variables are Name, Rate, Year, Capital.

    ReplyDelete
  5. Sample Code

    data sasuser.banks;
    input name $ 1-13 rate 6.;
    cards;
    FirstCapital 0.0718
    DirectBank 0.0721
    VirtualDirect 0.0728
    ;

    data sasuser.bank_new;
    do year = 1 to 3;
    set sasuser.banks;
    capital + 5000;
    end;
    run;

    ReplyDelete
  6. 1
    22 data practise.newbank;
    23 do year = 1 to 3;
    24 set practise.banks;
    25 capital + 5000;
    26 end;
    27 run;

    NOTE: There were 3 observations read from the data set PRACTISE.BANKS.
    NOTE: The data set PRACTISE.NEWBANK has 1 observations and 4 variables.
    NOTE: DATA statement used (Total process time):
    real time 0.07 seconds
    cpu time 0.03 seconds

    ReplyDelete
  7. Anonymous5:40 AM

    we have three variables in the dataset, when we set the dataset in the do loop it reads the three observations from data set banks (as the set statement is called thrice) when the do loop terminates the value fro year is 4 and the implicit output is executed at run;

    this causes the observation to be written in the new data set. since all three observations are read in the the do loop the data step gets terminated and only one observations is written

    since we have two additional variables in the data step year and capital we get total of 4 variables

    ReplyDelete
  8. swapna2:42 PM

    Answer is B

    data sasuser.banks;
    input name : $13. rate : 6.4;
    cards;
    FirstCapital 0.0718
    DirectBank 0.0721
    VirtualDirect 0.0728
    ;
    run;
    proc print;
    run;

    data sasuser.bank_new;
    do year = 1 to 3;
    set sasuser.banks;
    capital + 5000;
    end;
    run;
    proc print data=sasuser.bank_new;
    run;

    ReplyDelete
  9. Anonymous2:05 PM

    b is the answer

    the program is :
    data yogi;
    Input name$ rate;
    datalines;
    FirstCapital 0.0718
    DirectBank 0.0721
    VirtualDirect 0.0728
    ;

    data newbank;
    do year = 1 to 3;
    set yogi;
    capital + 5000;
    output;
    end;
    datalines;
    run;

    ReplyDelete
  10. Anonymous6:04 PM

    How come 'year' is 4?

    ReplyDelete
  11. It would be helpful for newcomers if you guys give us reason. copying and pasting answer will not help in long term.we need explanation.
    sorry for being rude.

    ReplyDelete
  12. Anonymous2:39 PM

    Sakar sham:
    B is the right answer. Please paste the output may be more helpful as Ram said.

    Okay, any of you can write it differently so we can get the result by bank then we will get 3 observation and 4 variables ( sorry my sas program corrupted and trying to download new but it does not let me downlaod, any suggestion sakar60@aol.com),

    SAS genius, Show us how to get result by bank?

    ReplyDelete
  13. Anonymous8:23 AM

    name rate capital year

    are these variables generated?

    ReplyDelete
  14. Anonymous9:25 AM

    Who ever commented this: Can you paste your log and out put results?

    ""we have three variables in the dataset, when we set the dataset in the do loop it reads the three observations from data set banks (as the set statement is called thrice) when the do loop terminates the value fro year is 4 and the implicit output is executed at run;

    this causes the observation to be written in the new data set. since all three observations are read in the the do loop the data step gets terminated and only one observations is written

    since we have two additional variables in the data step year and capital we get total of 4 variables ""
    thank you.

    ReplyDelete
  15. Anonymous9:19 AM

    I didn't comment earlier, but I just tried this problem out. Below are my log and output dataset. Since output is not printed within the do loop, and the set statement is within the do loop, all the observations are evaluated, and the output is printed for the last observation evaluated.


    248
    249 data newbank;
    250 do year = 1 to 3;
    251 set banks;
    252 capital + 5000;
    253 end; * capital starts at 0 - only three obs and 4 vars;
    254 run;

    NOTE: There were 3 observations read from the data set WORK.BANKS.
    NOTE: The data set WORK.NEWBANK has 1 observations and 4 variables.
    NOTE: DATA statement used (Total process time):
    real time 0.00 seconds
    cpu time 0.01 seconds


    MY OUTPUT:

    Obs year Name rate capital
    1 4 VirtualD 0.0728 15000

    ReplyDelete
  16. Remya9:23 AM

    Please ignore the comment in my previous code. I was testing something else out. The code that will provide results for all three obs is as follows:

    data newbank;
    do year = 1 to 3;
    set banks;
    capital + 5000;
    output;
    end;
    run;

    The output for the above datastep is:

    Obs year Name rate capital
    1 1 FirstCap 0.0718 5000
    2 2 DirectBa 0.0721 10000
    3 3 VirtualD 0.0728 15000

    ReplyDelete
  17. Remya9:33 AM

    This is the other alternate code with set statement outside the do loop that will yield three obs and 4 vars. However the capital numbers will be different as Capital is evaluated three times for each observation.

    data newbank;
    set banks;
    do year = 1 to 3;
    capital + 5000;
    end;
    run;

    Output :

    Obs Name rate year capital
    1 FirstCap 0.0718 4 15000
    2 DirectBa 0.0721 4 30000
    3 VirtualD 0.0728 4 45000

    ReplyDelete
    Replies
    1. crystal11:49 PM

      I don't understand this output. Why it shows up like this?

      Delete
    2. me too, i thought the output should be 9 observations...

      Delete
  18. what is the answer

    ReplyDelete
  19. Anonymous4:08 PM

    B.
    No output statement, so loop gives out only 1 record at the end of the data step. Also 4 variables will be created : Name rate year capital

    ReplyDelete
  20. Anonymous11:26 PM

    guys,
    ok i understand without the output command SAS goes through the loop 3 times and memories the last values of name and rate along with the 2 new variables but when we have the output,can someone please explain to me how we have this as an output:
    Obs year Name rate capital
    1 1 FirstCap 0.0718 5000
    2 2 DirectBa 0.0721 10000
    3 3 VirtualD 0.0728 15000

    why SAS does not write the three observation each time it goes through the loop.Why dont we have 9 observation ?

    Obs year Name rate capital
    1 1 FirstCap 0.0718 5000
    2 1 DirectBa 0.0721 5000
    3 1 VirtualD 0.0728 5000
    4 2 FirstCap 0.0718 10000
    5 2 DirectBa 0.0721 10000
    6 2 VirtualD 0.0728 10000
    7 3 FirstCap 0.0718 150000
    8 3 DirectBa 0.0721 150000
    9 3 VirtualD 0.0728 150000

    probably a stupid question but i just dont seem to understand the way loop works with datasets

    ReplyDelete
    Replies
    1. Anonymous5:46 PM

      The following code works:

      data bank_new;
      set banks;
      capital + 5000;
      run;

      Delete
  21. Anonymous11:28 PM

    remembers the last values meant to say,lol

    ReplyDelete
  22. AM AGRRE ITS A STUPID QUESTIONSya really its a stupid question ans sas shouldnt give these kind of EXPERIMENTATIONAL questions in certifications better question would hv been

    data newbank;
    set banks;
    do year = 1 to 3;

    capital + 5000;
    end;
    run;
    proc print;
    run;
    WITH ANS 3 OBSERVATIONS 4 VARIABLES

    ReplyDelete
  23. 1 obs and 4 var (year, name , rate and capital). why 1 obs since there is no explicit output, do loop will output only last obs but for year 4 since do loop runs always +1.

    ReplyDelete
  24. Anonymous3:39 AM

    This question is making you understand that a DO loop requires OUTPUT.

    ReplyDelete
  25. what happen if i change the "do year = 1 to 2"! I don't understand why SAS stops at the second observation.
    Thanks all :)

    ReplyDelete
  26. Anonymous2:26 PM

    hello, why is year "4" ? thanks in advance.

    ReplyDelete
  27. NOTE: There were 3 observations read from the data set WORK.BANK_NEW.
    NOTE: PROCEDURE PRINT used (Total process time):
    real time 0.03 seconds
    cpu time 0.04 seconds


    18 data bank_new;
    19 do year = 1 to 3;
    20 set banks;
    21 capital + 5000;
    22 end;
    23 run;

    NOTE: There were 3 observations read from the data set WORK.BANKS.
    NOTE: The data set WORK.BANK_NEW has 1 observations and 4 variables.
    NOTE: DATA statement used (Total process time):
    real time 0.02 seconds
    cpu time 0.00 seconds

    ReplyDelete
  28. Anonymous12:29 AM

    see document below:
    http://www2.sas.com/proceedings/sugi23/Begtutor/p50.pdf

    might help.

    with SET inside DO loop, there is no implied OUTPUT.

    ReplyDelete
  29. Anonymous9:17 PM

    Have anyone tried Year = 1 to 1?
    It gives me 3 obs with year = 2, and Capital = 5000, 10000, 15000 for each bank...

    can anyone explain why? thanks.

    ReplyDelete
  30. Anonymous6:02 PM

    I think the most difficult to understand is : original data set has 3 observations, why it becomes only one after running the program of do-loop. Can one can explain it?

    ReplyDelete
  31. Because the set statement is used inside the do loop and there is no output statement. The key here is the placement of Set and output statement. So only one observation.

    ReplyDelete
  32. Since Output statement is not used before the end statement then sas outputs last observation where capital=15000.

    data newbank;
    input name $ rate;
    datalines;
    FirstCapital 0.0718
    DirectBank 0.0721
    VirtualDirect 0.0728
    ;
    run;

    data oneobservation;
    do year = 1 to 3;
    set shoes;
    capital + 5000;
    /*output;*/
    end;
    run;


    data two_obs;
    do year = 1 to 3;
    set shoes;
    capital + 5000;
    output;
    end;
    run;

    ReplyDelete