Lesson 12 More SGPLOT examples Exporting data Macro variables Table Generation - PROC TABULATE Miscellaneous Topics
SCATTER vs SERIES vs REG vs STEP Obs trt month cd Want to plot mean CD4 levels over time for each of two groups.
* 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
* Series plot connects the points, marker option needed to plot symbols at each visit; proc sgplot; series x=month y=cd4/group=trt markers ; run;
* Regression plot does scatter plot and adds regression line; proc sgplot; reg x=month y=cd4/group=trt ; run;
* Step plot connects points with a step function; proc sgplot; step x=month y=cd4/group=trt ; run;
* 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;
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;
Moving a SAS Dataset to another computer Transfer SAS dataset directly - Easy and works on most systems - Can send as 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.
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;
TABLE GENERATION (dbp12 sbp12)*(N MEAN*f=8.1) GROUPALLGROUPALL
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
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
(sex=' ')*(N ROWPCTN*f=10.1) CLINICALLCLINICALL
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’;
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 &ilist; TITLE "PROC Means results for variables &nut and &ilist"; RUN; * Makes it easy to modify code;
Macro Variables Defined using %LET statement Referenced by using ¯ovarname SAS substitutes the value of macrovarname when it encounters ¯ovarname Useful for making a program easy to modify %let macrovarname = characters ;
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
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