Lesson 12
Miscellaneous Topics
• More SGPLOT examples
• Exporting data
• Macro variables
• Table Generation - PROC TABULATE
• LSB: See syllabus
SCATTER vs SERIES vs REG vs STEP
Obs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
trt
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
month
0
1
2
3
6
9
12
15
18
21
24
27
30
33
36
0
1
2
3
6
9
12
15
18
21
24
27
30
33
36
cd4
102
177
192
195
217
232
252
270
287
305
323
337
343
360
376
102
184
194
202
223
249
273
299
323
339
363
386
392
394
418
Want to plot CD4 levels over time for
each of two groups.
SCATTER vs SERIES vs REG vs STEP
* 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;
* Series plot connects the points, marker option needed to
plot points;
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 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.
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
Average Blood Pressure by Treatment at 12-Months
(dbp12 sbp12)*(N MEAN*f=8.1)
G
R
O
U
P
A
L
L
„ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ†
‚
‚ 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‚
Šƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒŒ
PROC TABULATE DATA=class.tomhsp FORMAT=8.0;
CLASS group;
Same as PROC MEANS
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;
Closer Look At TABLES Statement
TABLES group ALL='Total',
(dbp12 sbp12)*(N MEAN*f=8.1)/RTS=20;
Statement
information to
Statement
information to
before comma indicates row
display
after comma indicates column
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
N and Percent Men and Women Enrolled by Center
(sex=' ')*(N ROWPCTN*f=10.1)
C
L
I
N
I
C
A
L
L
„ƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ…ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ†
‚
‚
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‚
Šƒƒƒƒƒƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒ‹ƒƒƒƒƒƒƒƒƒƒŒ
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; WITH &ilist;
TITLE "PROC Means results for variables &nut
and &ilist";
RUN;
* Makes it easy to modify code;
Macro Variables
%let macrovarname = characters ;
• 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
Simple Macro to Shorten Code
%macro change(v);
dbpdif&v = dbp&v sbpdif&v = sbp&v choldif&v = chol&v
glucdif&v = gluc&v
%mend change;
dbpbl;
sbpbl;
- cholbl;
- glucbl;
Suppose I want to compute
the change in 4 variables at
3 time points. Can use
macro to help you.
Variables:
data temp;
set temp;
%change(12);
%change(24);
%change(36);
run;
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 MPRINT(CHANGE):
sbpdif12 = sbp12 MPRINT(CHANGE):
choldif12 = chol12
MPRINT(CHANGE):
glucdif12 = gluc12
36
%change(24);
MPRINT(CHANGE):
dbpdif24 = dbp24 MPRINT(CHANGE):
sbpdif24 = sbp24 MPRINT(CHANGE):
choldif24 = chol24
MPRINT(CHANGE):
glucdif24 = gluc24
37
%change(36);
MPRINT(CHANGE):
dbpdif36 = dbp36 MPRINT(CHANGE):
sbpdif36 = sbp36 MPRINT(CHANGE):
choldif36 = chol36
MPRINT(CHANGE):
glucdif36 = gluc36
38
run;
dbpbl;
sbpbl;
- cholbl;
- glucbl;
dbpbl;
sbpbl;
- cholbl;
- glucbl;
dbpbl;
sbpbl;
- cholbl;
- glucbl;
SAS substitutes
the value of v
everywhere there
is an &v