DATA Step, Macro, Functions and more

explicit OUTPUT statement in DO loop while using SET statement

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

explicit OUTPUT statement in DO loop while using SET statement

Please clear my confusion on the below explained scenario

I have a dataset salary with below observations 

obs     salary

1         23456

2         12245

3         34500

4         44000

5         34435

 

i ran the below code

 

data tt;
do until(totalsal ge 50000);
set  salary;
totalsal + salary;
end;
run;

and i get result as:

 

obs      salary   totalsal

1         23456  23456

2         12245  35701

3         34500  70201

 

but when i ran the below code

data tt;
do until(totalsal ge 50000);
set  salary;
totalsal + salary;
output;
end;
run;

and i get result as:

 

obs      salary   totalsal

1         23456  23456

2         12245  35701

3         34500  70201

4         44000  114201

5         34435  148636

 

Please explain why is there a difference in the output, as i have already put the condition to continue the process only till

the "TOTALSAL" reaches 50000 or more?

 


Accepted Solutions
Solution
‎08-31-2016 11:58 PM
Respected Advisor
Posts: 4,640

Re: explicit OUTPUT statement in DO loop while using SET statement

The result from the first version of the code actually is:

 

obs salary totalsal
1 34500 70201
2 44000 114201
3 34435 148636

 

When a data step doesn't include an explicit OUTPUT statement, SAS inserts one right before the RUN statement. Thus, in your first version of the code, the OUTPUT is situated outside the DO UNTIL() loop. It will run after the UNTIL condition is met. In the second version of the code, the OUTPUT occurs explicitly within each iteration of the loop, whether the UNTIL condition is met or not.

PG

View solution in original post


All Replies
Super User
Posts: 5,069

Re: explicit OUTPUT statement in DO loop while using SET statement

Actually, you have asked a complex question.  Here's a program you can test to start moving in the right direction.  How does this DATA step end?

 

data tt;

put 'Next, SAS will execute the SET statement.';

set salary;

totalsal + salary;

run;

 

Important clue:  How many messages does the PUT statement write?

Solution
‎08-31-2016 11:58 PM
Respected Advisor
Posts: 4,640

Re: explicit OUTPUT statement in DO loop while using SET statement

The result from the first version of the code actually is:

 

obs salary totalsal
1 34500 70201
2 44000 114201
3 34435 148636

 

When a data step doesn't include an explicit OUTPUT statement, SAS inserts one right before the RUN statement. Thus, in your first version of the code, the OUTPUT is situated outside the DO UNTIL() loop. It will run after the UNTIL condition is met. In the second version of the code, the OUTPUT occurs explicitly within each iteration of the loop, whether the UNTIL condition is met or not.

PG
New Contributor
Posts: 4

Re: explicit OUTPUT statement in DO loop while using SET statement

thanks for your reply @Astounding@PGStats

 

@PGStats, sorry for my mistake. the result is the same as you have mentioned.

Thanks for your explanation .

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 335 views
  • 3 likes
  • 3 in conversation