lesson 12 more sgplot examples exporting data macro variables table generation - proc tabulate lsb:...

19

Click here to load reader

Upload: doris-collins

Post on 22-Dec-2015

220 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Lesson 12

• More SGPLOT examples

• Exporting data

• Macro variables

• Table Generation - PROC TABULATE

• LSB: See syllabus

Miscellaneous Topics

Page 2: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

SCATTER vs SERIES vs REG vs STEP

Obs trt month cd4

1 1 0 102 2 1 1 177 3 1 2 192 4 1 3 195 5 1 6 217 6 1 9 232 7 1 12 252 8 1 15 270 9 1 18 287 10 1 21 305 11 1 24 323 12 1 27 337 13 1 30 343 14 1 33 360 15 1 36 376 16 2 0 102 17 2 1 184 18 2 2 194 19 2 3 202 20 2 6 223 21 2 9 249 22 2 12 273 23 2 15 299 24 2 18 323 25 2 21 339 26 2 24 363 27 2 27 386 28 2 30 392 29 2 33 394 30 2 36 418

Want to plot CD4 levels over time for each of two groups.

Page 3: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

* Scatter Plot for 2 group;proc sgplot; xaxis label = 'Months After Start of Therapy' values=(0 to 36 by 6); yaxis label = 'Mean CD4 Level'; title 'Mean CD4 After Start of Therapy by Treatment Type';

scatter x=month y=cd4/group=trt ;

format trt trtF.; label trt = 'Treatment Group';run;

SCATTER vs SERIES vs REG vs STEP

Page 4: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

* Series plot connects the points, marker option needed to plot points;

proc sgplot; series x=month y=cd4/group=trt markers ; run;

Page 5: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

* Regression plot does scatter plot and adds regression line;

proc sgplot; reg x=month y=cd4/group=trt ; run;

Page 6: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

* Step plot connects points with a step function;proc sgplot; step x=month y=cd4/group=trt ; run;

Page 7: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

* Exporting Data;LIBNAME mylib ‘C:\SAS_Files’;DATA temp; SET mylib.sescore; KEEP ptid clinic randdate group educ wt12 sbp12 sescr12;RUN;

* Export data to a comma delimited file;PROC EXPORT DATA=temp OUTFILE = 'C:\SAS_Files\se.csv'

DBMS = csv REPLACE;

Page 8: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Contents of file 'se.csv'ptid,clinic,randdate,group,educ,wt12,sbp12,sescr12

A00083,A,02/05/1987,2,7,125,113,1.05

A00301,A,02/17/1987,6,9,,,1.15

A00312,A,04/08/1987,3,4,131,113,1.15

This data can easily be put into excel by clicking on the file and then saving the file as a worksheet.

*export to excel directly;PROC EXPORT DATA=temp

OUTFILE = 'C:\SAS_Files\se.xls' DBMS = excel ; REPLACE;

Page 9: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Moving a SAS Dataset to another computer

• Transfer SAS dataset directly

- Easy and works on most systems

- Can send as e-mail attachment

• Use PROC CPORT and PROC CIMPORT

Works on all systems but requires you to create an xport (.xpt) file. Can transfer multiple datasets in one file.

Page 10: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Creating a SAS Export File

*Run this on the your computer ;

LIBNAME mylib ‘C:\SAS_Files';FILENAME tranfile ‘C:\SAS_Files\classdata.xpt';PROC CPORT LIB=mylib FILE=tranfile; SELECT sescore tomhsp;RUN;

* Run this on the other computer ;

FILENAME tranfile 'C:\My SAS Datasets\classdata.xpt';PROC CIMPORT LIB=work FILE=tranfile;PROC CONTENTS VARNUM DATA=sescore;PROC CONTENTS VARNUM DATA=tomhsp;RUN;

Page 11: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Average Blood Pressure by Treatment at 12-Months „ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ† ‚ ‚ Diastolic BP ‚ Systolic BP ‚ ‚ ‡ƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒ‰ ‚ ‚ N ‚ Mean ‚ N ‚ Mean ‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰ ‚Rx Group ‚ ‚ ‚ ‚ ‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ‰ ‚ ‚ ‚ ‚ ‚Beta Blocker ‚ 15‚ 77.8‚ 15‚ 120.2‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰ ‚CCB ‚ 17‚ 81.7‚ 17‚ 124.1‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰ ‚Diuretic ‚ 14‚ 78.1‚ 14‚ 117.4‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰ ‚Alpha Blocker ‚ 14‚ 77.7‚ 14‚ 127.6‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰ ‚Ace Inhibitor ‚ 13‚ 79.6‚ 13‚ 123.2‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰ ‚Placebo ‚ 19‚ 79.6‚ 19‚ 129.9‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ‰ ‚Total ‚ 92‚ 79.2‚ 92‚ 124.0‚ Šƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒŒ

TABLE GENERATION

(dbp12 sbp12)*(N MEAN*f=8.1)

GROUP

ALL

Page 12: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

PROC TABULATE DATA=class.tomhsp FORMAT=8.0;

