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

Hi Guys  - i need some help here please...

if i run below code, 1st obs for variable 'capital'  is 1010 and its adding interest in first obs of capital itself...i want first obs for capital same, 1000, and then interest for first obs,10, adds on second obs of capital (not in first obs itself...)...

Thanks!

data test;

  capital=1000;

do month=1 to 12;

  interest=capital*.12/12;

  capital+interest;

output;

end;

run;

-----------------------------------------------

capital     month    interest

1010        1            10

1020.1     2            10.1

------------------------------------------------

but i want this,

capital     month    interest

1000        1            10

1010        2            10.1

1 ACCEPTED SOLUTION

Accepted Solutions
8 REPLIES 8
Anotherdream
Quartz | Level 8

This is a very un-intelligent answer, but you can simply add an if then loop to your process logic. so it would look like this.

data test;

capital=1000;

do month=1 to 12;

if month~=1 then do;

interest=capital*.12/12;

capital+interest;

end;

output;

end;

run;

Likewise you could initalize your values and then start your loop at a later month (whichever is easier).

data test;

capital=1000;

interest=.;

month=1;

output;

do month=2 to 12;

interest=capital*.12/12;

capital+interest;

output;

end;

run;

Reeza
Super User

Move the output statement before the increment statement Smiley Happy

data test;

  capital=1000;

do month=1 to 12;

  interest=capital*.12/12;

  output;

  capital+interest;

end;

run;

sas_9
Obsidian | Level 7

@ Thanks Ano...--- but in both cases,

capital     month    interest

1000        1            .

1010        2            10

1020.1     3            10.1

but i think this is incorrect because for 2nd obs capital value is right, 1010 but interest would be 10.1 which is 10 in your both cases...this is very confusing for me at least...

@ Reeza _ i think it will show only 1 obs 13th with same result as if we use output with all 12 obs......i want to see below...if we can do this ,...

capital     month    interest

1000        1            10

1010        2            10.1

Thanks for your time!

Reeza
Super User

Try it.

art297
Opal | Level 21

What do you want the interest to be for the other 10 months?  Your initial code keeps the rate the same for each month.  Is that what you want?

sas_9
Obsidian | Level 7

sorry Reeza, i misunderstood your post...

its absolutely correct...i thought you are saying REMOVE output statement 🙂

Thanks a lot!

sas_9
Obsidian | Level 7

Yes Sir...

Thanks!

Anotherdream
Quartz | Level 8

Ah I'm sorry I glazed over your want and have too quickly!

I thought you wanted the interest to begin calculating in the second month and be applied at the same time, hence the solution.

Sorry about that!

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
  • 8 replies
  • 970 views
  • 3 likes
  • 4 in conversation