SAS: Macro Variable

- 1 min

Overview


1. %LET statement

Syntax:

%LET macro-variable-name = text-or-text-value;

2. Using the INTO in PROC SQL

Creating a single value:

PROC SQL;
	SELECT var-1, var-2
	INTO :macro-var-1, :macro-var-2
	FROM table;
QUIT;

Note: only the first record is assigned to the macro variable, when there are multiple records.

Creating lists of values:

Using SEPERATED BY:

PROC SQL;
	SELECT var-1, var-2
	INTO :macro-var-1 SEPERATED BY ',', 
		 :macro-var-2 SEPERATED BY ' '
	FROM table;
QUIT;

Using THROUGH:

PROC SQL;
	SELECT var-1, var-2
	INTO :macro-var-1-x THROUGH :macro-var-1-y, 
		 :macro-var-2-x THROUGH :macro-var-2-y
	FROM table;
QUIT;

From &macro-var-1-x to macro-var-1-y variables, each of them stores each record of var-1 from the $x^{th}$ row to the $y^{th}$ row. And the same for &macro-var-2-x to macro-var-2-y.

No upper bound:

PROC SQL;
	SELECT var-1, var-2
	INTO :macro-var-1-, 
		 :macro-var-2-
	FROM table;
QUIT;

3. Using the CALL SYMPUTX routine

Syntax:

DATA _NULL_;
	CALL SYMPUT('macro_var', value);
RUN;
%PUT &macro_var;

Reference

Zhijian Liu

Zhijian Liu

A foodaholic

comments powered by Disqus
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora