0

Can anyone teach me how to rewrite the following command more efficiently. Basically, I want to execute multiple operations if a certain condition is satisfied.

data a;
set a;
if var1 > 5 then var2 = 5;
if var1 > 5 then var3 = 5;
if var1 > 5 then var4 = var1;
run;

I know I can rewrite it as the following. But is there any more efficient way to do this.

Data a;
set a; 
if var> 5 then do;
var2 = 5; var3= 5; var4 = var1;
end;
run;
fly36
  • 513
  • 2
  • 6
  • 23
  • Please elaborate what you mean by efficient. Computation wise your second solution is already rather efficient. – Jetzler Apr 08 '16 at 11:26
  • Using do end, the program is a little bit long and looks a little bit messy. So I am think whether we can write those multiple command in one sentence? – fly36 Apr 08 '16 at 11:29
  • You could generate a function or a macro for repeated use. Other than that I do not see a more compressed version of the do group. – Jetzler Apr 08 '16 at 11:46

1 Answers1

0

I would be amazed if it made any measurable performance difference, but if you only have one block of conditional code and you really hate semi-colons that much, you could use return instead of a do-end block:

Data a;
set a; 
if var <= 5 then return;
var2 = 5; var3= 5; var4 = var1;
run;

When SAS reaches a return statement, it skips the rest of your logic for the current iteration of the data step, outputs there and then (unless you've used an explicit output statement somewhere...), and (in this simple scenario) goes straight to the next iteration of the data step.

In general, though, this approach is on par with goto and should not be used under any circumstances.

Community
  • 1
  • 1
user667489
  • 9,501
  • 2
  • 24
  • 35