6
Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application. Rick Guest IBM Microelectronics Division Essex Junction, VT 05452 ABSTRACT Do you generate a hundred or more graphs each day just to ensure that a particular one required by your manager is available? SAS graphics catalogs are excellent for storing a variety of graphics covering all aspects of your business, however, they can use a lot of CPU and DASD to create and hold graphics that may rarely, if ever, be used. This paper describes a simple SAS program for creating graphics interactively when they are needed. Actual program text provides examples that can be executed or modified with very little effort, which will enable you to convert these examples into a working tool for your graphics requirements. With this information, you should be able to use a SAS INCLUDE file, write a simple SAS MACRO, generate a simple SAS graph and output a graph to an ADMGDF file in a CMS environment. A STEP-BY-STEP SAS PROGRAM These basic steps are necessary to create your SAS program (while only a basic example, the concept can help you do more sophisticated programming. It may also be more logical for you to order some of these steps differently. The sequence used here is only an example). STEP 1 : SETUP Define anything that remains consistent. For example, FOOTNOTE1 and PATTERN1 are defined but GLOBAL variables, file definitions, the environment etc., could have been set. 903 FOOTNOTE1 H=1 J';'C C=W 'GENERATED IN SAS PROGRAM - SAMPLE'; PATTERN1 C=W V=S; STEP 2: DATA INPUT Date can be input from a permanent SAS database or data file, but here the data is input using SAS CARDS (the variables DEPT, THRUPUT, WIP, NUMEMPS and BUDGET are used). There are 12 observations in the CARDS and five variables. The data set name (while the program executes) is WORK.SAMPLE. DATA SAMPLE; INPUT DEPT $4. THRUPUT 5. WIP 5. NUMEMPS 3. BUDGET 10.; FORMAT BUDGET DOLLAR10.; CARDS; 175V 1023 1051 2015500 1542 1972 17252722350 207G 1488 9141917600 293C 1522 9101813950 194F 1688 7681511700 213T 1392 8881612400 292R 2014 11682217050 546B 21131344 2418600 127N 1657 116721 16275 413F 1492 5981410850 618G 1993 7771713200 317T 1815 7251511700 RUN;

Using SAS® INCLUDE Files and SAS Macro Variables To ... Guest.pdf · Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application. Rick Guest

Embed Size (px)

Citation preview

Page 1: Using SAS® INCLUDE Files and SAS Macro Variables To ... Guest.pdf · Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application. Rick Guest

Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application.

Rick Guest IBM Microelectronics Division

Essex Junction, VT 05452

ABSTRACT

Do you generate a hundred or more graphs each day just to ensure that a particular one required by your manager is available? SAS graphics catalogs are excellent for storing a variety of graphics covering all aspects of your business, however, they can use a lot of CPU and DASD to create and hold graphics that may rarely, if ever, be used. This paper describes a simple SAS program for creating graphics interactively when they are needed. Actual program text provides examples that can be executed or modified with very little effort, which will enable you to convert these examples into a working tool for your graphics requirements. With this information, you should be able to use a SAS INCLUDE file, write a simple SAS MACRO, generate a simple SAS graph and output a graph to an ADMGDF file in a CMS environment.

A STEP-BY-STEP SAS PROGRAM

These basic steps are necessary to create your SAS program (while only a basic example, the concept can help you do more sophisticated programming. It may also be more logical for you to order some of these steps differently. The sequence used here is only an example).

STEP 1 : SETUP

Define anything that remains consistent. For example, FOOTNOTE1 and PATTERN1 are defined but GLOBAL variables, file definitions, the environment etc., could have been set.

903

FOOTNOTE1 H=1 J';'C C=W 'GENERATED IN SAS PROGRAM - SAMPLE'; PATTERN1 C=W V=S;

STEP 2: DATA INPUT

