Chapter Contents

Previous

Next
SAS Component Language: Reference

Using Macro Variables

Macro variables, which are part of the macro facility in base SAS software, can be used in SCL programs. Macro variables are independent of any particular SAS table, application, or window. The values of macro variables are available to all SAS software products for the duration of a SAS session. For details, refer to macro variables in SAS Macro Language: Reference. In SCL programs, you can

Examples of types of information that you frequently need to pass between entries in an application include


Storing and Retrieving Macro Variable Values

To assign a literal value to a macro variable in an SCL program, you can use the standard macro variable assignment statement, %LET. For example, the following statement assigns the literal value sales (not the value of an SCL variable named SALES) to a macro variable named DSNAME:

%let dsname=sales;

Macro variable assignments are evaluated when SCL programs are compiled, not when they are executed. Thus, the %LET statement is useful for assigning literal values at compile time. For example, you can use macro variables defined in this manner to store a value or block of text that is used repeatedly in a program. However, you must use a different approach if you want to store the value of an SCL variable in a macro variable while the SCL program executes (for example, to pass values between SCL programs).

Macro variables store only strings of text characters, so numeric values are stored as strings of text digits that represent numeric values. To store values so that they can be retrieved correctly, you must use the appropriate CALL routine. The following routines store the value of a macro when an SCL program runs:

CALL SYMPUT
stores a character value in a macro variable.

CALL SYMPUTN
stores a numeric value in a macro variable.

For example, the following CALL routine stores the value of the SCL variable SALES in the macro variable TABLE:
call symput('table',sales);

To retrieve the value of a macro variable in an SCL program, you can use a standard macro variable reference. In the following example, the value of the macro variable TABLE is substituted for the macro variable reference when the program is compiled:

dsn="&table";

The function that you use to retrieve the value of a macro variable determines how the macro variable value is interpreted. The following functions return the value of a macro variable when a program runs:

SYMGET
interprets the value of a macro variable as a character value.

SYMGETN
interprets the value of a macro variable as a numeric value.


Using the Same Name for Macro Variables and SCL Variables

Using the same name for a macro variable and an SCL variable in an SCL program does not cause a conflict. Macro variables are stored in SAS software's global symbol table, whereas SCL variables are stored in the SCL data vector (SDV). However, if your program uses submit blocks and you have both a macro variable and an SCL variable with the same name, then a reference with a single ampersand substitutes the SCL variable. To force the macro variable to be substituted, reference it with two ampersands (&&). The following example demonstrates using a reference that contains two ampersands:

dsname='sasuser.class';
call symput('dsname','sasuser.houses');
submit continue;
   proc print data=&dsname;
   run;
   proc print data=&&dsname;
   run;
endsubmit;

The program produces the following:

proc print data=sasuser.class;
run;
proc print data=sasuser.houses;
run;


Using Automatic Macro Variables

In addition to macro variables that you define in your programs, SAS software provides a number of predefined macro variables for every SAS session or process. These automatic macro variables supply information about the current SAS session or process and about the host operating system on which the SAS session is running. For example, you can use the automatic macro variable SYSSCP to obtain the name of the current operating system. Automatic macro variables are documented in SAS Macro Language: Reference.

When you use automatic macro variables, remember to use the appropriate routines and functions to set and retrieve variable values. For example, consider the following program statements. The first uses a macro variable reference:

jobid="&sysjobid";
The second uses an SCL function:
jobid=symget('sysjobid');

The macro variable reference, designated by the & (ampersand), is evaluated when the program is compiled. Thus, the identifier value for the job or process that compiles the program is assigned to the variable JOBID. Assuming that the preceding two statements were compiled by an earlier SAS process, if you want the JOBID variable to contain the identifier for the current process, then you must use the second form (without the &). The SYMGET function extracts the macro variable value from the global symbol table at execution.

Note:   The values that are returned by SYSJOBID and other automatic macro variables depend on your host operating system.   [cautionend]


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.