Architecting, installing and maintaining your SAS environment

Loop Help

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

Loop Help

data a6;

a=12345;

b=0;

/*do while (A >0);*/

do i=1 to length(a);

b=(b*10)+mod(a,10);

a=int(a/10);

put _all_;

end;

run;

When I run this code instead of 4 My loop is runs for 12 times Can any one explain me .

Regards,

venkay


Accepted Solutions
Solution
‎08-05-2014 11:04 AM
Super User
Posts: 10,454

Re: Loop Help

And if you are trying to reverse the number

b = input(reverse(put(a,f5.)),f5.);

or if looking for the last 4 digits

b = input(substr(reverse(put(a,f5.)),1,4),f5.);

View solution in original post


All Replies
Super User
Posts: 3,100

Re: Loop Help

The reason is you are applying a character function - LENGTH - to a number - 12345. So SAS is automatically converting the number into a character variable using the BEST12. SAS format.

Inside SAS your number now looks like this (ignore the surrounding quotes) - '       12345' - in other words it has 7 blank spaces in front of the number.

So that is why your look runs 12 times. If you changed a=12345 to a='12345' it would loop 5 times.

Solution
‎08-05-2014 11:04 AM
Super User
Posts: 10,454

Re: Loop Help

And if you are trying to reverse the number

b = input(reverse(put(a,f5.)),f5.);

or if looking for the last 4 digits

b = input(substr(reverse(put(a,f5.)),1,4),f5.);

Contributor
Posts: 71

Re: Loop Help

Thank you Smiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 769 views
  • 3 likes
  • 3 in conversation