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
B
ReplyDeleteHi Neha,, can you give explanation for the answer??
ReplyDeleteThere 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.
DeleteU NEED OUTPUT STATEMENT WITHIN A DO LOOP TO GET ALL THE 3 OBSERVATIONS PRINTED...
ReplyDelete1 observation and two variables
ReplyDeleteC
ReplyDeleteAns is B
ReplyDeleteThe four variables are Name, Rate, Year, Capital.
Sample Code
ReplyDeletedata 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;
1
ReplyDelete22 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
Answer is B
ReplyDeletewe 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;
ReplyDeletethis 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
Answer is B
ReplyDeletedata 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;
b is the answer
ReplyDeletethe 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;
How come 'year' is 4?
ReplyDeleteIt would be helpful for newcomers if you guys give us reason. copying and pasting answer will not help in long term.we need explanation.
ReplyDeletesorry for being rude.
Sakar sham:
ReplyDeleteB 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?
name rate capital year
ReplyDeleteare these variables generated?
Who ever commented this: Can you paste your log and out put results?
ReplyDelete""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.
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.
ReplyDelete248
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
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:
ReplyDeletedata 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
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.
ReplyDeletedata 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
I don't understand this output. Why it shows up like this?
Deleteme too, i thought the output should be 9 observations...
Deletewhat is the answer
ReplyDeleteB.
ReplyDeleteNo 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
guys,
ReplyDeleteok 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
The following code works:
Deletedata bank_new;
set banks;
capital + 5000;
run;
remembers the last values meant to say,lol
ReplyDeleteAM 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
ReplyDeletedata newbank;
set banks;
do year = 1 to 3;
capital + 5000;
end;
run;
proc print;
run;
WITH ANS 3 OBSERVATIONS 4 VARIABLES
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.
ReplyDeleteThis question is making you understand that a DO loop requires OUTPUT.
ReplyDeletewhat happen if i change the "do year = 1 to 2"! I don't understand why SAS stops at the second observation.
ReplyDeleteThanks all :)
hello, why is year "4" ? thanks in advance.
ReplyDeleteNOTE: There were 3 observations read from the data set WORK.BANK_NEW.
ReplyDeleteNOTE: 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
B
ReplyDeletesee document below:
ReplyDeletehttp://www2.sas.com/proceedings/sugi23/Begtutor/p50.pdf
might help.
with SET inside DO loop, there is no implied OUTPUT.
Have anyone tried Year = 1 to 1?
ReplyDeleteIt gives me 3 obs with year = 2, and Capital = 5000, 10000, 15000 for each bank...
can anyone explain why? thanks.
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?
ReplyDeleteBecause 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.
ReplyDeleteSince Output statement is not used before the end statement then sas outputs last observation where capital=15000.
ReplyDeletedata 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;