BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

 

 

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

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.

Shmuel
Garnet | Level 18

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;

 

SAS_inquisitive
Lapis Lazuli | Level 10

@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.

Reeza
Super User

@SAS_inquisitive In this particular case it's useless. We can't say that about all cases. 

SAS_inquisitive
Lapis Lazuli | Level 10

@Reeza I wonder if you have any example.

Reeza
Super User

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;

 

 

Shmuel
Garnet | Level 18

@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.

 

 

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1265 views
  • 4 likes
  • 4 in conversation