sas slides42 macro
Post on 03-Jun-2018
237 Views
Preview:
TRANSCRIPT
-
8/12/2019 SAS Slides42 Macro
1/41
www.turkuamk.fi
SAS macro language 401
Macro basicsfor high schools
Markku Suni 30.06.2009
-
8/12/2019 SAS Slides42 Macro
2/41
www.turkuamk.fi05/31/992
SAS Macro language
401: High School course
Source material:
SAS Guide to Macro Processing
SAS Macro Facility Tips & Techniques
In the Know SAS Tips & Techniques From Around the
Globe SAS Macro Programming Made Easy
Reporting From the Field
Proceedings from SUGI 15, 16, 17, 18, 19, 21, 22, 23
Own and other wizards experiences
Macro Facility = Macro Processor + Macro Language
-
8/12/2019 SAS Slides42 Macro
3/41
www.turkuamk.fi05/31/993
Macro processor theory
SAS program has been submitted: SUBMIT
Scanner scans the program code
Tokenizer searches for a token (name or smthng.)Finds character % or &
Macro processor is woken; reads the next word
Macro processor acts accordingly
Possibly replaces the word with text
Updates its tables Re-reads the word, does the previous
Destroys the word it just read
Tokenizer carries on after the character
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
4/41www.turkuamk.fi05/31/994
Creating a macro variable
Macro variable can be created in:
iterative %DO-statement
%GLOBAL statement
%INPUT statement
%LET statement SQL query
%LOCAL statement
%MACRO statement
%WINDOW statement
Or using %SYMPUT-routine
Sasmakr2.ppt
So many ways. How canI remember all of those
ways an know what to use?
-
8/12/2019 SAS Slides42 Macro
5/41www.turkuamk.fi05/31/994
Creating a macro variableMacro variable can de created in SQL query. Let us count
# observations in a file:
%let unit = Actuarian;Proq sql noprint;
select count(*)into :nobs
from base.bills ( where = ( unit =:&yksiko));quit;%put Unit &unit has been sent &nobs bills;
Keyword INTO directs the result to variable.Colon defines the variable as macro variable
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
6/41www.turkuamk.fi
05/31/994
Value list
Talking about SQL . . . A list of values could be useful sometimes:
%data bosses;
input name $ ;
cards;
Nilken
Murikka
;
Proq sql noprint;
select quote(name)
into :names separated by
from bosses
quit;
data salaries;
set perre.palkat
( where= ( name IN ( &names )));
run;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
7/41www.turkuamk.fi
05/31/995
Macro loop - %do
Repetition is possible:%macr o names( name, amt ) ; Char string as a number
%do n = 1 %t o &amt ;
&name&n dot not needed
%end;
%mend names;
Pr oc pr i nt dat a = i nf o;
var %names( var , 4 ) ; => var var 1 var 2 var 3 var 4 ;
But:
%macr o names( name, kpl ) ; char string as a number
%do n = 1 %t o &kpl ;
&name. n&n Now a dot is needed there
%end;
%mend ni met ;
Pr oc pr i nt dat a = i nf o;
var %names( var , 4 ) ;
=> var var n1 var n2 var n3 var n4 ;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
8/41www.turkuamk.fi
05/31/996
Creating a macro variable
%LET value is an arbitraty macro expression:
%LET macro = summa ; same value
%LET macro = summa ; same value
%LET luku = 200 ;
%LET luku = 200 + 300;
%LET luku = %str( 200 + 300 ); Preferably like this
%LET expr = ¯o &luku ;
%LET name = %STR( Sunis & Sons company );
%LET tittel = %STR( title Listing for &name on &sysdate; );
%LET titel = %STR( title %str(&tittel) &sysdate; );
This results in a warning, but it will work
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
9/41
-
8/12/2019 SAS Slides42 Macro
10/41
-
8/12/2019 SAS Slides42 Macro
11/41
-
8/12/2019 SAS Slides42 Macro
12/41
www.turkuamk.fi05/31/9910
Environmental issues
Confusing example:%macro namelst( name, number );
%do n = 1 %to &number;
&name&n
%end;%mend namelst;
%let n = 42;
proc print data = guide;
var %namelst( dept, 5 );
title The answer is &n;
run;
Now the compiler will see:proc print data = guide;var dept1 dept2 dept3 dept4 dept5 ;
title The answer is 6;
run;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
13/41
www.turkuamk.fi05/31/9911
Environmental iussues
Confusing example:%macro namelst( name, number );
%do n = 1 %to &number;
&name&n
%end;
%mend namelst;
%let n = 42;
proc print data = guide;
var %namelst( dept, 5 );
title The answer is &n;
run;
Now the compiler will see:proc print data = guide;
var dept1 dept2 dept3 dept4 dept5 ;
title The answer is 6;
run; Solution: %local n;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
14/41
www.turkuamk.fi05/31/9912Sasmakr2.ppt
Macro statements
This course will skip
statements
%DISPLAY
%GOTO%INPUT
%KEYDEF
%label%WINDOW
-
8/12/2019 SAS Slides42 Macro
15/41
www.turkuamk.fi05/31/9913
Macro statements - comments
Comment statement%* comment ;
This comment statement only exists in source code of macro.It wont go to the generated program code.
/* comment */Normal SAS language comment to go the generated code.
Macro variables are not spotted inside comments
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
16/41
www.turkuamk.fi05/31/9914
Macro statements
SAS macro language knows several statements that resemble
corresponding SAS statements, e.g.
%IF %THEN %DO;
.
%END;
There is a clear distinction: macro language statements directthe macro processor and they will be executed before
compilation.
As a result of the execution there will be SAS program code.
Macro statements operate on higher level, should we say, on
META-level.
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
17/41
www.turkuamk.fi05/31/9915
Macro statements - %DO
%DO-statement forms a block: %DO . %END;Iterative %DO:
%DO = %TO %BY ;
: created, if it does not yet exist
, , : macro expressions with integer values
%macro create( maxim )
input %do i = 1 %to &maxim; var&i %end; ;
%mend create;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
18/41
www.turkuamk.fi05/31/9916
Macro statements - %DO %UNTIL
%DO %UNTIL -statement repeats until the condition is true (atleast once) :
%DO %UNTIL( ); %END ;
E.g. :
%DO %UNTIL( &hold = no ); .. %END;%DO %UNTIL( %INDEX( &target, &source ) = 0 ); ... %END;
%macro create( kpl);%let I = 1;
input%do %until( I = &kpl);
var&I%let I = %eval( &I + 1 );
%end; ;%mend create;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
19/41
www.turkuamk.fi05/31/9917
Macro statements - %DO %WHILE
%DO %WHILE statement repeats while the condition is true (possibly 0 times) :%DO %WHILE( ); %END ;
E.g.:
%DO %WHILE( &hold < &b ); .. %END;
%DO %WHILE( %LENGTH(&target) > 20 and &source ne ); ... %END;
%macro create( kpl);%let I = &kpl ;input%do %while( I > 0);var&I
%let I = %eval( &I - 1 );%end; ;%mend create;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
20/41
www.turkuamk.fi05/31/9918
Mackro statements - %END, %GLOBAL
%END statement ends a %DO-block
%GLOBAL defines macro variables and declares them to beglobal.
An existing local macro variable can not be changed global
The new variables are initially empty until they are
assigned a value Inside a macro the new variable created can be deinfed
global
Often there is a reason to do so
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
21/41
www.turkuamk.fi05/31/9919
Macro statements - %LET
%LET statement defines a macro variable and/or assigns it avalue
See the rules of the EPA
%LET variable = value ;
All the beginning and trailing blanks are removed between characters =ja; and the rest there is set to new value of the macro variable.
The value van also be empty (= nothing).
If special characters are needed, the function %STR can be used asmentioned in course macro 101.
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
22/41
-
8/12/2019 SAS Slides42 Macro
23/41
www.turkuamk.fi05/31/9921
Macro statements - %MACRO
%MACRO-statement defines a macro
%macro < / options >;
Macro name is 1 - 8 chars. Parameter list: comma separated parameter names
(positional parameters) or pairs =
Positional parameters come first on the list A parameter can be empty
Options:
STMT: macro is called as a statement
PARMBUFF: the whole parameter list becomes the value of
automagic macro variable SYSPBUFF
(allows varying parameter lists)
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
24/41
www.turkuamk.fi05/31/9922
Macro statements - %PUT
%PUT-statement writes text on log
It can be used inside or outside a macro
%put Macro execution begins ;
%put Macro variable term has value of &term ;
%put %STR( Use the characters ; and % and & right !! );
%put %str( ) ;
Sasmakr2.ppt
Macro statements %SYSEXEC
-
8/12/2019 SAS Slides42 Macro
25/41
www.turkuamk.fi05/31/9923
Macro statements - %SYSEXEC
%SYSEXEC-statement causes the immediate execution of an
operating system command and sets the corresponding
return code to macro variable SYSRC.
Statement can be used in open code or inside a macro in an
interactive session.
Statement works the same way as X-statement in SAS-
language
%sysexec ; goes to operating system state; the user can give
many commands and then return to SAS
%sysexec delete *.tmp ;
Sasmakr2.ppt
Macro statements %SYSEXEC
-
8/12/2019 SAS Slides42 Macro
26/41
www.turkuamk.fi05/31/9924
Macro statements - %SYSEXEC
%SYSEXEC-statement is somewhat operatign systemdependent: statement exists in batch or non-interactive execution
Return to SAS from operating system state The operating system commands available Return codes
Example:%macro exec;%if %upcase( &sysscp ) = OS %then
%sysexec ex dt.s1234.clist(siivous);%else %if %upcase( &sysscp ) = WIN %then
%sysexec c:\dos\siivous.exe;%else %put The system does not have this service &sysscp;
%mend exec;
Sasmakr2.ppt
M l f i
-
8/12/2019 SAS Slides42 Macro
27/41
www.turkuamk.fi05/31/9925
Macro language functions
Macro language knows some character string functions:
%index searches for the character string in another
%length returns the length of its argument%qscan searches words and quotes the result, also % ja &
%qsubstr like %substr, but quotes the result
%qupcase like %upcase, but quotes
%scan searches words from a character string and returns
the word%substr searches a substring from a character string
%upcase converts argument to upper case
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
28/41
www.turkuamk.fi05/31/9926
Macro language functions
Macro language contains a small group of arithmetic functions:
%eval interprets the expression gives in argument as aninteger expressions and executes the arithmetic operations
and logical comparisons in it
%sysevalf the same with floating point numbers
%let x = 100%let x = %eval( &x + 150 );%let sum = 42 + 21;%let sum = %eval( 42 + 21 );
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
29/41
www.turkuamk.fi05/31/9927
Macro language functions
Macro language offers a number of quoting functions:%bquote
%nrbquote
%nrquote
%nrstr
%quote
%str
%superq
%unquote
They are powerful and extremely dangerousWe skip them in this course
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
30/41
www.turkuamk.fi05/31/9928
Indirect macro variables
A macro variable can be referred to indirectly
The program contains &&name
Macro processor examines the text after & searching for a word
&& is replaced with & and the result of the examination is re-scanned
Example:
%let v1 = 42;%let v2 = 21;%let v3 = 10;%let v4 = 0;%macro setit( num, value );
%let &&v&num = &value;%mend aseta;
If num is 1, , 4, all goes well.
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
31/41
Example: Faculty name in title
-
8/12/2019 SAS Slides42 Macro
32/41
www.turkuamk.fi05/31/9922
p y
%let y010 = CeB;
%let y060 = Health ;
%let y130 = Art ;
/* Create the material */
DATA a;
DO fac = '010', '060', '130';
DO class = 1 to 3;
DO case = 1 to 10;
paymt = ranuni( 1107 )*100;
fee = ranuni( 1107 ) * 80;
OUTPUT a;
END;
END;END;
run;
Sasmakr2.ppt
Example: Faculty name in title
-
8/12/2019 SAS Slides42 Macro
33/41
www.turkuamk.fi05/31/9922
p y
DATA a;
DO; DO; DO;
END; END; END;
run;
%macro printit( facu );
PROC tabulate data = a ;
WHERE fac = "&facu";
CLASS class;VAR paymt fee;
TABLE class, (paymt fee)*sum;
TITLE Faculty: &&y&facu, payments and fees";
run;%mend printit;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
34/41
Examples
-
8/12/2019 SAS Slides42 Macro
35/41
www.turkuamk.fi05/31/9930
p
Department name and shortening
proc format;value $fdept
CeB = JuhaIT = JanneEle = Markku ;
%let mosas = JTUOT3;
data _null_;set info;by dept;if first.dept then
call symput( dept, put( dept,$fdept. ));
proc print data = info;where dept = &mosas;title Department &mosas, boss &&&mosas;
run;
Sasmakr2.ppt
Examples
-
8/12/2019 SAS Slides42 Macro
36/41
www.turkuamk.fi05/31/9931
p
Beginning and end of a month:
%macro mmobe( pvm ) /* pvm: SAS-datevariable */
begmo = mdy( month( &pvm ), 1, year( &pvm ));%mend mmobe;
%macro mmoen ( pvm ) /* pvm: SAS-datevariable */
if month( &pvm ) = 12 then
endmo = mdy( 1, 1, year( pvm ) + 1 ) -1;
else endmo = mdy( month( &pvm ) + 1, 1, year( &pvm )) -1;
%mend mmoen;
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
37/41
www.turkuamk.fi05/31/9932
Examples
Procedure TABULATE has a handicap: classes for which
there are no observations, are not shown; thus the reader
does not know that valueja could there be
(compare to procedures CHART ja GCHART option
MIDPOINTS) .
Q&D-solution: append the original material an empty
material with the wanted class variable valuet, butnothing else.
Sasmakr2.ppt
Examples
-
8/12/2019 SAS Slides42 Macro
38/41
www.turkuamk.fi05/31/9933
p
%macro tabulk( x ) / parmbuff ;%let stop = %length( &syspbuff );
%let seti = %scan( &syspbuff, 1 );
%let name = %scan( &syspbuff, 2 );
data _mz_ ( keep = &name );%do I = 3 to &stop;
%let value = %scan( &syspbuff, &i );
%if &value ne %then %do;
&name = &value;output;
%end;
%else %let i = &stop;
%end;proc append base = &seti data = _mz_;
run;
%mend tabulk;
Sasmakr2.ppt
Examples
-
8/12/2019 SAS Slides42 Macro
39/41
www.turkuamk.fi05/31/9934
data koe; using the macroinput classn euros sales;cards;
1 100 2001 100 2003 300 503 200 605 100 505 200 60
;%tabulk( koe, classn, 1, 2, 3, 4, 5 );proc tabulate data = koe;
class luokka;var markka myynti;
table luokka all, ( markka myynti )* sum/misstext = Missing;
run;
Sasmakr2.ppt
Examples
-
8/12/2019 SAS Slides42 Macro
40/41
www.turkuamk.fi05/31/9935
---------------------------------
! ! EUROS ! SALES ! And the result is---------------------------------
!CLASS ! ! !
!-----------!---------!---------!
!1 ! 200! 400!
!-----------!---------!---------!
!2 ! Missing! Missing!
!-----------!---------!---------!
!3 ! 500! 110!
!-----------!---------!---------!
!4 ! Missing! Missing!
!-----------!---------!---------!
!5 ! 300! 110!
!-----------!---------!---------!
!ALL ! 1000! 620!
---------------------------------
Sasmakr2.ppt
-
8/12/2019 SAS Slides42 Macro
41/41
www.turkuamk.fi05/31/9936Sasmakr2.ppt
This is the endThe Doors
top related