The SAS Output Delivery System and reporting techniques

Do loop problem

Reply
Contributor
Posts: 45

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: 282

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
Super User
Posts: 6,320

Re: Do loop problem

There is a big difference between subsetting if like you are using (IF ENDSmiley Wink 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: 45

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

Obsageyearnumerator_factordenominator_factordenominatornumeratorPercentagedelete_dummy
162111110.00%1
262211220.00%1
363111330.00%1
4632014325.00%1
5641015340.00%1
6641015340.00%0
7642016350.00%1
8642016350.00%0

This is what I would like to get

Obsageyearnumerator_factordenominator_factordenominatornumeratorPercentagedelete_dummy
162111110.00%1
263111220.00%1
3641013233.33%1
4641013233.33%0
562211110.00%1
6632012150.00%1
7642013166.66%1
8642013166.66%0

Any ideas?

Thank you!

Ask a Question
Discussion stats
  • 3 replies
  • 359 views
  • 0 likes
  • 3 in conversation