Home SAS code is giving error for Do-While loop
Reply: 1

SAS code is giving error for Do-While loop

user2827927
1#
user2827927 Published in 2018-02-13 18:11:13Z

I need to print the fuel consumption and mileage of the car in SAS code. if given that mileage is 20 miles per gallon. It should stop generating output if fuel reaches to 10 gallon OR car travel 250 miles. My code:

data milage;
fuel=1;
do while (fuel<11);
miles =  fuel*20;
fuel+1;
output;
end;
run;

My output:

Code Output

The output for fuel needs to be started from 1 for first 20 miles which incorrect in my code. Any suggestion on what I am missing here.

Thanks!!

Reeza
2#
Reeza Reply to 2018-02-13 18:42:58Z
  1. Add an explicit OUTPUT for the first line or start at 0 instead. If you start at 0, make sure the order of the fuel and miles calculation are correct.
  2. Change your loop condition to be <10 and add in the MILES criteria as well. In this case you're only looping if fuel<10 AND the miles lt 250. Make sure the boundaries are what you want.

    data milage;
    fuel=0; miles=0;
    
    do while (fuel<10 and miles lt 250);
    
        fuel+1;
        miles =  fuel*20;
    
        output;
    end;
    
    run;
    
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.297263 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO