DATA Step, Macro, Functions and more

%Do %while loop

Posts: 39

%Do %while loop

HI i am trying to write a code to calculate the below scenario , 


lets say i started investing with Rs1 , and i will double the money every day , i.e 

1st day=Rs1

2nd day = Rs2

3rd day = Rs4




so on..


i want to know how many days it will take to make my investment in to Rs100000

i am trying the below code but some how not getting the logic to do so , please help... 


DATA investthree;
DO until (value >= 100000);

day + 1;

Posts: 20

Re: %Do %while loop

Posted in reply to soham_sas

Your loop won't resolve.  The below outputs what I think you are looking for.


data x;
	value = 1;
	days = 1;
	do while (value lt 100000);
		value = value*2;
		put 'NOTE: ' days ': ' value;
		days = days+1;
Cameron | Selerity
Super User
Posts: 9,611

Re: %Do %while loop

Posted in reply to soham_sas

That's a simple fomula:

endvalue = startvalue * 2 ** 100000;

Which will, of course, exceed any displayable value. Think of the wise guy demanding rice from the king. 1 grain on the first square of the chess-board, and double on every further square.

Now, if you want to find the iteration where the result exceeds 100000, you need this:

data want;
value = 1;
iteration = 1;
do until (value > 100000);
  value = value * 2;
  iteration + 1;
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 7,860

Re: %Do %while loop

[ Edited ]
Posted in reply to soham_sas

So your question does not match your subject line. You appear to be trying to use a data step DO statement instead of a macro %DO statement.  That will actually make it easier to do what you want since the DO statement is much more powerful than the %DO statement.

664   data investthree;
665     do day=1 by 1 until (value >= 100000);
666       if day=1 then value=1;
667       else value=value*2 ;
668       put day=z4. +1 value= comma10.;
669       output;
670     end;
671   run;

day=0001  value=1
day=0002  value=2
day=0003  value=4
day=0004  value=8
day=0005  value=16
day=0006  value=32
day=0007  value=64
day=0008  value=128
day=0009  value=256
day=0010  value=512
day=0011  value=1,024
day=0012  value=2,048
day=0013  value=4,096
day=0014  value=8,192
day=0015  value=16,384
day=0016  value=32,768
day=0017  value=65,536
day=0018  value=131,072
NOTE: The data set WORK.INVESTTHREE has 18 observations and 2 variables.
Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation