Re: Coding efficiency SAS program

Ah, I think I see. Your not talking about whether the program runs quickly or uses less resources. What you are talking about is maintaining that large amount of if statements. You could consider putting the conditions in a separate table, maybe something like:

VAR TEST VAL RESPONSE

A = 1 111

B < 10 222

...

Then you can use that to generate your code out as:

data _null_;

set tests end=last;

if _n_=1 then call execute('data want; set have; if code='unmatch' then do; ')

call execute(cats(' if ',var,test,val,' then code="',response,'"');

if last then call execute('run;');

run;

Then its just a matter of fiddling with the tests dataset rather than the code. Note, you may have to change it slightly, I assumed all numeric test, if some are text, some numeric then you need to code round it.

Re: Coding efficiency SAS program

As Reeza pointed out, there are some savings possible by altering the IF/THEN statements and adding ELSE. Another possibility (assuming that ELSE makes sense logically):

data want;

set have;

length code $ 7;

if (condition 1) then code='111';

else if (condition 2) then code='222';

...

else if (condition 9) then code='999';

else code='unmatch';

run;

Also note, you don't have to go in order if your conditions are mutually exclusive. For example, you could code "999" first, if that is the most prevalent value. However ...

These savings will be relatively small. The most important thing you can do in this program is inspect which variables are needed and which are not. Add KEEP= or DROP= on the SET statement to limit reading in variables to only those that are needed:

data want;

set have (keep=var1-var20);

......

run;

The expense of reading in data far outweighs the expense of calculations.

Re: Coding efficiency SAS program

Thanks for all suggestions. The reason that I raise this question is that each condition is a full page (when print out hard copy) of hard coding IF/ELSE or other statements for verification before I assigned the value to code. The SELECT statement can not apply here. IF/ELSE statement works as I am coding now. I need to find a coding method that boost up the program efficiency.

Re: Coding efficiency SAS program

Usually, coding doesn't effect execution time that much - it's still a straight pass through data. Cab´n tou share some CPU statistics?

I'm more interested in the vast amount of hard coded logic, not very maintenance friendly. Can you give an example of your <condition n>? It would be wise to find a data driven way to handle the logic.

Re: Coding efficiency SAS program

Ah, I think I see. Your not talking about whether the program runs quickly or uses less resources. What you are talking about is maintaining that large amount of if statements. You could consider putting the conditions in a separate table, maybe something like:

VAR TEST VAL RESPONSE

A = 1 111

B < 10 222

...

Then you can use that to generate your code out as:

data _null_;

set tests end=last;

if _n_=1 then call execute('data want; set have; if code='unmatch' then do; ')

call execute(cats(' if ',var,test,val,' then code="',response,'"');

if last then call execute('run;');

run;

Then its just a matter of fiddling with the tests dataset rather than the code. Note, you may have to change it slightly, I assumed all numeric test, if some are text, some numeric then you need to code round it.