You are on page 1of 37

Macro variable

%let city=New Orleans; title "Data for &city"; title "Data for New Orleans";

Macro structure
%MACRO macro-name;
macro definition %MEND macro-name;
%macro dsn; Newdata %mend dsn;

Macro structure
%macro-name title "Display of Data Set %dsn"; title "Display of Data Set Newdata";

Parameter variables
%macro plot(yvar= ,xvar= ); proc plot; plot &yvar*&xvar; run; %mend plot; %plot(yvar=income,xvar=age) %plot(yvar=income,xvar=yrs_educ)

Parameter
Macro execution produces the following code: proc plot; plot income*age; run;
proc plot; plot income*yrs_educ; run;

Types of macro variable


user-defined macro variables automatic macro variables Examples of automatic macro varibale &sysday, &sysdate

Types of macro variable


Used in titles & footnotes footnote "Report for &sysday, &sysdate"; FOOTNOTE "Report for Tuesday, 17DEC96";

Scope of macro variable


Global Local

Delimiting Macro Variable


%let name=sales; data &name1 &name2; set in&name.temp; run; Macro resolution DATA (?????); SET ???; RUN;

Delimiters
DATA &NAME1 &NAME2; SET INSALESTEMP; RUN; data &name.1 &name.2 DATA SALES1 SALES2; set in&name..temp; SET INSALES.TEMP;

%put ,symbolgen
%let a=first; %let b=macro variable; %put &a ***&b***; write

first ***macro variable***

Symbolgen
options symbolgen; %let title "%upcase(&proc) of %upcase(&dset)"; SYMBOLGEN: Macro variable PROC resolves to gplot SYMBOLGEN: Macro variable DATA resolves to sasuser.houses

Indirect reference
Series of varable city1 to city20 %let city1=new york ; %put &city&n; Resolve to ???

Indirect reference
1.
2. 3.

resolves && to & passes CITY as text resolves &N into 6 %let var=city; %let n=6; %put &&&var&n;

Global
%let county=Clark; %macro concat; data _null_; length longname $20; longname="&county"||" County"; put longname; run; %mend concat; %concat

scope
%macro holinfo(day,date); %let holiday=Christmas; %put *** Inside macro: ***; %put *** &holiday occurs on &day, &date, 1997. ***; %mend holinfo; %holinfo(Thursday,12/25) %put *** Outside macro: ***; %put *** &holiday occurs on &day, &date, 1997.

%put
_ALL_ _AUTOMATIC_ _GLOBAL_ _LOCAL_ _USER_ %put _ALL_ ;

Scope of macro variable


%let origin=North America; %macro dogs(type=); data _null_; set all_dogs; where dogtype="&type" and dogorig="&origin"; put breed " is for &type."; run; %put _user_; %mend dogs; %dogs(type=work)

Scope
DOGS TYPE work GLOBAL ORIGIN North America

Changing the Values of Existing Macro Variables


%let new=inventry; %macro name1; %let new=report; %mend name1;
%put &new ;

Forcing
%macro namelst(name,number); %do n=1 %to &number; &name&n %end; %mend namelst; %let n=North State Industries; proc print; var %namelst(dept,5); title "Quarterly Report for &n"; run;

Forcing
title "Quarterly Report for 6"; %macro namels2(name,number); %local n; %do n=1 %to &number; &name&n %end; %mend namels2;

%eval %sysevalf
%let a =(1+2); %let b =(10*3); %put The value of a is &a; %put The value of b is &b; Resolves to ????

%Eval
%let a=%eval(1+2); %let b=%eval(10*3); The value of a is 3 The value of b is 30 %let d=%eval(10.0+20.0);
%let d=%sysevalf(10.0+20.0);

Call symput
data senior; set census;
if age > 65 then do; %let sr_cit = yes; /* ERROR */

output; end; run;

Symput
Call symput
%let sr_age = 0; data senior; set census; if age > 65 then do; call symput("sr_age",age); stop; end; run;

Call symput
data _null_; put "This data set contains data about a person"; put "who is &sr_age years old."; run;

Sql macro interface


proc sql noprint; select style, sqfeet into :type, :size from sasuser.houses; %let type=&type; %let size=&size; %put The first row contains a &type with &size square feet.;

Into clause
proc sql noprint; select style, sqfeet into :type1 - :type4 notrim, :size1 - :size4 from sasuser.houses; %macro putit; %do i=1 %to 4;
%put Row&i: Type=**&&type&i** Size=**&&size&i**; %end; %mend putit; %putit

Macro Quoting
%let printit=proc print; run; ; &put &printit ?????

%Str & %Nrstr


Macro quoting function mask the special Characters like +-*/<>=^| ~ LE LT EQ NE GE GT AND OR NOT Macro(GE)

Quoting
%let p=%str(proc print; run;); &put &p ;
%Nrstr %macro example; %local myvar; %let myvar=abc; %put %nrstr(The string &myvar appears in log output,); %put instead of the variable value.; %mend example;

Mlogic ,Mprint ,symbolgen


%macro second(param); %let a = %eval(&param); a %mend second; %macro first(exp); %if (%second(&exp) ge 0) %then %put **** result >= 0 ****; %else %put **** result < 0 ****; %mend first; options mlogic; %first(1+2)

Mprint
MPRINT(FIRST): DATA _NULL_; MPRINT(FIRST): VAR= MPRINT(SECOND): 3 MPRINT(FIRST): ; MPRINT(FIRST): PUT VAR=; MPRINT(FIRST): RUN; VAR=3

Mlogic
MLOGIC(FIRST): Beginning execution. MLOGIC(FIRST): Parameter EXP has value 1+2 MLOGIC(SECOND): Beginning execution. MLOGIC(SECOND): Parameter PARAM has value 1+2 MLOGIC(SECOND): %LET (variable name is A) MLOGIC(SECOND): Ending execution. MLOGIC(FIRST): %IF condition (%second(&exp) ge 0) is TRUE MLOGIC(FIRST): %PUT **** result >= 0 **** MLOGIC(FIRST): Ending execution.

Mprint
%macro second(param); %let a = %eval(&param); a %mend second; %macro first(exp); data _null_; var=%second(&exp); put var=; run; %mend first; options mprint; %first(1+2)

Thank You
Please dont ask any question

You might also like