DATA Step, Macro, Functions and more

Creating a new data set using DO loop from another data set

Accepted Solution Solved
Reply
Contributor
Posts: 22
Accepted Solution

Creating a new data set using DO loop from another data set

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!


Accepted Solutions
Solution
‎12-20-2016 01:17 AM
Super User
Posts: 5,085

Re: Creating a new data set using DO loop from another data set

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

View solution in original post


All Replies
Super User
Posts: 6,955

Re: Creating a new data set using DO loop from another data set

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 42

Re: Creating a new data set using DO loop from another data set

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

Solution
‎12-20-2016 01:17 AM
Super User
Posts: 5,085

Re: Creating a new data set using DO loop from another data set

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

Contributor
Posts: 22

Re: Creating a new data set using DO loop from another data set

Thank you everyone for the explanations! :-)
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 185 views
  • 0 likes
  • 4 in conversation