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

 

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

vatemro
Calcite | Level 5

Thanks for your help. The explanation was very simplified and clear

Patrick
Opal | Level 21

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:

https://support.sas.com/documentation/cdl/en/lestmtsref/68024/HTML/default/viewer.htm#p1cydk5fq0u4bf...

 

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;
vatemro
Calcite | Level 5

Am new to programming and am not sure about a lot of the statements. Thanks for your help

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 837 views
  • 0 likes
  • 3 in conversation