turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- ODS and Base Reporting
- /
- Do loop problem

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-29-2014 11:58 AM

Good morning,

I have the following code and a data set of 100 observations. I am asking for 5 loops so given the code below I would expect 505 obervations. Instead, I get 109 observations. Does anybody know why and how to fix it?

do year=1 to 5;

if age<(65-year) then numerator_factor=1; else numerator_factor=0;

denominator_factor=1;

denominator+denominator_factor;

numerator+numerator_factor;

Percentage=1-(Numerator/Denominator);

delete_dummy=1;

output;

if end; delete_dummy=0; percentage=percentage; output;

format percentage percent8.2;

end;

run;

Thank you!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-29-2014 12:26 PM

Hi,

I think it would be easier if we saw the whole of your program and perhaps some data.

The 2nd output statement only gets executed if the condition "if end;" is satisfied, how many observations are you expecting that to be true for? It would have to be true for every observation for the 2nd output to be executed every time.

Regards,

Amir.

Message was edited by: Amir Malik - amended last line.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-29-2014 12:32 PM

There is a big difference between subsetting if like you are using (IF END and an IF/THEN conditional. You probably want to change

if end; delete_dummy=0; percentage=percentage; output;

to

if end then do;

delete_dummy=0;

percentage=percentage;

output;

end;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

01-29-2014 01:54 PM

Good afternoon and thank you for your responses:

This is a partial dataset and the code:

**I have three people, 63, 64, 65 years old. **

ods html;

data employees;

input age @@;

datalines;

62 63 64

run;

**I want to know what percentage of my workforce will retire in 1 year and in 2 years from now.**

**the variable percentage at delete_dummy=0 should give me the answer. **

data retirement;

set employees end=end;

do year=1 to 2;

if age<(65-year) then numerator_factor=1; else numerator_factor=0;

denominator_factor=1;

denominator+denominator_factor;

numerator+numerator_factor;

Percentage=1-(Numerator/Denominator);

delete_dummy=1;

output;

if end then do; delete_dummy=0; percentage=percentage; output;

end;

format percentage percent8.2;

end;

run;

proc print data=retirement;

run;

ods html close;

**This is what I get**

Obs | age | year | numerator_factor | denominator_factor | denominator | numerator | Percentage | delete_dummy |

1 | 62 | 1 | 1 | 1 | 1 | 1 | 0.00% | 1 |

2 | 62 | 2 | 1 | 1 | 2 | 2 | 0.00% | 1 |

3 | 63 | 1 | 1 | 1 | 3 | 3 | 0.00% | 1 |

4 | 63 | 2 | 0 | 1 | 4 | 3 | 25.00% | 1 |

5 | 64 | 1 | 0 | 1 | 5 | 3 | 40.00% | 1 |

6 | 64 | 1 | 0 | 1 | 5 | 3 | 40.00% | 0 |

7 | 64 | 2 | 0 | 1 | 6 | 3 | 50.00% | 1 |

8 | 64 | 2 | 0 | 1 | 6 | 3 | 50.00% | 0 |

**This is what I would like to get**

Obs | age | year | numerator_factor | denominator_factor | denominator | numerator | Percentage | delete_dummy |

1 | 62 | 1 | 1 | 1 | 1 | 1 | 0.00% | 1 |

2 | 63 | 1 | 1 | 1 | 2 | 2 | 0.00% | 1 |

3 | 64 | 1 | 0 | 1 | 3 | 2 | 33.33% | 1 |

4 | 64 | 1 | 0 | 1 | 3 | 2 | 33.33% | 0 |

5 | 62 | 2 | 1 | 1 | 1 | 1 | 0.00% | 1 |

6 | 63 | 2 | 0 | 1 | 2 | 1 | 50.00% | 1 |

7 | 64 | 2 | 0 | 1 | 3 | 1 | 66.66% | 1 |

8 | 64 | 2 | 0 | 1 | 3 | 1 | 66.66% | 0 |

Any ideas?

Thank you!