Hello,
In this program, I wonder why IF statement does not work. I have inherited programs like this ie. using IF statement after OUTPUT statement.
data a; input a; cards; 1 2 3 ; run; data b; input b; cards; 4 5 6 ; run; data want; set a; do i = 1 to nobs; set b point=i nobs = nobs; output; end; if b > 5; run;
Your example is a subsetting IF, any IF that operated on a variable that was retained from obs to obs would be affected.
This is a useless example, but I know for a fact that I've used this to increment a value after the OUTPUT statement on purpose to retain a value across rows. There are multiple ways to do this and I can't come up with a good example but it has it's uses. Note that in the example below the effective rate in the log is different than the effective rate on the data set.
data have;
do i=1 to 100;
rate=0.05; deposit=100;
output;
end;
run;
data want;
set have;
retain effective_rate 0.03 x 0;
x=deposit+x*(1+effective_rate);
output;
effective_rate =(rate**_n_);
put effective_rate;
run;
.
This subsetting if is completely useless. The explicit output statement in the loop deactivates the implicit output that usually happens at the conclusion of a data step iteration.
Did you mean to do:
data want; set a; do i = 1 to nobs; set b point=i nobs = nobs; if b > 5 then output; end; run;
@Shmuel This is how it should be done. As I mentioned earlier, I have some programs that have used subsetting IF after explicit OUTPUT statement that confused me.
@SAS_inquisitive In this particular case it's useless. We can't say that about all cases.
@Reeza I wonder if you have any example.
Your example is a subsetting IF, any IF that operated on a variable that was retained from obs to obs would be affected.
This is a useless example, but I know for a fact that I've used this to increment a value after the OUTPUT statement on purpose to retain a value across rows. There are multiple ways to do this and I can't come up with a good example but it has it's uses. Note that in the example below the effective rate in the log is different than the effective rate on the data set.
data have;
do i=1 to 100;
rate=0.05; deposit=100;
output;
end;
run;
data want;
set have;
retain effective_rate 0.03 x 0;
x=deposit+x*(1+effective_rate);
output;
effective_rate =(rate**_n_);
put effective_rate;
run;
.
@SAS_inquisitive - using statement like "IF <condition> ; " intends to filter implicit output;
In your code, the explicit output was already done and the IF statement has no effect.
You wrote "...why IF statement does not work." - what was your target when adding this IF statement ?
If your target was to filter output then your code should be:
IF B > 5 then output;
In @Reeza's example, the statment following OUTPUT is to deal with a retained variable, that may affect next itteration.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.