Sas Sql Macro Example

Active7 years, 6 months ago

SAS has a powerful programming feature called Macros which allows us to avoid repetitive sections of code and to use them again and again when needed. It also helps create dynamic variables within the code that can take different values for different run instances of the same code. Nov 18, 2014  Solved: Is it possible to use a macro variable for proc sql like condition? Such as: proc sql; create table key as select. from c.numkeys.

How to use macros in SQL? (for every thing, that was selected)

I mean something like this:

&VarTable is a table, which have two variables: (for example) Lib and Table

Each observation in &VarTable is the name of table: Lib.Table

I want to do things for every table:

1) exist?

Sas Macro Proc Sql Example

2) sort it

and last condition: each table, if it exist, have a variable &VarField.

how to do this with sql and macros?

gaussblurincgaussblurincSas
2,2437 gold badges28 silver badges55 bronze badges

2 Answers

Do you have to use sql and macros? A simple data step and call execute would do what you need here.

Below is an example that takes a data set that has a list of tables to process, checks to see if the table exists and if it does, sorts it by &VarField. This could be easily extended to sort each table by a custom set of variables if desired.

If the table does not exist, it generates a warning message.

cmjohnscmjohns

Call execute is a good solution, however if the data step code being 'executed' is complicated (which it is not in this example), I find it hard to debug.

Another method is to put all the variables into macro variables and then loop through them in a macro do-loop;

(building on @cmjohns data)

Sas Sql Macro ExampleJay CorbettJay Corbett
14.3k20 gold badges51 silver badges73 bronze badges

Sas Sql Macro Example Definition

Not the answer you're looking for? Browse other questions tagged sqlsassas-macro or ask your own question.