Personal tools
You are here: Home Software Packages SAS PRI SAS Macro Library

PRI SAS Macro Library

SAS Macros currently in the PRI SAS Macro Library

What's a macro & why do I need one? Macros are programs which allow you to extend and customize the SAS system. Macros can perform commonly used calculations, like converting weight from pounds & ounces to grams. Macros can allow you to perform SAS Procs on condition or repeatedly. The Programming Team at PRI has compiled a library of macros for all PopNet users to use.

The list below describes all the macros which are currently in the library. Macro names always begin with a %. Following the description is an example of a SAS data step statement using the macro. If you click on the name of the macro you can access an executable sample program using it. Try running the code to observe the macro "in action".

Note that many macros, like the %nvals macro below, return a value to your program. Macros of this type are used in an assignment statement and require you to create a new variable to receive the value returned by the macro call.


Array Manipulations


%ARRSORT -- Sort values in a numeric array in place. (example)
%ARRSORT2 -- Sort values in a numeric array and store them in a new array. (example)
%ARRSORT3 -- Sort values in a numeric array and store them in a new array, store original position of each value in a third array. Useful when accessing several arrays based on the order of the sorted array. (example)

Given: x=9946 y=46 z=0 a=2346 b=463 c=.
 array vars (*) x y z a b c;
 array newvars(*) n1-n6;
 array oldpos(*) p1-p6;

 %arrsort(vars);  
/* Returns: x=.    y=0   z=46  a=463  b=2346  c=9946   */
 
 %arrsort2(vars);  
/* Returns: x=9946 y=46  z=0   a=2346 b=463   c=. 
/*          n1=.   n2=0  n3=46 n4=463 n5=2346 n6=9946  */

 %arrsort3(vars);  
/* Returns: x=9946 y=46  z=0   a=2346 b=463   c=. 
/*          n1=.   n2=0  n3=46 n4=463 n5=2346 n6=9946  */
/*          p1=6   p2=3  p3=2  p4=5   p5=4    p6=1     */	


Handy Functions


%NVALS -- Count the number of variables EQUAL TO a particular value. (example)
%NLESS -- Count the number of variables which are LESS THAN a particular value. (example)
%NMORE -- Count the number of variables which are GREATER THAN a particular value. (example)

Given: test1 = ., test2 = 65, quiz1 = 9, quiz2 = 10, midterm = 75, final = 72, pass = 70.
 count10= %nvals(10, quiz1 quiz2 ); /* Returns 1 */
 under10= %nless(10, test1 test2 quiz1 quiz2); /* Returns 2 */
 over10 = %nmore(pass, test1 test2 midterm final); /* Returns 2 */

%WORDCNT -- Count number of words in a character string. (example)


Date & Time Macros


%AGE -- Calculate age in years on the day of reference date, given a date of birth (SASdate) and a reference date (SASdate). (example)

    dob     = '12AUG60'd;
    when    = '10AUG98'd;
    agenow  = %age(dob,today());        /* returns age today */
    agethen = %age(dob,'22OCT65'd);     /* returns 5 assuming 1965 */
    agewhen = %age(dob,when);           /* returns 37 */
    age21st = %age(dob, '02OCT2005'd);  /* returns 45 */

%AGEOM -- Calculate age in years at the end of the month of the reference date, given a date of birth (SASdate) and a reference date (SASdate). (example)

    dob     = '12AUG60'd;
    when    = '10AUG98'd;
    agenow  = %ageeom(dob,today());        /* returns age at end of this month */
    agethen = %ageeom(dob,'22OCT65'd);     /* returns 5 assuming 1965 */
    agewhen = %ageeom(dob,when);           /* returns 38 */
    age21st = %ageeom(dob, '02OCT2005'd);  /* returns 45 */

%CENTMTH -- Calculate century month from valid month value (1-12) and year value. If two-digit year is supplied, the macro assumes 20th century. (example)


Statistical Macros


%GLIMMIX -- SAS macro for fitting generalized linear mixed models using PROC MIXED and the Output Delivery System (ODS). (example)

HECKMAN Heckman Two-Step Selection Correction Estimation. This is a sample data step program and associated macros which you can copy and modify.

%ROBUST

MACRO ROBUST produces robust estimates of the covariance matrix for
the coefficients from PROC LOGISTIC or PROC PHREG when the data are
clustered. One common application is to longitidunal data with each
individual treated as a cluster.  IML is required.

The macro uses the method of Halbert White (1982) "Maximum likelihood
estimation of misspecified models," Econometrica 50: 1-25.