Date can be input from a permanent SAS database or data file, but here the data is input using SAS CARDS (the variables DEPT, THRUPUT, WIP, NUMEMPS and BUDGET are used). There are 12 observations in the CARDS and five variables. The data set name (while the program executes) is WORK.SAMPLE.

DATA SAMPLE; INPUT DEPT $4. THRUPUT 5. WIP 5. NUMEMPS 3. BUDGET 10.; FORMAT BUDGET DOLLAR10.;

CARDS; 175V 1023 1051 2015500 1542 1972 17252722350 207G 1488 9141917600 293C 1522 9101813950 194F 1688 7681511700 213T 1392 8881612400 292R 2014 11682217050 546B 21131344 2418600 127N 1657 116721 16275 413F 1492 5981410850 618G 1993 7771713200 317T 1815 7251511700

RUN;

Page 2: Using SAS® INCLUDE Files and SAS Macro Variables To ... Guest.pdf · Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application. Rick Guest

STEP 3: DEFINING THE SAS MACRO

Always begin a SAS MACRO with %MACRO, followed by the name of the MACRO and any variables to be passed. I n this case, the MACRO is DOC HART, and four variables have been selected to pass to it.

The variables being passed are XAX (the variable name from WORK.SAMPLE used for the X axis of the plot), Y AX (the variable name from WORK.SAMPLE used for the Y axis of the plot), DISP (the variable selected to show the plot on the screen or save it in an ADMGDF), and CHRTNAME (if saving the plot, use this filename).

STEP 4: SETTING UP GOPTIONS TO DISPLAY OR SAVE THE PLOT

A different set of GOPTIONS must be used to 'SAVE' rather than just display the plot in this example. Use actual SAS MACRO code to ensure that the MACRO variable DISP is set to 'SAVE'. If it is, set up the GOPTIONS to save the plot in an ADMGDF file. If the MACRO variable DISP is set to 'SHOW', then set the GOPTIONS DEVICE and DISPLAY. Much of this coding is done within a SAS MACRO (although not necessary) because it provides the flexibility for entering anything at any time, i.e., like the code that follows. This is easy to do inside a MACRO, but very difficult outside a MACRO. A MACRO variable can be represented anytime by placing an & before the MACRO variable name.

%IF &DISP = 'SAVE' %THEN %DO; GOPTIONS DEVICE=GDDMFAM1

GDDMT = L5279A 1 GSFMODE = REPLACE CHARTYPE = 2

%END;

ADMGDF GSFNAME = &CHRTNAME NODISPLAY;

904

%ELSE %IF &DISP = 'SHOW' %THEN %DO; GOPTIONS DEVICE=GDDMPCG

DISPLAY; %END;

STEP 5: SETTING UP THE AXES AND TITLE.

Axes setup occurs at the beginning of the program so you can generate more then one graph at a time. The MACRO starts after the data is input, therefore, data only needs to be read once, regardless of how many plots are selected.

In this step, the axes and titles are set. AXIS1 defines the attributes assigned to the Y AXIS, while AXIS2 defines the attributes assigned to the X AXIS. Both axes will use label names equal to the variable name in the WORK. SAMPLE data set. These names can be redefined if the variable names are too long or not descriptive. Here, only one TITLE is defined (keeping it simple by titling the chart name &Y AX (the Y var name) BY &XAX (the X var name)). Because this paper is black and white, all colors are defined in the plots as WHITE; therefore, in this environment, they will print black.

AXIS1 LABEL=(H=1 R=90 A=-90 C=W "&YAX") VALUE=(H=.9 C=W) COLOR=WHITE;

AXIS2 LABEL=(H=1 C=W "&XAX") VALUE=(H=1 R=90 A=-90 C=W ) COLOR=WHITE;

TITLE1 F=SWISS C=W H=2 "&YAX BY &XAX";

STEP 6: CREATING THE PLOT WITH GCHART (VBAR)

A chart can be created when everything is set in the MACRO. A simple GCHART using VBAR is shown. Again, note the use of MACRO variable XAX and Y AX, which are being used rather than

