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?
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.
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?
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.
thanks for your reply @Astounding, @PGStats
@PGStats, sorry for my mistake. the result is the same as you have mentioned.
Thanks for your explanation .
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.