Hi:
First some terminology -- the SAS programming language is not a scripting language. I think of a script program (like JavaScript or PHP or Perl) as something that executes immediately and is
not compiled.
SAS code is compiled, and then, if free of compile errors, is executed. In my mind, that makes SAS something OTHER than a scripting language like JavaScript. I'd call a group of SAS statements a program, not a script. A SAS program can be composed of 1 step or of multiple steps. However, each step is compiled before it is executed.
A SAS program is basically composed of DATA steps and PROCEDURE steps (most commonly called PROC steps). (along with option statements and other global statements that have an impact on how the program is executed or how the output is treated) A DATA step program allows you to use SAS language syntax, such as IF/THEN statements, assignment statements, ARRAYs, DO loops, etc.
A PROC step program allows you to use "canned" statements that have pre-defined ways of operating. So for example, PROC MEANS has a VAR statement; PROC SQL has a SELECT statment. Neither PROC MEANS nor PROC SQL (for example) allow an IF statement to be used inside their syntax. Regular SAS IF statements belong to a DATA step program. So you got an error because you used an IF statement in the wrong place.
However, another rule of the SAS programming language is that you can't mix DATA step syntax and PROC syntax...so for example, you CANNOT do this:
[pre]
DATA TEST;
if TTEST = 'T' then do;
proc sql;
......... more proc sql code .........
end;
run;
[/pre]
That's because DATA step programs and PROC step programs are "self-contained". So how do you define and generate generic reusable code??? Enter the SAS Macro facility.
There is an additional piece of SAS language called the SAS Macro Facility. The SAS Macro facility is used to perform text substitution and to help you generate valid code that will compile and then execute. Just like a DATA step program has an IF statement, you can use a %IF statement inside a MACRO program to generate program code conditionally.
What happens is that the Macro facility grabs any macro "triggers" like % or & and resolves them BEFORE any code is compiled. What the compiler sees is the resolved SAS code. The SAS Macro facility doesn't "run" anything -- it only takes a macro program or macro variable references and resolves the program into valid SAS statements or takes the macro variable references and resolves them in code (like a TITLE statement or other statements).
A good tutorial or place to start with the SAS Macro facility is here:
http://www2.sas.com/proceedings/sugi28/056-28.pdf
Of course the Macro documentation is good, too, and there's a lot to Google on SAS Global Forum papers about using SAS Macro programs to generate reusable, generic code. I suggest you start with the tutorial and if that doesn't help, then look for some other examples of using SAS Macro programs to conditionally generate program statements.
cynthia