Macro programs Flashcards

1
Q

Define a macro program with positional parameters.

Define a macro program with keyword parameters.

When mixing positional and keyword parameters, which one goes first?

A

Mix: positional parameters go before keyword parameters

%macro macro-name(parameter1, parameter2);

stuff

%mend (macro-name);

%macro macro-name(parameter1=value1, parameter2=value2);

stuff

%mend (macro-name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which option can you use to create a note in the SAS log when a macro has completed compilation?

A

OPTIONS MCOMPILENOTE = options;

options:

NONE: default. no message

NOAUTOCALL: note is made except for autocall macros

ALL: note is made for all completed macro compilations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you call a macro?

Where can you make a macro call within the program?

Does it require a semicolon?

A

%macroname

Can call a macro anywhere in a program except in DATALINES

Does NOT require a semicolon because it is not a SAS statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A macro call is processed by the macro processor before any ___ _____ ______ such as DATA steps are compiled or executed

A

A macro call is processed by the macro processor before any SAS language statements such as DATA steps are compiled or executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When you call a macro, SAS language statements within the macro that are sent to the compiler do not appear in the SAS log. Which option can you use to see this text?

A

OPTIONS MPRINT;

Default is NOMPRINT

It writes to the SAS log each SAS statement generated by a macro.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you use MLOGIC? What does it do (5 things)?

A

OPTIONS MLOGIC;

Default is NOMLOGIC

It traces the flow of execution of your macro, including

  • the beginning of macro execution
  • the values of macro parameters at invocation
  • the execution of each maccro program statement
  • whether each %IF condition is true or false
  • the end of macro execution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you place comments inside a macro definition?

A

Use macro comment statement:

%*comment;

or the good ol’

/*comment*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you call a macro with keyword parameters?

A

%macroname(parameter1=value1, parameter2=value2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you use the PARMBUFF option and what does it do?

A

%macro macroname/parmbuff;

text

%mend;

It allows you to create a macro that can accept a varying number of parameters. The list of parameters, including the parentheses, are stored in automatic macro variable syspbuff, so the text has to have a reference to it (%syspbuff).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you use the %GLOBAL statement and what does it do?

A

It can be used inside or outside macro definition.

It creates one or more macro variables in the global symbol table and assigns null values to them

e.g.

%global dsn vars;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you delete a macro variable from the global symbol table?

A

%SYMDEL macroname;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you create a global macro variable (4 ways)?

A
  1. %LET
  2. DATA step with SYMPUT
  3. SELECT statement with INTO clause in PROC SQL
  4. %GLOBAL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create local macro variables (5 ways)

What special about the SYMPUT routine?

A

The SYMPUT routine can only create a local macro variable if a local symbol table already exists. If local symbol table doesn’t exist, SYMPUT creates a global macro variable

  1. parameters in a macro definiton
  2. %LET within a macro definition
  3. DATA step with SYMPUT within a macro definition
  4. SELECT statement with INTO clause in PROC SQL within a macro definition
  5. %LOCAL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

%LOCAL

Where can you use it?

What does it do?

A

You can use it only inside a macro definition.

It creates one or more macro variables in the local symbol table and assigns null values to them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What steps does the macro processor go through when you use a %let statement within a macro program (eg. %let macvar=value)?

What if you use the %let statement in open code?

A

Note: same steps apply when you call a macro, e.g. &macvar

  1. The macro processor checks to see if macvar already exists in the local symbol table. If so, it updates macvar in the local symbol table with the value “value.” If macvar doesn’t exist in the local table, it goes on to step 2.
  2. The macro processor checks to see if macvar already exists in the global symbol table. If so, it updates macvar in the global symbol table with the value “value.” If macvar doesn’t exist in the global table, it goes to step 3.
  3. The macro processor creates a macro variable named macvar in the local symbol table and assigns a value of “value” to it.

If use %let in open code, the macro processor only checks the global symbol table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the value of variX and variY in the global and/or local symbol tables?

%macro outer;

%local variX;

%let variX=one;

%inner

%mend outer;

%macro inner;

%local variY;

%let variY = &variX;

%mend inner;

%let variX=zero;

%outer

A

Note: macro processor checks for macro variables in the most recently created local symbol table first when there are multiple local symbol tables.

In the global symbol table, variX = zero.

In the outer local symbol table, variX = one.

In the inner local symbol table, variY = one.

17
Q

What option allows you to see the macro nesting information in the SAS log as part of the MPRINT output?

A

Note: MPRINT has to be on as well

MPRINTNEST option

options mprint mprintnest;

18
Q

What option allows you to see macro nesting information in the SAS log as part of the MLOGIC output?

A

Note: MLOGIC has be on

MLOGICNEST option

options mlogic mlogicnest;

19
Q

Syntax for %IF %THEN %ELSE statements

A

expression = any valid macro expression that resolves to an integer. If expresssion resolves to zero, then it is false.

%IF expression %THEN text;

(%ELSE text;)

20
Q

Syntax for %DO-%END statements within %IF%THEN%ELSE statement

A

Note: all these statements can only be used within a macro program

%IF expression %THEN %DO;

text and/or macro language statements

%END;

%ELSE %DO;

text and/or macro language statements

%END;

21
Q

Syntax for iterative %DO statements

A

index-variable = macro variable name or text expression that generates a macro variable name

%DO index-variable=start %TO stop (%BY increment);

text

%END;

22
Q

How to do calculations within a macro?

A

Use %EVAL, e.g. %let a=%eval(2+1);

Note: only gives you integers as answers

Use %SYSEVALF to get floats as answers

23
Q

%EVAL does NOT convert the following to numeric values (2)

A

Numeric strings that contain a period or E notation

SAS date and time constants

24
Q

Syntax to use %SYSEVALF to conver values. What are the conversion types (4)?

A

%SYSEVALF(expression, conversion-type)

Coversion types: BOOLEAN, CEIL, FLOOR, or INTEGER

CEIL rounds up, FLOOR rounds down

25
Q

If you save your macro program to an external file, how do you call it?

A

The macro is compiled when the %INCLUDE statement is submitted

First use %INCLUDE statement, then call the macro.

%INCLUDE file-specification (/SOURCE2);

%macroname

26
Q

How do you save a macro program in a SOURCE entry in a SAS catalog?

A
  1. File -> Save As Object. Select desired library
  2. If library.Mymacs catalog does not already exist, create it.
  3. Enter macroname in the Entry Name field. Make sure entry type is set to SOURCE entry (SOURCE), and click Save.
27
Q

How do you see the macros that you’ve stored in a SAS catalog?

A

Note cat= is short for catalog=

PROC CATALOG CATALOG = libref.catalog;

CONTENTS;

QUIT;

28
Q

How do you compile and call a macro stored in a SOURCE entry of a SAS catalog?

A

entry-type is source

FILENAME macroname

CATALOG‘libref.catalog.macroname.entry-type’;

%INCLUDE macroname;

%macroname

29
Q

How do you compile and call two macros stored in a SOURCE entry of the same SAS catalog?

A

FILENAME fileref CATALOG‘libref.catalog.’;

%INCLUDE fileref(macroname1);

%INCLUDE fileref(macroname2);

%macroname1

%macroname2

30
Q

How do you save a macro to an autocall library?

A
  1. Make a folder that will hold the macro
  2. Save macro as macroname and file type .sas
31
Q

True or False: If a macro is stored in an autocall library, you do not need to submit or include the macro definition before you submit a call to the macro

A

True

32
Q

How do you call a macro in an autocall library?

A

filepath points to the folder that contains your macro

options mautosource sasautos = (‘filepath’, sasautos);

%macroname

33
Q

How do you create a permanently stored compiled macro?

A
  1. Assign a libref to the SAS library in which the compiled macro is stored
  2. set the system options MSTORED and SASMSTORE=libref
  3. use the STORE option in the %MACRO statement when you submit the macro definition:

%macro macro-name /STORE;

text

%mend;

34
Q

How do you store the source code of the compiled macro with the compiled macro code?

A

SOURCE option

libname macrolib ‘file-path’;

options mstored samstore=macrolib;

%macro macro-name /store source;

35
Q

How do you call a stored compiled macro?

A
  1. Assign a libref to the SAS library that contains a Sasmacr catalog in which teh macro was stored
  2. set the system options MSTORED and SASMSTORE=libref
  3. call the macro
36
Q

Which is the only catalog in which compiled macros can be stored?

A

You can create a Sasmcr catalog in any SAS library

Sasmcr

37
Q

How can you write the source code of a compiled macro to the SAS log (assume you used SOURCE option when storing macro definition)?

A

%copy macro-name/source;