-
Notifications
You must be signed in to change notification settings - Fork 0
/
11 Macro Variables.sas
380 lines (312 loc) · 8.25 KB
/
11 Macro Variables.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*01 Introduction*/
%let path=/courses/d649d56dba27fe300/STA5067/SAS Data;
libname orion "&path/orion";
%put _automatic_;
%put this is some text to be put to the log;
%put This text was output: &systime &sysday &sysdate9 ;
%put The last dataset created was: &syslast;
/*without macro variables*/
proc freq data=orion.Customer;
table Country / nocum;
footnote1 "Created 11:47 Wednesday, 11Mar2020";
footnote2 "By user jhshows0";
run;
footnote;
/*with macro variables*/
proc freq data=orion.Customer;
table Country / nocum;
footnote1 'Created &systime &sysday, &sysdate9';
footnote2 'By user &sysuserid ';
run;
footnote;
/*with macro variables*/
proc freq data=orion.Customer;
table Country / nocum;
footnote1 "Created &systime &sysday, &sysdate9";
footnote2 "By user &sysuserid";
run;
footnote;
%let mymacvar=Some text (often code goes here);
%put Mymacvar: &mymacvar;
%let txt="Some text in quotes";
%put The macro variable txt is: &txt;
/* without macro variables*/
proc freq data=orion.order_fact;
where year(order_date)=2007;
table order_type;
title "Order Types for 2007";
run;
proc means data=orion.order_fact;
where year(order_date)=2007;
class order_type;
var Total_Retail_Price;
title "Price Statistics for 2007";
run;
title;
/* with macro variables*/
%let year=2006;
proc freq data=orion.order_fact;
where year(order_date)=&year;
table order_type;
title "Order Types for &year";
run;
proc means data=orion.order_fact;
where year(order_date)=&year;
class order_type;
var Total_Retail_Price;
title "Price Statistics for &year";
run;
title;
data normals;
array x{10} x1-x10;
call streaminit(654321);
do rep=1 to 1000;
do n=1 to 10;
x{n}=rand("normal");
end;
mn=mean(of x{*});
output;
end;
run;
ods select histogram;
proc univariate data=normals;
var mn;
histogram mn/normal;
run;
%let obs=100;
%let sampsize=1000;
%let seed=54321;
data normals;
array x{*} x1-x&obs;
call streaminit(&seed);
do rep=1 to &sampsize;
do n=1 to &obs;
x{n}=rand("normal");
end;
mn=mean(of x{*});
output;
end;
run;
ods select histogram;
proc univariate data=normals;
var mn;
histogram mn/normal;
run;
filename iris1 '/courses/d649d56dba27fe300/Data Sets/IrisDataNoHeaderCsv.csv';
PROC IMPORT DATAFILE=iris1
DBMS=CSV
OUT=DataNoHeaderCsv
replace;
GETNAMES=no;
RUN;
%macro impdat(datanm);
filename iris "/courses/d649d56dba27fe300/Data Sets/&datanm..csv";
PROC IMPORT OUT=&datanm
DATAFILE=iris
DBMS=CSV
replace;
GETNAMES=no;
RUN;
%mend;
%impdat(IrisDataNoHeaderCsv);
/*02 Program Flow*/
libname nhanes3 "&path/nhanes3";
proc reg data=nhanes3.chdagesbp outest=betas1;
model spf1=age1;
run;
proc reg data=nhanes3.chdagesbp
outest=betas2
;
model
spf1=age1;run;
/*03 User-Defined Macro Variables*/
%let name= Ed Norton ;
%put The value of the macro variable name is: &name;
%let name2=' Ed Norton ';
%put name2 is &name2;
%let title="Joan's Report";
%put quotes are included in the saved text, title is &title;
%let start=;
%put Macro variable may be assigned a null value: &start;
%let sum=3+4;
%put Macro variables contain text, including numbers, no arithmetic is done. Sum is ∑
%let total=0;
%let total=&total+∑
%put When macro variable definition included macro variables;
%put The included macro variables are "resolved" and then the definition is made;
%put total is now: &total;
%let x = varlist;
%let &x = name age height;
%put The macro variable being defined can be a macro variable. Still the resolution is done first.;
%put &varlist;
%put &x;
%let units = 5;
Proc print data=orion.order_fact;
Where quantity > &units;
Var order_date product_id quantity;
Title "Orders exceeding &units units";
Run;
%let lower = 29;
%let upper = 30;
Proc means data=nhanes3.agesbp;
Var age1 spf1;
Where &lower le age1 le &upper;
Run;
title;
footnote;
%let office=Sydney;
proc print data=orion.employee_addresses;
where city="&office";
var employee_name;
title "&office Employees";
run;
%let date1=25may2007;
%let date2=15jun2007;
proc print data=orion.order_fact;
where order_date between "&date1"d and "&date2"d;
var order_date product_id quantity;
title "Orders between &date1 and &date2";
run;
title;
%put _user_;
Options symbolgen;
%let office=Sydney;
Proc print data=orion.employee_addresses;
Where city="&office";
Var employee_name;
Title "&office Employees";
Run;
title;
options nosymbolgen;
%let x1=First macro text;
%let x2=Second macro text;
%put &x1 &x2;
%symdel x1 x2;
%put &x1 &x2;
/*04 Delimiting macro variables*/
libname nh9ques "&path/nhanes1999/questionnaire";
libname nh9lab "&path/nhanes1999/lab";
libname nh9exam "&path/nhanes1999/exam";
libname nh9demo "&path/nhanes1999/demographics";
libname nh9diet "&path/nhanes1999/dietary";
libname nh9 (nh9demo nh9exam nh9lab nh9ques);
%let mac=waist;
proc means data=nh9.bodymeasurements;
var bmx&mac;
run;
/*Why do we get an error here?*/
%let datanm=IrisDataNoHeaderCsv;
filename iris "/courses/d649d56dba27fe300/Data Sets/&datanm.csv";
PROC IMPORT OUT=&datanm
DATAFILE=iris
DBMS=CSV
replace;
GETNAMES=no;
RUN;
/*Correct way*/
%let datanm=IrisDataNoHeaderCsv;
filename iris "/courses/d649d56dba27fe300/Data Sets/&datanm..csv";
PROC IMPORT OUT=&datanm
DATAFILE=iris
DBMS=CSV
replace;
GETNAMES=no;
RUN;
/*05 macro functions*/
%let lower=lowercaseword;
%let upper=%UPCASE(lowercaseword) ;
%let upper2=%upcase(&lower);
%put lower=&lower upper=&upper upper2=&upper2;
%let str1=thisisastringwithoutspaces;
%let sub1=%SUBSTR(thisisastringwithoutspaces,5,2);
%let sub2=%SUBSTR(&str1,8,6);
%put str1=&str1 sub1=&sub1 sub2=&sub2;
%put date9=&sysdate9;
%put year=%substr(&sysdate9,6);
%let str1=this is a string with spaces;
%let word1=%scan(&str1,2);
%let word2=%scan(this is a string with spaces,4);
%put str1=&str1 word1=&word1 word2=&word2;
data tmp;
do i=1 to 4;
x=i**2;
output;
end;
run;
%put syslast=&syslast;
%put dsn=%scan(&syslast,2,.);
%let dietvars=PROTEIN FAT TOTAL_CARBOHYDRATE Alcohol CALCIUM
PHOSPHORUS IRON SODIUM POTASSIUM SATURATED_FAT
OLEIC_ACID LINOLEIC_ACID CHOLESTEROL;
%put Variables: &dietvars;
/*Find the number of variables*/
%let numvars=%sysfunc(countw(&dietvars));
%put Number of variables= &numvars;
/*Find the fifth variable*/
%let var5=%scan(&dietvars,5);
%put Fifth variable= &var5;
/*find the last variable*/
%let lastvar=%scan(&dietvars,&numvars);
%put Last variable=&lastvar;
%let str1=this is simple text;
%let index1=%INDEX(this is simple text,is);
%let index2=%INDEX(&str1,text);
%put str1=&str1 index1=&index1 index2=&index2;
%let sum=2+7;
%let sum1=%EVAL(&sum);
%let sum2=%EVAL(&sum1>7);
%let sum3=%EVAL(&sum<=7;);
%put sum=&sum sum1=&sum1 sum2=&sum2 sum3=&sum3;
%let x=%eval(2+2);
%put x=&x;
%let z=3;
%let x=%eval(&z+2);
%put x=&x;
%let x=%eval(&x+&z);
%put &x;
%let thisyr=2007;
%let lastyr=%eval(&thisyr-1);
%put thisyr=&thisyr lastyr=&lastyr;
proc means data=orion.order_fact maxdec=2 min max mean;
class order_type;
var total_retail_price;
where year(order_date) between &lastyr and &thisyr;
title1 "Orders for &lastyr and &thisyr";
title2 "(as of &sysdate9)";
run;
%let name=JUSTIN SHOWS;
%put name=&name;
%let name1=%sysfunc(propcase(&name));
%put name1=&name1;
%let varlist=alc bmi sbp diab chol chd;
%let numvars=%SYSFUNC(countw("&varlist"));
%put varlist=&varlist numvars=&numvars;
title1 "%sysfunc(today(),weekdate.)";
title2 "%sysfunc(time(),timeAMPM8.)";
data orders;
set orion.Order_fact;
where year(Order_Date)=2007;
Lag=Delivery_Date - Order_Date;
run;
proc means data=orders maxdec=2 min max mean;
class Order_Type;
var Lag;
run;
title;
%let dietvars=PROTEIN FAT TOTAL_CARBOHYDRATE Alcohol CALCIUM PHOSPHORUS IRON SODIUM
POTASSIUM SATURATED_FAT OLEIC_ACID LINOLEIC_ACID CHOLESTEROL;
%put &dietvars;
%let varlist=%sysfunc(compbl(&dietvars));
%put &varlist;
%let varlist=%sysfunc(translate(&varlist,","," "));
%put &varlist;
%let str1=proc means data=orion.payroll min max;;
%put str1=&str1;
%let str1=proc means data=orion.payroll min max%str(;);
%let str2=%str(proc means data=orion.payroll min max;);
%put str1=&str1;
%put str2=&str2;
%let statement=%str(title "S&P 500";);
%put &statement;
%let statement=%nrstr(title "S&P 500";);
%put &statement;