CLASS group;

VAR sbp12 dbp12;

TABLES group ALL='Total',

(dbp12 sbp12)*(N MEAN*f=8.1)/RTS=20;

LABEL dbp12 = 'Diastolic BP';

LABEL sbp12 = 'Systolic BP';

LABEL group = 'RX Group';

FORMAT group fgroup.;

TITLE 'Average Blood Pressure at 12-Months';

RUN;

Same as PROC MEANS

Page 13: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Closer Look At TABLES Statement

TABLES group ALL='Total',

(dbp12 sbp12)*(N MEAN*f=8.1)/RTS=20;

Statement before comma indicates row information to display

Statement after comma indicates column information to display

A * indicates to crosstabulate data

A space indicates to concatenate data

Words: For each group and the total display the N and mean of diastolic and systolic BP

Page 14: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

N and Percent Men and Women Enrolled by Center „ƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ† ‚ ‚ Men ‚ Women ‚ ‚ ‡ƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒ‰ ‚ ‚ N ‚ Percent ‚ N ‚ Percent ‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒ‰ ‚Clinical ‚ ‚ ‚ ‚ ‚ ‚Center ‚ ‚ ‚ ‚ ‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒ‰ ‚ ‚ ‚ ‚ ‚Birmingham ‚ 12‚ 66.7‚ 6‚ 33.3‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒ‰ ‚Chicago ‚ 20‚ 69.0‚ 9‚ 31.0‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒ‰ ‚Minneapolis ‚ 30‚ 83.3‚ 6‚ 16.7‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒ‰ ‚Pittsburgh ‚ 11‚ 64.7‚ 6‚ 35.3‚ ‡ƒƒƒƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒˆƒƒƒƒƒƒƒƒƒƒ‰ ‚Total ‚ 73‚ 73.0‚ 27‚ 27.0‚ Šƒƒƒƒƒƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒƒƒŒ

(sex=' ')*(N ROWPCTN*f=10.1)CLINIC

ALL

Page 15: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

PROC TABULATE DATA=class.tomhsp FORMAT=8.;

CLASS clinic sex;

TABLE (clinic ALL='Total'),

(sex=' ')*(N ROWPCTN*f=10.1)/RTS=15;

FORMAT sex sex. clinic $clinic.;

LABEL clinic = 'Clinical Center';

KEYLABEL ROWPCTN = 'Percent';

TITLE 'N and Percent Men and Women Enrolled

by Center';

RUN;

ODS HTML FILE = ‘mytable.html’;

Page 16: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Macro Variables and Use

LIBNAME t ‘C:\SAS_Files';

%let nut = kcalbl dcholbl calcbl sodbl;%let ilist = income educ;%let options = N MEAN STDDEV;

DATA temp; SET t.tomhsp (KEEP=ptid clinic &nut &ilist);PROC MEANS DATA=temp &options; VAR &nut; WITH &ilist;TITLE "PROC Means results for variables &nut

and &ilist";RUN;* Makes it easy to modify code;

Page 17: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Macro Variables

• Defined using %LET statement

• Referenced by using &macrovarname

• SAS substitutes the value of macrovarname when it encounters &macrovarname

• Useful for making a program easy to modify

%let macrovarname = characters ;

Page 18: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Simple Macro to Shorten Code%macro change(v); dbpdif&v = dbp&v - dbpbl; sbpdif&v = sbp&v - sbpbl; choldif&v = chol&v - cholbl; glucdif&v = gluc&v - glucbl;%mend change;

data temp; set temp; %change(12); %change(24); %change(36);run;

Suppose I want to compute the change in 4 variables at 3 time points. Can use macro to help you.

Variables:

Dbp12,24,36 and dbpbl

Sbp12,24,36 and sbpbl

Chol12,24,36 and cholbl

Gluc12,24,36 and glucbl

Page 19: Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE LSB: See syllabus Miscellaneous Topics

Simple Macro to Shorten Code%macro change(v);36 %change(12);MPRINT(CHANGE): dbpdif12 = dbp12 - dbpbl;MPRINT(CHANGE): sbpdif12 = sbp12 - sbpbl;MPRINT(CHANGE): choldif12 = chol12 - cholbl;MPRINT(CHANGE): glucdif12 = gluc12 - glucbl;36 %change(24);MPRINT(CHANGE): dbpdif24 = dbp24 - dbpbl;MPRINT(CHANGE): sbpdif24 = sbp24 - sbpbl;MPRINT(CHANGE): choldif24 = chol24 - cholbl;MPRINT(CHANGE): glucdif24 = gluc24 - glucbl;37 %change(36);MPRINT(CHANGE): dbpdif36 = dbp36 - dbpbl;MPRINT(CHANGE): sbpdif36 = sbp36 - sbpbl;MPRINT(CHANGE): choldif36 = chol36 - cholbl;MPRINT(CHANGE): glucdif36 = gluc36 - glucbl;38 run;

SAS substitutes the value of v everywhere there is an &v