Author:  Paul D. Allison, University of Pennsylvania
          allison@ssc.upenn.edu

Adapted from an IML program written by Terry Therneau, Mayo Clinic.

For PROC LOGISTIC, you must specify the OUTEST=name1 and
COVOUT options on the PROC statement. You must also use the OUTPUT
statement with OUT=name2 and DFBETAS=namelist.  The namelist should
have one name for each term in the model, including the intercept.
There must also be a variable in the data set containing a unique
value (either character or numeric) for each cluster. The macro is
invoked after running the LOGISTIC procedure.

For PROC PHREG, you must specify OUTEST=name1 on the PROC
statement. (COVOUT is unncessary).  You must also use the OUTPUT
statement with OUT=name2 and DFBETA=namelist.  The namelist should
have one name for each variable in the model. There must also be a
variable in the data set containing a unique value (either character
or numeric) for each cluster. This variable must be added to the
UTPUT data set by using the ID statement.  The macro is
invoked after running the PHREG procedure.

The macro has the following parameters:

OUTEST   Name of data set used in the OUTEST= option.
OUT      Name of data set used in the OUT= option.
ID       Name of variable containing a unique value for each cluster
DF       List of names used in the DFBETAS or DFBETA option.

Examples of usage:

proc logistic outest=a covout;
  model y = x z w;
  output out=b dfbetas=dint dx dz dw;
run;
%robust(outest=a, out=b, id=subjid, df=dint dx dz dw)

proc phreg outest=a;
  model y*d(0) = x z w;
  id subjid;
  output out=b dfbeta= dx dz dw;
run;
%robust(outest=a, out=b, id=subjid, df=dx dz dw)

BE CAREFUL:
On the OUTPUT statement, it's DFBETAS in LOGISTIC but DFBETA in PHREG.
(The former is standardized, the latter is not).
Also PHREG does NOT have an intercept.

%ESTSTATS -- Produces a SAS dataset containing test statistics and confidence intervals for regression parameter estimates obtained from the REG, LOGISTIC (including probit models), PHREG, or LIFEREG procedures. Approximate confidence intervals for the parameter estimates are not given by the REG, PHREG, PROBIT, or LIFEREG procedures, but are provided by this macro. For logistic and proportional hazards models, odds or risk ratios and their confidence intervals are also given. (example)

%SREGSUB -- Provides linear regression capabilities currently not available in PROC SURVEYREG. This includes: . restricting the regression analysis to a subpopulation (SUBPOP= ), and . incorporating missing values into the variance computations.
(more information | example)

More Macros Available for Download from SAS

SAS provides a library of useful statistical macros. Some may already have been installed on our system, so check for the macro in the list above before downloading.

Analyzing Developmental Trajectories

A workshop held on February 5th & 6th , 2003 through the Methodology Center Conducted by Dr. Daniel Nagin of Carnie Mellon University introduced the SAS subprogram, PROC TRAJ.

A trajectory or line is defined from data which describes the course of behavior over time using semi-parametric, group based analysis.

The semi-parametric, group based analysis can
  1. allow for the identification distinctive groups of trajectories.
  2. allow for estimating the proportion of the population following each trajectory group.
  3. relate the probability of group membership to the individuals characteristics and circumstances.
  4. to use the group membership probability for purposes such as creating a profile of the group members.

PROC TRAJ is a software subprogram/routine written for PC - SAS.

PROC TRAJ can fit linear models of psychometric, count and binary responses of longitudinal data. PROC TRAJ can

  • handle missing data
  • handle sample weights
  • handle irregular time spacing of measurements
  • accommodate over-lapping cohort designs

The PROC TRAJ subprogram and associated parts for Windows PC SAS version 8 can be downloaded at the website:

www.stat.cmu.edu/~bjones/traj.html

There will be 2 files from the download

  • trajplot
  • traj.dll

Before excuting SAS...

The file named trajplot should be copied to the location

C:/ ... /SAS/V8/CORE/SASMACRO/ directory of your hard drive.

The file named traj.dll should be copied to the location

C:/ ... /SAS/V8/CORE/SASEXE/ diectory of your hard drive.

Additionally to use the trajplot.sas you will need to have SAS/GRAPH available on your PC.

The website also provides documentation on PROC TRAJ syntax and some examples as well as references for the analysis methods.

Document Actions

Copyright ©2009, The Pennsylvania State University | Privacy and Legal Statements
Contact the Help Site Administrator | Last modified Aug 13, 2008 | Weblion Partner