Macro basics Flashcards
You can reference a macro variable anywhere is a SAS program except
in datalines
When referencing a macro variable within a title, can you use both double and single quotation marks?
No, only double quotation marks. It won’t resolve in single quotation marks
How do you display macro variable values in the SAS log using a system OPTION?
OPTIONS NOSYMBOLGEN/SYMBOLGEN;
It lists each macro variable and what it resolves to in the log.
Tell me what %PUT does and 3 important things about it
%PUT allows you to write messages to the SAS log and resolve macro variables
- writes only to the SAS log
- does not require quotation marks around text
- can be used either inside or outside a macro definition
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?
%put _all_
%put _automatic_
%put _local_
%put _user_
What do macro quoting functions do?
Tell the macro processor to interpret special characters and mnemonics as text.
%STR is most useful for character strings that contain
- A semicolon that should be treated as text
- blanks that are significant
- a quotation mark or parenthesis without a match
%STR syntax for unmatched quotation mark, percent sign before a parenthesis and string with comment symbols
- %’ or %”, e.g. %str(Jim%’s office)
- %%) e.g. %str(20%%)
- %str(/)%str(*)comment text %str(*)%str(/)
How are NR functions different than their non-NR friends?
They also mask % and &, so they prevent macro resolution.
Use BQUOTE when
An expression has unmatching quotes
Difference between %STR and %BQUOTE
%STR performs during compilation. %BQUOTE performs during execution.
%BQUOTE doesn’t require that unmatched quotation marks be marked with a %
What function is like the %UPCASE function but also masks special characters and macro triggers?
%QUPCASE
%SUBSTR function syntax
string is the text that you want to scan
%SUBSTR(string, position, n)
What function is like the %SUBSTR function but also masks special characters and macro triggers?
%QSUBSTR
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);
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
%INDEX function. What does it do and syntax
%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)
%SCAN function
%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
%SCAN vs %QSCAN
%SCAN does not mask special characters or mnemonic operators in its result even when the argument was previously masked by a macro quoting function.
%SYSFUNC and %QSYSFUNC
%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
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
Add a period after the macro variable
&var.x
Add 2 periods
&libref..dataset
What do you use when you want to assign a macro variable within a DATA step?
CALL SYMPUT(macroname, value)
macroname and value can be:
a literal, enclosed in quotation marks
a DATA step variable
a DATA step expression
How do you convert from character to numeric? From numeric to character?
Character to numeric:
INPUT(“text”, informat)
Numeric to character:
PUT(number, format)
How is CALL SYMPUTX different from CALL SYMPUT?
CALL SYMPUTX removes leading and trailing blanks from both the macro variable name and its value
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
&&&
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
%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?
&&teach&3
On first pass, this will resolve to &teach3, which then resolves to Mr. Simmons
How do you obtain the value of a macro variable during a DATA step execution?
Use SYMGET(macroname)
use quotation marks for a macro variable name
can also be a DATA step variable name or expression (no quotation marks)
Syntax to create a single macro variable during a PROC SQL Step Execution
macro variable can only store one value, so need a summary function
proc sql noprint;
select sum(column1)
into :macroname
from table1;
Syntax to create multiple macro variables in PROC SQL step
proc sql;
select column1, column2
into :macro1 - :macro-n,
:anothermacro1 - :anothermacro-n
from table1;
Syntax to create a macro variable that holds multiple values in a PROC SQL step
proc sql;
select column1
into :macroname
separated by‘delimiter’
from table1;
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;
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