Macro basics Flashcards

1
Q

You can reference a macro variable anywhere is a SAS program except

A

in datalines

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

When referencing a macro variable within a title, can you use both double and single quotation marks?

A

No, only double quotation marks. It won’t resolve in single quotation marks

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

How do you display macro variable values in the SAS log using a system OPTION?

A

OPTIONS NOSYMBOLGEN/SYMBOLGEN;

It lists each macro variable and what it resolves to in the log.

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

Tell me what %PUT does and 3 important things about it

A

%PUT allows you to write messages to the SAS log and resolve macro variables

  1. writes only to the SAS log
  2. does not require quotation marks around text
  3. can be used either inside or outside a macro definition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you list the values of all macro variables using %PUT?

all automatic variables?

all user-generated local variables?

all user-defined macro variables?

A

%put _all_

%put _automatic_

%put _local_

%put _user_

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

What do macro quoting functions do?

A

Tell the macro processor to interpret special characters and mnemonics as text.

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

%STR is most useful for character strings that contain

A
  • A semicolon that should be treated as text
  • blanks that are significant
  • a quotation mark or parenthesis without a match
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

%STR syntax for unmatched quotation mark, percent sign before a parenthesis and string with comment symbols

A
  • %’ or %”, e.g. %str(Jim%’s office)
  • %%) e.g. %str(20%%)
  • %str(/)%str(*)comment text %str(*)%str(/)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How are NR functions different than their non-NR friends?

A

They also mask % and &, so they prevent macro resolution.

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

Use BQUOTE when

A

An expression has unmatching quotes

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

Difference between %STR and %BQUOTE

A

%STR performs during compilation. %BQUOTE performs during execution.

%BQUOTE doesn’t require that unmatched quotation marks be marked with a %

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

What function is like the %UPCASE function but also masks special characters and macro triggers?

A

%QUPCASE

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

%SUBSTR function syntax

A

string is the text that you want to scan

%SUBSTR(string, position, n)

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

What function is like the %SUBSTR function but also masks special characters and macro triggers?

A

%QSUBSTR

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

What is the result of

%let a=one;
%let b=two;
%let c=%nrstr(&a &b);

%put &c;
%put %substr(&c, 1, 2);
%put %qsubstr(&c, 1, 2);

A

Note that for %substr, the substring it finds is &a (two characters) but then this resolves to the value of a.

%put &c: &a &b

%put %substr(&c, 1, 2); one

%put %qsubstr(&c, 1, 2); &a

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

%INDEX function. What does it do and syntax

A

%INDEX(source, string)

It searches source for the first occurrence of string and returns a number representing the position in source of the first character of string (count by character)

17
Q

%SCAN function

A

%SCAN(string, n, delimiters)

Allows you to extract a word from a string

n is the position of the work to return

delimiters is optional

18
Q

%SCAN vs %QSCAN

A

%SCAN does not mask special characters or mnemonic operators in its result even when the argument was previously masked by a macro quoting function.

19
Q

%SYSFUNC and %QSYSFUNC

A

%SYSFUNC(function(arguments), format)

Allows you to use other SAS functions in the macro language

function is the name of the SAS function

arguments are arguments of the SAS function

format is optional

e.g.

%sysfunc(today(), weekdate.)

Friday, November 4, 2011

20
Q

What do you do if you want to have text right after a macro variable reference?

e.g. &varx where var is the macro variable

What if you want to have a period right after the macro variable

e.g. &libref.dataset

A

Add a period after the macro variable

&var.x

Add 2 periods

&libref..dataset

21
Q

What do you use when you want to assign a macro variable within a DATA step?

A

CALL SYMPUT(macroname, value)

macroname and value can be:

a literal, enclosed in quotation marks

a DATA step variable

a DATA step expression

22
Q

How do you convert from character to numeric? From numeric to character?

A

Character to numeric:

INPUT(“text”, informat)

Numeric to character:

PUT(number, format)

23
Q

How is CALL SYMPUTX different from CALL SYMPUT?

A

CALL SYMPUTX removes leading and trailing blanks from both the macro variable name and its value

24
Q

How many &s do you need in front of a macro variable name when its value matches the name of a second macro variable that you want to resolve?

e.g.

%let a = apple

%let apple = yummy

How do you get yummy by referencing a

A

&&&

This is the forward re-scan rule.

Two ampersands get resolved to &, so need a third ampersand

(See pg. 347)

&a and &&a resolve to apple

&&&a first resolves to &apple, which on re-scan resolves to yummy

25
Q

%let teach1 = Mr. Rogers

%let teach2 = Ms. Clare

%let teach3 = Mr. Simmons

%let num = 3

How do you reference teach3 using the num macro variable?

A

&&teach&3

On first pass, this will resolve to &teach3, which then resolves to Mr. Simmons

26
Q

How do you obtain the value of a macro variable during a DATA step execution?

A

Use SYMGET(macroname)

use quotation marks for a macro variable name

can also be a DATA step variable name or expression (no quotation marks)

27
Q

Syntax to create a single macro variable during a PROC SQL Step Execution

A

macro variable can only store one value, so need a summary function

proc sql noprint;

select sum(column1)

into :macroname

from table1;

28
Q

Syntax to create multiple macro variables in PROC SQL step

A

proc sql;

select column1, column2

into :macro1 - :macro-n,

:anothermacro1 - :anothermacro-n

from table1;

29
Q

Syntax to create a macro variable that holds multiple values in a PROC SQL step

A

proc sql;

select column1

into :macroname

separated by‘delimiter’

from table1;

30
Q

What is the difference in these two codes:

proc sql;

create view subcrsid as

select student_name, student_company, paid

from sasuser.all

where course_code=”&crsid”;

quit;

proc sql;

create view subcrsid as

select student_name, student_company, paid

from sasuser.all

where course_code=symget(‘crsid’);

quit;

A

The macro variable referenced by &crsid is resolved during the creation of the view, so it has a constant value whenever the view is used. e.g. if at creation crsid=003, it keeps this value.

Using SYMGET allows the view to look up the macro variable value when the view is used