-
Notifications
You must be signed in to change notification settings - Fork 1
/
lesson06_SAScode.sas
213 lines (165 loc) · 4.72 KB
/
lesson06_SAScode.sas
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
* ======================================;
* N736 Lesson 06 SAS Code
* 09/13/2017
*
* Melinda Higgins, PhD
* ======================================;
* ======================================;
* load dataA;
* ======================================;
proc import datafile='C:\MyGithub\N736Fall2017_lesson05\dataA.csv'
out=dataA dbms=csv;
run;
* ======================================;
* load dataB;
* ======================================;
proc import datafile='C:\MyGithub\N736Fall2017_lesson05\dataB.csv'
out=dataB dbms=csv;
run;
* ======================================;
* add a comment to dataset dataA;
* ======================================;
data dataAcomment (label="This is a comment on dataA");
set dataA;
run;
* ======================================;
* see the dataset comment using PROC CONTENTS;
* ======================================;
proc contents data=dataAcomment; run;
* ======================================;
* define some formats;
* ======================================;
proc format;
value likert
1='strongly disagree'
2='disagree'
3='neutral'
4='agree'
5='strongly agree';
value $gender
'm'='male'
'f'='female';
run;
* ======================================;
* view dataA without formats applied;
* ======================================;
proc print data=dataA; run;
* ======================================;
* create dataA2 with formats for var1;
* ======================================;
data dataA2;
set dataA;
format var1 likert.;
run;
* ======================================;
* view dataA2 with formats for var1 applied;
* ======================================;
proc print data=dataA2; run;
* ======================================;
* can also use the formats on the fly;
* ======================================;
proc print data=dataB;
format var2 $gender.;
run;
* ======================================;
* run a frequency table without formatting;
* ======================================;
proc freq data=dataB;
tables var2;
run;
* ======================================;
* add the formatting for the table;
* ======================================;
proc freq data=dataB;
format var2 $gender.;
tables var2;
run;
* ======================================;
* see frequency table without formatting
* ======================================;
proc freq data=dataA;
tables var1;
run;
* ======================================;
* see frequency table with formatting
* ======================================;
proc freq data=dataA2;
tables var1;
run;
* ======================================;
* see frequency table with formatting on the fly;
* and add a title - note global change;
* ======================================;
proc freq data=dataB;
title "Frequency Table of Gender";
format var2 $gender.;
tables var2;
run;
* ======================================;
* the title remains - need to reset;
* ======================================;
proc print data=dataB; run;
* ======================================;
* set title to blank;
* ======================================;
title;
* ======================================;
* no title is shown;
* ======================================;
proc print data=dataB; run;
* ======================================;
* can create up to 3 custom titles
* and custom footnotes;
* ======================================;
title "Univariate Report for var 1 in dataA2";
title2 "Report by Melinda Higgins";
title3 "dated 09/15/2017";
footnote "this is footnote 1";
footnote2 "this is footnote 2";
footnote3 "this is footnote 3";
proc univariate data=dataA2 plots;
var var1;
histogram var1/normal;
run;
* ======================================;
* set all titles and footnotes back to empty;
* ======================================;
title;
title2;
title3;
footnote;
footnote2;
footnote3;
* ======================================;
* concatenate/stack dataA and dataB together
* ======================================;
data dataAB;
set dataA dataB;
run;
proc print data=dataAB noobs; run;
* ======================================;
* define a new format;
* ======================================;
proc format;
value splitYN
0 = 'NO var1 <= 3'
1 = 'YES var1 > 3';
run;
* ======================================;
* create new dataset with a new recoded
* variable and with formatting applied;
* ======================================;
data dataAB_new;
set dataAB;
if var1 > 3 then var1split3 = 1;
else var1split3 = 0;
format var1split3 splitYN.;
run;
* ======================================;
* view all of these changes and the new variable
* with the new formatting and
* add plots using PROC FREQ;
* ======================================;
proc freq data=dataAB_new;
tables var1split3/plots=freqplot;
run;