Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 SAS Formats and SAS Macro Language HRP223 – 2011 November 9 th, 2011 Copyright © 1999-2011 Leland Stanford Junior University. All rights reserved. Warning:

Similar presentations


Presentation on theme: "1 SAS Formats and SAS Macro Language HRP223 – 2011 November 9 th, 2011 Copyright © 1999-2011 Leland Stanford Junior University. All rights reserved. Warning:"— Presentation transcript:

1 1 SAS Formats and 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 portion of it, may result in severe civil and criminal penalties and will be prosecuted to maximum extent possible under the law.

2 2 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.

3 3 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.

4 4 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;

5 5 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; run; Send this one.

6 6 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;

7 7 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;

8 8 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.

9 9 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.

10 10 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.

11 11 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.

12 12 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:

13 13 Create a prompt

14 14 Choose a variable list Choose the dataset with the variable names

15 15 Delete the subject variable The prompt is available for the project.

16 16 Note the typo…

17 17 Edit the typo…

18 18 The code The “ and “n are not needed. Note the trailing decimal. Always use it. My version is simpler.

19 19 Note the ?

20 20 It works….

21 21 Pure code… without the prompt This will be set for the entire SAS session. Most dangerous!

22 22 Notice no & and. Notice the & and.

23 23 Macros can do many tasks.

24 24 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.

25 25 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.

26 26 Other Examples I want to make a quantile plot to show the percentiles for a dataset.

27 27 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.

28 28 Sensitivity, Specificity, Positive and Negative Predicted Value Somehow SAS forgot them… In the log

29 29 Binomial Probabilities If you need to calculate binomial probabilities look at my macro:

30 30

31 31 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.

32 32 Original Macro variables

33 33 Original 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.

34 34 You can define default values You can define default and you can override the defaults.


Download ppt "1 SAS Formats and SAS Macro Language HRP223 – 2011 November 9 th, 2011 Copyright © 1999-2011 Leland Stanford Junior University. All rights reserved. Warning:"

Similar presentations


Ads by Google