I'll first give you pointers with your method, then provide my preferred solution.
Your solution, updated with my fixes:
data q2;
*indent please!;
denom =1;
x = 0.3;
*Move this assignment to after x is assigned;
numerator= x;
*initialize sign;
sign = 1;
*initialize tot;
tot=x;
*need an output to start out with;
output;
*WHILE change to UNTIL (req. switching > to <);
*tot changed to term;
do until (term < abs(0.000001));
numerator=x*numerator;
denom=denom+1;
sign=sign*-1;
term=numerator/denom;
*you want to add (sign*term) to the total, right?;
tot=tot + sign*term;
output;
end;
*always include a RUN after data steps, even though not required;
run;
proc print data=q2;
run;
My preferred solution:
*Use the macro language to define things like starting values;
%let x=0.3;
*let us see how close we get to correct!;
%put %sysfunc(log(1.3));
data want;
*initialize sum;
sum = 0;
*loop until we hit the limit. The until here is technically unneeded;
*but I like to include it for clarity;
do term = 1 by 1 until (abs(new) lt 1e-6);
*make the new value to be added (x to the power of term, divided by term;
*times -1 to the power of term plus one to get the right value for the sign);
new = (((&x)**term)/term) * (-1)**(term+1);
*stop iterating if the new term would be too small;
if abs(new) lt 1e-6 then leave;
*add to the sum now and output;;
sum = sum + new;
output;
end;
stop; *stop the data step loop;
run;
You might want to not output that last line, by the way, but that should be easy to add.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…