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?
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.
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
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.
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 16. Read more here about why you should contribute and what is in it for you!
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.