Page 3: Using SAS® INCLUDE Files and SAS Macro Variables To ... Guest.pdf · Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application. Rick Guest

the names of the variables. SAS is very user friendly and automatically recognizes when you refer to either the VALUE of the MACRO variable or the MACRO variable NAME. Note also that you can override treatment of the variable, but this will not be discussed here.

PROC GCHART DATA=SAMPLE; VBAR &XAXI

SUMVAR=&YAX RAXIS=AXIS1 MAXIS=AXIS2;

RUN;

STEP 7: ENDING THE MACRO

Every good MACRO must come to an end. To end your MACRO, use %MEND, then the name originally assigned to the MACRO and a semicolon.

%MEND DOCHART;

STEP 8: OBTAINING INPUT VARIABLES

The SAS INCLUDE file is an external file or piece of SAS code that you can "INCLUDE" anywhere in your SAS program. INCLUDE files are very useful for storing pieces of code that must be continually rewritten and repetitively changed in many different programs.

This INCLUDE file makes this program cohesive by passing the criteria for the plot to the SAMPLE program, thereby avoiding the unusually difficult coding of input parameters in a program.

In the following example, the SAS INCLUDE file is not used in an ordinary way but rather as a vehicle for passing plot parameters to a canned SAS program. First, look at how a SAS INCLUDE file is used. The syntax for the file is %INCLUDE X, where X equals the file name to include. In a CMS environment, the INCLUDE file will always have the file type of SAS (same as a SAS program). In an MVS environment,

905

the INCLUDE file should reside in the same library as the rest of your SAS programs. Remember: the INCLUDE file is actually a SAS program.

%INCLUDE DOCHART;

To call the SAS MACRO, place this code in your program.

