SAS Macro Language. HRP223 – . 2011. November . 9. th. , 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 porti.... ID: 548519
DownloadNote - The PPT/PDF document "SAS Formats and" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
SAS Formats and
SAS Macro Language
HRP223 – 2011November 9th, 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.Slide2
Formats Saved in Libraries
* x "mkdir C:\Projects\hrp223\cportExample";libname cportex "c:\projects\hrp223\cportexample";proc format library = cportEx; value isMale 0 = "Female" 1 = "Male" . = "Missing" other = "** bad **" ;run;
Save the format here instead of in work. SAS is pushing this idea.Slide3
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.Slide4
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 12 82 0;run;Slide5
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;run;
Send this one.Slide6
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;Slide7
If somebody forgets to send you the formats you can include this line and the data will display unformatted without errors:
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.Slide9
And now for something completely different… Macros
Early in the class I told you to download my
. Those auto-complete SAS codes as you type into the editor. Keyboard macros are
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.Slide10
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.Slide11
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);
Here is the call that makes this plot.Slide12
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:Slide13
Create a promptSlide14
Choose a variable list
Choose the dataset with the variable namesSlide15
Delete the subject variable
The prompt is available for the project.Slide16
Note the typo…Slide17
Edit the typo…Slide18
The “ and “n are not needed.
Note the trailing decimal. Always use it.
My version is simpler.Slide19
Note the ?Slide20
Pure code… without the prompt
This will be set for the entire SAS session. Most dangerous!Slide22
Notice no & and .
Notice the & and .Slide23
Macros can do many tasks.Slide24
Macros begin with the word… macro and
with the word m
As a user of the macro, you can ignore everything after the first line.
The first line has the parameters (aka
) 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.Slide25
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
macro. The macro is actually “done” when you include a line like this.Slide26
I want to make a quantile plot to show the percentiles for a dataset.Slide27
Run the macros once.
You can then invoke the macro repeatedly.
The code may be hardcore but you only need to figure out the comments.Slide28
Sensitivity, Specificity, Positive and Negative Predicted Value
Somehow SAS forgot them…
In the log
In the logSlide29
If you need to calculate binomial probabilities look at my macro:Slide30Slide31
How to Create a Macro
Get code that works either by manually writing it or looking at the code that EG generates.
things that you want to change
with different runs of the macro.
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.Slide32
Add the wrapper.
Add names of the macro variables notice you do NOT put & and . In the list of arguments.
Now you can call the macro.Slide34
You can define default values
You can define default and you can override the defaults.