Contributor
Posts: 48

# Do loop problem

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!

Super Contributor
Posts: 324

## Re: Do loop problem

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.

Super User
Posts: 7,938

## Re: Do loop problem

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;

Contributor
Posts: 48

## Re: Do loop problem

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!

Discussion stats
• 3 replies
• 391 views
• 0 likes
• 3 in conversation