DATA Step, Macro, Functions and more

Do while loop SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Do while loop SAS

I am stuck to this prob;em please help me out:

 

A bike deliver a milage of 20 miles per gallon. write a code in SAS so that the code stops generating observations when distance reaches 250 miles or when 10 gallons of fuel  have ben used.

 


Accepted Solutions
Solution
‎07-02-2017 10:57 AM
Trusted Advisor
Posts: 1,137

Re: Do while loop SAS

Posted in reply to Nishant_saxena

Consider that you have a dataset with miles and you want to create a new dataset creating a new variable and if this new variable values reaches the 250 mark then you need to stop. Use the do until as i used.

 

data have;
do miles=100 to 1000 by 50;
output;
end;
run;

data want;
do until(mile<=250);
set have;
mile=miles;
end;
run;
Thanks,
Jag

View solution in original post


All Replies
PROC Star
Posts: 7,467

Re: Do while loop SAS

Posted in reply to Nishant_saxena

What have you tried?

 

Art, CEO, AnalystFinder.com

Occasional Contributor
Posts: 11

Re: Do while loop SAS

I was trying some stuff like:

 

data milage;
do gallons= 1 to 10;
while(distance<250);
run;

Solution
‎07-02-2017 10:57 AM
Trusted Advisor
Posts: 1,137

Re: Do while loop SAS

Posted in reply to Nishant_saxena

Consider that you have a dataset with miles and you want to create a new dataset creating a new variable and if this new variable values reaches the 250 mark then you need to stop. Use the do until as i used.

 

data have;
do miles=100 to 1000 by 50;
output;
end;
run;

data want;
do until(mile<=250);
set have;
mile=miles;
end;
run;
Thanks,
Jag
Occasional Contributor
Posts: 11

Re: Do while loop SAS

Posted in reply to Jagadishkatam

This solution is exactly working

Thanks a lot

Valued Guide
Posts: 765

Re: Do while loop SAS

Posted in reply to Nishant_saxena

Hi, do you really need a loop ...


data want;
set have (where=(miles le 250));
mile=miles;
run;

 

If you don't want that new variable, don't use "mile=miles"

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 189 views
  • 0 likes
  • 4 in conversation