Please can someone explain in details the data steps below and what each data statement is doing.
thanks
data work.invest;
do year = 2010 to 2014;
capital + 3000;
capital + (capital * 0.10);
end;
The do loop repeats the code within the block the number of times specified by the from and to (and step if supplied).
data work.invest;
do year = 2010 to 2014;
capital + 3000;
capital + (capital * 0.10);
end;
In your instance, the loop variable year runs from 2010 to 2014 (default step 1), so 2010, 2011, 2012, 2013.
You can expand the code to look like:
year=2010
add 3000 to capital
add capital multiplied by 0.10 to capital
year=2011
add 3000 to capital
add capital multiplied by 0.10 to capital
...
So year will end up as 2014, and capital will be the end result of those adds.
The do loop repeats the code within the block the number of times specified by the from and to (and step if supplied).
data work.invest;
do year = 2010 to 2014;
capital + 3000;
capital + (capital * 0.10);
end;
In your instance, the loop variable year runs from 2010 to 2014 (default step 1), so 2010, 2011, 2012, 2013.
You can expand the code to look like:
year=2010
add 3000 to capital
add capital multiplied by 0.10 to capital
year=2011
add 3000 to capital
add capital multiplied by 0.10 to capital
...
So year will end up as 2014, and capital will be the end result of those adds.
Thanks for your help. The explanation was very simplified and clear
Is this an interview or study question?
Do you know SAS Language at all? Which bit of the code is not clear to you?
Here the documentation for a do-loop:
Another good way to understand what's happening is to actually execute the code, look at the result and try to understand what's going on. Each "output" statement in below code will write a row to table "work.invest".
data work.invest;
do year = 2010 to 2014;
output;
capital + 3000;
output;
capital + (capital * 0.10);
output;
end;
run;
Am new to programming and am not sure about a lot of the statements. Thanks for your help
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.