| Date: | Fri, 20 Dec 2002 10:19:49 -0500 |
| Reply-To: | Gerry Pauline <GPauline@PACE.EDU> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Gerry Pauline <GPauline@PACE.EDU> |
| Subject: | Re: Lorenz curves and SAS |
|
| Content-Type: | text/plain; charset=us-ascii |
Gijs:
You might find the code below helpful (this should run thri SAS v8.2).
-Gerry
Gerard T. Pauline
Computer Systems, DoIT
Pace University
====================================================================
/**********************************************************************/
/*************************** Pace University
**************************/
/****************** Department of Academic Computing
******************/
/**********************************************************************/
/*
*/
/* Program Name: Ginicode (source for the GINI macro)
*/
/*
*/
/* Language: SAS (6.07 )
*/
/*
*/
/* Date Implemented: 03/03/93
*/
/*
*/
/* Program Version: 1.00
*/
/*
*/
/* Program Author: Mark Keintz (Gini Methodology)
*/
/* Computer Group Manager
*/
/* Population Studies Center
*/
/* University of Pennsylvania / 6298
*/
/* 3718 Locust Walk
*/
/* Philadelphia, Pa 19104-6290
*/
/* (215) 898-6713
*/
/* Mkeintz @ Mail.Sas.Upenn.Edu
*/
/*
*/
/* Gerard T. Pauline (Macro Shell)
*/
/* Mgr, Internet/DB Applications
*/
/* Computer Systems, DoIT
*/
/* Pace University
*/
/* 1 Pace Plaza
*/
/* New York, NY 10038
*/
/* (212) 346-1706
*/
/* Gerry@Pace.Edu
*/
/*
*/
/**********************************************************************/
/************************* Purpose of Program
*************************/
/**********************************************************************/
/*
*/
/* This program computes the GINI concentration coefficient; option-
*/
/* ally, if requested, it will also plot a Lorenz Curve. The coef-
*/
/* ficient is generated by the following methodology:
*/
/*
*/
/* 1- Compute the total for the variable to be 'GINIed' by
*/
/* group to generate 'X' (num of cases) and 'Y' (sum of
*/
/* the variable to be ginied) values.
*/
/*
*/
/* 2- Compute the representation of the variable total (of
*/
/* the variable to be 'GINIed') and the cases in each
*/
/* group (X and Y).
*/
/*
*/
/* 3- Compute the ratio in each group (y / x)
*/
/*
*/
/* 4- Sort by the ratio of y / x .
*/
/*
*/
/* 5- Generate GINI coefficient and the coordinates for the
*/
/* Lorenz Curve.
*/
/*
*/
/* The dataset may contain values that are discrete (with a requisite
*/
/* Id variable) or grouped.
*/
/*
*/
/* Output of the coefficient occurs in one of two ways: if the plot-
*/
/* ting of the Lorenz Curve is requested, the coefficient is display-
*/
/* at the top of the plot; if the curve is not requested, the coef-
*/
/* ficient is displayed on the user's terminal (if submission is by
*/
/* VMBatch, the coefficient is written to a LISTING file.
*/
/*
*/
/* @@ Note:
*/
/*
*/
/* This code is compiled and stored in the file 0SASMACR GINIMAC.
*/
/* If the data source is one of the online databases, the macro
*/
/* is loaded as part of the ACCESS macro system. If the source of
*/
/* data is not one of the online databases, the macro is invoked
*/
/* by 'including' the file GINICOEF SAS in the source program,
*/
/* which contains code to load the GINI and DEVICE macros.
*/
/*
*/
/**********************************************************************/
/************************** Acknowledgements
**************************/
/**********************************************************************/
/*
*/
/* The methodology to compute the GINI coefficient was developed by
*/
/* Mark Keintz, and his assistance in the implementation of this code
*/
/* is gratefully acknowledged by the Department of Academic Computing
*/
/* at Pace University.
*/
/*
*/
/**********************************************************************/
/************************ Input And Output Files
**********************/
/**********************************************************************/
/*
*/
/* A temporary or permanent SAS dataset containing one or more var-
*/
/* iables to be 'GINIed'.
*/
/*
*/
/**********************************************************************/
/********************* Subroutines And Procedures
*********************/
/**********************************************************************/
/*
*/
/* None
*/
/*
*/
/**********************************************************************/
/**************** External References And Dependencies
****************/
/**********************************************************************/
/*
*/
/* %Device - SAS macro to assign a graphics capable output device
*/
/* (3179G, 3278-2PS, 3279G2 & G3, Kermit, 3287-2C, 4045
*/
/* Xerox lasers)
*/
/*
*/
/* This program uses the Stored Macro Facility under SAS 6.07 to
*/
/* compile and store the GINI macro.
*/
/*
*/
/**********************************************************************/
/***************************** Update Log
*****************************/
/**********************************************************************/
/*
*/
/* 1.) 00/00/00
*/
/*
*/
/**********************************************************************/
Options MAUTOSOURCE MSTORED SASMSTORE=GINIMAC
;
%Macro GINI (DS= , /* Input Dataset
*/
ID= , /* Id (Grouping) Variable
*/
VAR= , /* Variable To 'Gini'
*/
DEVICE= , /* Graphics Output Device
*/
GSIZE=72, /* Graph Size (Text Graphics Only)
*/
GRAPH=N) /* Control (N-No Plot, T-Text Grp, S-SASGraph)
*/
/ Store ; /* Compile And Store The Macro In GINIMAC
*/
%Let VAR = %Upcase(&VAR) ; /* Uppercase For Title Display
*/
/*----------------- Compute Totals For &VAR By &ID
----------------*/
Proc SUMMARY Data=&DS
;
Class &ID ; /* Group By This Variable
*/
Var &VAR ; /* For This Variable
*/
Output Out=GINISUM N=X Sum=Y ; /* N Is # of Cases, Y is &VAR Sum
*/
Run
;
/*-------------- Compute Ratio of X & Y In Each Group
-------------*/
Data GINIDATA (Keep=X Y RATIO)
;
Set GINISUM
;
Retain TOTAL_X TOTAL_Y
;
If (_TYPE_ = 0)
Then
Do
;
TOTAL_X = X ; /* Grand Total of X
*/
TOTAL_Y = Y ; /* Grand Total of Y
*/
Delete ; /* Remove The Type 0 Record From Dataset
*/
End
;
Y = Y / TOTAL_Y ; /* Current Y For This Record
*/
X = X / TOTAL_X ; /* Current X For This Record
*/
RATIO = Y / X ; /* Ratio of Y To X For This Record
*/
Run
;
/*----------- Sort Records For Lorenz Curve Computation
-----------*/
Proc SORT Data=GINIDATA
;
By RATIO
;
Run
;
/*----- Generate GINI Coefficient And Lorenz Curve Coordinates
----*/
Data LORENZ (Drop=CUM_AREA)
;
Set GINIDATA End=EOF
;
Retain CUM_X CUM_Y CUM_AREA 0
;
/*--------------- Compute Lorenz Curve Coordinates
--------------*/
CUM_AREA = CUM_AREA + X * (2 * (CUM_X - CUM_Y) + (X - Y))
;
CUM_X = CUM_X + X
;
CUM_Y = CUM_Y + Y
;
/*--------- At End of File, Compute The GINI Coefficient
--------*/
If (EOF)
Then
Do
;
GINI = CUM_AREA / (CUM_X * CUM_Y) ; /* Gini Coefficient
*/
Call SYMPUT ('GINICO', GINI) ; /* Assign To Macro Variable
*/
End
;
Run
;
/*-- Display The Coefficient, And If Requested, The Lorenz Curve
--*/
%If (&GRAPH = S) %Then /* Plot Lorenz Curve With SAS Graph
*/
%Do
;
%Device (&DEVICE) /* Assign Graphics Output Device
*/
Symbol1 Color=RED H=2 V=DOT I=J
;
Axis1 Label=('Group')
;
Axis2 Label=('Concentration')
;
Title1 H=2
F=SWISSB
'Lorenz Curve'
;
Title3 H=1 F=SWISSB
Box=1
"Gini Coefficient For The Variable &VAR Is &GINICO"
;
Proc GPLOT Data=LORENZ
;
Plot CUM_Y * CUM_X / Vaxis=AXIS1 Haxis=AXIS2 Caxis=BLUE
;
Run
;
%End
;
%Else
%If (&GRAPH = T) %Then /* Plot Lorenz Curve With Text Graphics
*/
%Do
;
Options LS=&GSIZE NODATE
;
Proc PLOT Data=LORENZ Nolegend
;
Plot CUM_Y * CUM_X
;
Label CUM_Y =
'Group'
CUM_X = 'Concentration'
;
Title1 'Lorenz Curve'
;
Title3 "Gini Coefficient For The Variable &VAR Is &GINICO"
;
Run
;
%End
;
%Else
%If (&GRAPH = N) %Then /* Display GINI Coefficient Only
*/
%Do
;
%Let ID = %Substr(&SYSJOBID, 1, 5) ; /* User Id
*/
Options LS=&GSIZE NODATE
;
Filename GINI Terminal
;
Data _NULL_
;
%If (&ID ^= BATCH) %Then /* From The User's CMS Acct
*/
%Do
;
File GINI ; /* Output To Terminal
*/
%End
;
%Else /* Batch Submission
*/
%Do
;
File PRINT Notitle ; /* Output To A Listing File
*/
%End
;
Length GVR $ 8 /* Variable That Was GINIed
*/
GCO $ 15 /* Gini Coefficient
*/
D1
D2
D3 $ 65
;
GVR = Symget('VAR') ; /* Retrieve Value of &VAR
*/
GCO = Symget('GINICO') ; /* Retrieve Value of &GINICO
*/
D1 = 'GINI Coefficient for the variable '
||
Trim(GVR) || ' is ' || GCO
;
D2 = Repeat('-', Length(D1)-1)
;
D3 = D2
;
Put / @1 D2 / @1 D1 / @1 D3 / ; /* Write To Output Unit
*/
Run
;
%End
;
%Mend GINI
;
====================================================================
Gijs Dekkers wrote:
>
> I am looking for a macro/program which calculates (and plots) Lorenz
> curves. Can anybody help me?
>
> Thanks in advance,
>
> Gijs
>
> --
> Dr. Gijs Dekkers
> Federaal Planbureau
> Algemene Directie
> Kunstlaan 47-49
> B 1000 Brussel
> ++32/(0)2/5077413
> fax 7373
|