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

Hi, here's my simple code:

 

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

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

 

It gives 1 observation with 4 variables.

I do understand 4 variables - year, name, rate, capital but not 1 observation.

year it repeating 3 times - and per a iteration banks ( 3 observations) are loaded into.

Thus should it have 3 times * 3 observations = 9 observations in total?

 

 

 

P.S: 1 really really basic question. The code above doesnot fully embrace all character of 'name' field.

I have tried length name 15 but doesn't work. Is there any other way?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data newbank;
do year = 1 to 3;
put year;
set banks;
capital + 5000;
end;
run;

If there is no explicit output statement in a data step, the data step compiler adds an implicit one at the end of the code, so it would look like

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

In the first iteration of the data step, the do loop reads all 3 observations in the source data set, and the end state of the do loop is written to newbank. In the second iteration of the data step, it encounters a EOF condition in set banks in the first iteration of the do loop, and terminates without further output.

@art297's solution corrects all that by moving the set statement out of the loop (therefore only one read per datastep iteration), and adding the explicit output.

View solution in original post

2 REPLIES 2
art297
Opal | Level 21

The first problem can be overcome by using an informat. The second requires some changes to the structure of your code. e.g.:

data banks;
  informat name $14.;
  input name rate;
  datalines;
FirstCapital 0.0718
DirectBank 0.0721
VirtualDirect 0.0728
;
run;

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

Art, CEO, AnalystFinder.com

 

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

If there is no explicit output statement in a data step, the data step compiler adds an implicit one at the end of the code, so it would look like

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

In the first iteration of the data step, the do loop reads all 3 observations in the source data set, and the end state of the do loop is written to newbank. In the second iteration of the data step, it encounters a EOF condition in set banks in the first iteration of the do loop, and terminates without further output.

@art297's solution corrects all that by moving the set statement out of the loop (therefore only one read per datastep iteration), and adding the explicit output.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 754 views
  • 0 likes
  • 3 in conversation