BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PoojaP1
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

"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

4 REPLIES 4
Kurt_Bremser
Super User

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.

chrej5am
Quartz | Level 8

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

Astounding
PROC Star

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

PoojaP1
Fluorite | Level 6
Thank you everyone for the explanations! 🙂

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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