Hello,
The SAS data set BANKS is listed below:
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
-> The answer for above question is (B). Could anyone please explain how it is being processed to get only 1 observation in final output ?
Thank you!
"B" is correct, but the process is a little more complex.
The DATA step leaves the DATA statement to execute the remaining programming statements. As part of that process, it executes the DO loop (which includes executing the SET statement 3 times and reading all 3 observations from BANKS). At the bottom of the DATA step, it outputs the one observation and returns to the DATA statement.
The DATA step leaves the DATA statement a second time, and starts executing the remaining statements within the DATA step. It starts the DO loop, and attempts to execute the SET statement for the fourth time. SInce there is no fourth observation to be read, the DATA step ends at that point.
You can help illustrate the process by adding this statement immediately following the DO statement:
put _n_= year=;
You can add to the PUT statement, and add other PUT statements as well to help illustrate the process.
A data step has an implicit output; at the end of the step, if no explicit output; has been coded.
Your data step reads three observations from banks, and so reaches the end-of-file condition. After the do loop, the data step reaches the end of its first iteration, writes one observation (no output present in the do loop!), recognizes the EOF, and terminates.
newbank will have the last observation of bank, with year = 4 and capital = 15000 added.
year will be 4 because of the way a do loop with an iteration variable is executed.
Just to add, the data step written this way creates loop which gives you exact control over the lines of the data set banks, in addition it does not output at the end of each step.
You can imagine that the looping variable year corresponds to the line number of the data set. It goes over all 3 lines and outputs outside the loop when it reaches the run statement in the data step(implicit output).
The more accurate explanations are the preceeding ones 🙂 but this could help you to understand...
"B" is correct, but the process is a little more complex.
The DATA step leaves the DATA statement to execute the remaining programming statements. As part of that process, it executes the DO loop (which includes executing the SET statement 3 times and reading all 3 observations from BANKS). At the bottom of the DATA step, it outputs the one observation and returns to the DATA statement.
The DATA step leaves the DATA statement a second time, and starts executing the remaining statements within the DATA step. It starts the DO loop, and attempts to execute the SET statement for the fourth time. SInce there is no fourth observation to be read, the DATA step ends at that point.
You can help illustrate the process by adding this statement immediately following the DO statement:
put _n_= year=;
You can add to the PUT statement, and add other PUT statements as well to help illustrate the process.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.