%DOCHART(DEPT,BUDGET,'SHOW, DEPTBUDG);

This executes the MACRO DOCHART passing DEPT as the XAX MACRO variable, BUDGET as the Y AX MACRO variable, 'SHOW as the MACRO DISP variable, and DEPTBUDG as the MACRO CHRTNAME variable. The MACRO variable CHRTNAME is not needed here because 'SHOW is selected and not 'SAVE' (only included for consistency). Rather then placing this MACRO call in the SAMPLE program, it is placed in a separate file (the INCLUDE file) called ISAMPLE (Figure 1). This file, which is controlled and stored by an end user, contains specific instructions written as comments to the user and information aI:1out how to use the file. The amount of instruction is directly tied to the user community's level of SAS knowledge.

Please note that the line you're instructed to change is identical to the one used previously as the example of how the MACRO call is placed inside the SAMPLE program. The difference is that this SAS file will be on the user's account so that each person can specify only the plots he or she wishes to see. The'S' in 'plots' is significant because it enables you to duplicate this line and change the MACRO variable to generate more then one plot. In fact, there is no limit on the number of plots which can be shown or saved with one execution of this program. The first plot is the most expensive because it reads in all the data. All subsequent plots are less costly because they do not have to reread the data. The following are three outputs derived

Page 4: Using SAS® INCLUDE Files and SAS Macro Variables To ... Guest.pdf · Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application. Rick Guest

from executing the SAMPLE program one time. The INCLUDE file contains these MACRO calls:

%DOCHART(DEPT,BUDGET,'SHOW', CHRTNAME);

%DOCHART(DEPT, WIP,'SHOW, CHRTNAME);

%DOCHART(DEPT,NUMEMPS,'SAVE', NUMBDEPT);

Figure 2 represents output from the SAS INCLUDE file macro variable pass 1. The XAX variable is set to DEPT, the YAX variable to BUDGET, the DISP variable to 'SHOW' (for display on screen), and the CHRTNAME variable to CHRTNAME (because 'SHOW and not 'SAVE' is selected).

Figure 3 represents output from the SAS INCLUDE file macro variable pass 2. The XAX variable is set to DEPT, the YAX variable to WIP, the DISP variable to 'SHOW' (for display on screen), and the CHRTNAME variable to CHRTNAME (because 'SHOW and not 'SAVE' is selected).

Figure 4 represents output from the SAS INCLUDE file macro variable pass. The XAX variable is set to DEPT, the YAX variable to NUMEMPS, the DISP variable to 'SAVE' (for saving in an ADMGDF file) and the CHRTNAME variable to NUMBDEPT (the name of the saved chart will be NUMBDEPT ADMGDF).

906

CONCLUSIONS

A simple example of how to use SAS INCLUDE files for passing variables to another piece of SAS code has been presented, demonstrating that this type of code can be used for a wide range of applications. Although your application may require only simple code, SAS INCLUDE files and the macro variable provide the· capability to pass a great deal more information, i.e., SAS data set names, sort criteria, a color map, footnotes, targets, fonts, graphics type (HBAR, VBAR, PIE), PROC SUMMARY criteria, groups variables, formats, etc. Your options are limitless. While a graphics example is used here, the benefits derived from SAS INCLUDE files are not limited to any specific type of output or SAS product.

To be more competitive in today's dynamic business environment, IBM is cutting costs wherever possible. The SAS tools described in this paper have given management the flexibility required to effectively run a manufacturing line at a competitive cost. Hopefully, you will also find these tools useful for your business.

SAS is a registered trademark of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.

Page 5: Using SAS® INCLUDE Files and SAS Macro Variables To ... Guest.pdf · Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application. Rick Guest

B U D G [

1 1 If·····································································/1 I/*TB:tS IS A BAS DrCLlJDB rILl: TO DB OSBD Dr BAS PROGRMI - SAilPLB */1 1I*'l'B:IS rILl: :IS DBS:IGJIfBI) FOR YOU TO STOU: 011 YOUR A DISK AIm PASS '1'BB */1 I/*PARAMBTBRS YOU WISH TO PLO'1' TO A GBIIBRIC BAS PROGUII STORISD ON SOIII*" II*CBII'l'RAL LOCATXON ALOJfG WITH TBB RBQUXRBD DATABASB. YOO' II&Y ADD *'1 I'*MOLTIPLB LIHBS TO 'l'B:IS rILl: TO B:ITBl!R VIn OR SAW lOR MOD PLOTS.*II I'*AT THE BO'1'TOII or 'l'HIS rXLB YO'O' CAR SD ALL TBB X AND Y VARUBUS */1 1'*AVAlLABLB 'l'JDtCIO'GB 'rIIB BAS PROGRAM: SAIIPLB. */1 1/* */1 /I*TO OSB THIS r:!LB YOU SHOULD DB Dl SOIIB TYPB or EDIT NODS. */1 1/* */1 1'* 1) aSPBAT 'l'BB LlNB BBLOW '1'BB ••••••••• LINB '!'HAT STAJtTS */1 1'* *'aDOCBART. *'1 1'* 2) RBKOVB TJtB • CHARACTBR l'ROII TBB 0. LIN'S .n1S'1' CRBA.'l'BD. */1 1/* 3) DlSBRT AIID RIIPLACB AS HBBDBD TO PASS PLOT VARIABLBS. */1 1/* */1 1/* xu: 'nIB VA!UABLB YOO' WART ON TBB X AXIS. */1 11* YAX 'l'BB VAlUABLB YOO WANT 011 TBB Y AXIS. *'1 1/* DXSP LBAVJ: TBB QTJOTBS (0) AND Tl'PB OVO SHOW TO */1 1'* saow YOUR PLO'r OR'1'YPB SAW TO SAW YOOR PLO'l'. */1 1/* CB1lTRAIIl!I: IF YOtJ BAVS SBLBCTBD 'SHOW' IN 'l'BB DISP SPACE */1 1/* 'mEN LBAVB CIIR'l'HloIIB '" CIIR'l'HAIIB, ELSE TYPB IN */1 1/* A MAIm OF UP TO 8 CHARACTUS TO SAVB TBB PlCl'. */1 1/* */1 1/* 4) UPBAT THZS PROCBSS FOR BACH PLO'1' YOU WZO TO SBB ./1 1/. YOU MAY DO AS MANY AS YOU LXD AND BOn 'l'HZS F:ILB AS .11 1/. orrBN AS YOU 1oZQ. */1 1/* S} 0IlCB YOU HAVE Blft'BRBD ALL 'l'BI: CllD.TS YOU WZSB TO so, */1 1/* SAVE THZS FZLB. */1 1/* 6) LDIlC TO 'l'BI: toCATZOli YOUR. DATA AND 'l'BI: SAllPLB PROGRAM ZS */1 1/. S'l'ORBD AND TYPB: DS SAKPLB. YOtJll PLOTS WILL DB DISPLAYBD */1 1/* OR SAVED BASBD 01f YOU DISP CRITERIA. */1 1/* ·/1 1/* *lfO'l'B: IF YOU DO HO'l' KNOW WBBU 'l'BB DATA AND PROGJlAI( :IS STORBD *11 1/* PLBASB CONTACT YODR APPLlCATZOIlS ADMINISTRATOR. *11 1/*·*···**··****·····*···*······***·*·**·······························/1 1 1 1·~BART(XAX,yAX, 'DISP',CBR'l'HAIII!); 1 1 1 1/· * ••• * •• " ••••••••••• * ••• * •••••••• * ••••••••••••••••••• * *··*·*·*·*·**·*·/1 I/.THIS IS A LIST OF AVAlLABLB VARIABLBS FOR THIS PROGRAII: */1 1/· ell I/.X AXIS V.ARlAElLBS = DBPT ·/1 1/· ·/1 I/*Y AXZS VAlUAElLKS = WZP, THRDPD'I', BODGB'I', HtlMBIIPS ./1 1/· ·/1 1/·*···*·***····*·**·*****·**··***··*·*···*···*·**··**·***·············/1 1 1

Figure 1, Example of a SAS INCLUDE file.

BUDGET BY DEPT 130,000

120,000

T 110,000

10

I I I I 2 2 2 2 3 4 5 6 2 5 7 9 0 I 9 9 I I 4 I 7 4 5 4 7 3 2 3 7 3 6 8 N 2 V f G T R C T f B G

DEPT

GENERATED IN SAS PROGRAM - SAMPLE

Figure 2. Output of SAS code with extemal variables generated from an INCLUDE file, showing budget by department.

907

Page 6: Using SAS® INCLUDE Files and SAS Macro Variables To ... Guest.pdf · Using SAS® INCLUDE Files and SAS Macro Variables To Develop An Interactive Graphics Application. Rick Guest

1800 1700 1500 1,00 1400 1300 1100 1100

V 1000 I 900 P 800

700 &00 ,00 400 300 100 100

o

I I 2 5 7 4 N 2

WIP BY DEPT

I I 2 2 2 2 7 9 0 I 9 9 5 4 7 3 2 3 Y f ·C T R C

DEPT

CENERATED IN SAS PROCRAM - SAMPLE

3 4 5 6 I I 4 I 7 3 6 8 T f B C

Figure 3. Output of SAS code with external variables generated from an INCLUDE file, showing work in process (WIP) by department.

N U M E M P

30

5 10

I I 2 5 7 4 N 2

NUMEMPS BY DEPT

I I 2 2 2 2 3 4 5 6 7 9 0 I 9 9 I I 4 I 5 4 7 3 2 3 7 3 6 8 Y f C T R C T f B C

DEPT

CENERATED IN SAS PROGRAM - SAMPLE

Figure 4. Output of SAS code with external variables generated from an INCLUDE file, showing number of employees by department.

908