macro_223_20111109

SAS Formats and
SAS Macro Language
HRP223 – 2011
November 9th, 2011
Copyright © 1999-2011 Leland Stanford Junior University. All rights reserved.
Warning: This presentation is protected by copyright law and international treaties.
Unauthorized reproduction of this presentation, or any portion of it, may result in
severe civil and criminal penalties and will be prosecuted to maximum extent possible
under the law.
1
Formats Saved in Libraries
* x "mkdir C:\Projects\hrp223\cportExample";
libname cportex "c:\projects\hrp223\cportexample";
proc format library = cportEx;
value isMale
0 = "Female"
Save the format here instead
of in work. SAS is pushing
1 = "Male"
this idea.
. = "Missing"
other = "** bad **"
;
run;
2
Where to Find Formats
• SAS and EG look for formats in the work
library. You can tell it to look in other libraries
with a line like this:
options fmtsearch = (cportEx work);
Your library name
goes here.
3
Sharing Files
• If everyone on your team uses the same version of SAS you can send
datasets and catalogs via encrypted email. Send both the files in the
library and teach the recipient about options fmtsearch .
data cportEx.stuff;
format sex isMale.;
input ID age sex;
datalines;
1 83 1
2 82 0
;
run;
4
proc cport
• If you need to share datasets and formats across
platforms (including 32 vs 64 bit Windows SAS),
store the library in cport file and send that (via
encrypted email).
proc cport
library = cportEx
file="C:\Projects\hrp223\cportExample\example.cport"
memtype = all;
Send this one.
run;
5
proc cimport
• Use proc cimport on the other machine:
libname cportEx "C:\Projects\hrp223\cportExample";
options fmtsearch = (cportEx work);
proc cimport library = cportEx
file = "C:\Projects\hrp223\cportExample\example.cport" ;
run;
6
No Formats…
• If somebody forgets to send you the formats
you can include this line and the data will
display unformatted without errors:
options nofmterr;
7
32bit vs. 64 bit SAS
• The different versions of SAS optimize
datasets and formats to work as fast as
possible. You can open a 32 bit SAS dataset
with a 64 bit version of SAS but it is slower
than necessary.
• Formats saved in permanent libraries (as
catalogs) may have problems opening on
different platforms/operating systems.
8
And now for something completely
different… Macros
• Early in the class I told you to download my
keyboard macros. Those auto-complete SAS
codes as you type into the editor. Keyboard
macros are not what SAS people call Macros.
• Macros are programs that do automated
tasks. Rather than having to reinvent
solutions to complex problems, SAS
programmers keep libraries of useful code in
easy to use macro format.
9
I want….
• Bar charts are the wrong way to display data if
you have tiny samples. I want a plot to show
the mean value as a red bar and the individual
data points around it.
• This requires fairly complicated voodoo and I
want to be able to reuse the code.
10
Here is the
call that
makes this
plot.
I create the plot once then tweak it to turn it into a macro.
This is like a user-defined function.
%plotit(w2, weight, group, 4,
group1=Thing1, group2=Thing2,
group3=Thing3, group4=Thing4);
11
Prompts
• The macro “call” is like prompting a user to fill
in the details for the required “arguments”.
• I have a dataset and I want EG to ask the user
to what variable to analyze:
12
Create a prompt
13
Choose a
variable list
Choose the dataset
with the variable
names
14
The prompt is available
for the project.
Delete the
subject variable
15
Note the typo…
16
Edit the typo…
17
The code
My version is
simpler.
The “ and “n
are not
needed.
Note the trailing
decimal. Always use it.
18
Note the ?
19
It works….
20
Pure code… without the prompt
This will be set for the entire
SAS session. Most dangerous!
21
Notice no & and .
Notice the & and .
22
Macros can
do many
tasks.
23
Macro Details
• Macros begin with the word… macro and end
with the word mend.
• As a user of the macro, you can ignore
everything after the first line.
• The first line has the parameters (aka
arguments) that the macro needs. Hopefully
the person who wrote the macro will give you
the details on the arguments.
– The parameters are filled in using the order you
typed them unless the arguments have names.
24
Definition of the plotit macro is here.
This creates the macro but does not cause
it to do anything. Expand the code if and
only if you want to.
Arguments are hopefully well named. They
are a comma delimited list.
Macros typically arrive with a big comment
to explain what you use for the arguments
This calls/invokes the plotit macro. The
macro is actually “done” when you include
a line like this.
25
Other
Examples
• I want to
make a
quantile plot
to show the
percentiles
for a
dataset.
26
The code may be
hardcore but you only
need to figure out the
comments.
Run the macros once.
You can then invoke the
macro repeatedly.
27
Sensitivity, Specificity, Positive and
Negative Predicted Value
• Somehow SAS forgot them…
In the log
In the log
28
Binomial
Probabilities
• If you need to
calculate binomial
probabilities look
at my macro:
29
30
How to Create a Macro
• Get code that works either by manually writing it or
looking at the code that EG generates.
• Identify the things that you want to change with
different runs of the macro. Replace the those
keywords with a name preceded by an & and
followed by a .
• Enclose the edited code with inside of %macro(); and
mend;
• Insert your macro variables as a comma delimited list
inside the () on the macro line.
31
Original
Macro
variables
32
Add the
Original
wrapper.
Add names of the macro
variables notice you do NOT put
& and . In the list of arguments.
Now you
can call the
macro.
33
You can define
default values
You can define
default and you
can override the
defaults.
34