-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11 Processing Data Iteratively.sas
594 lines (484 loc) · 10.1 KB
/
11 Processing Data Iteratively.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
libname orion '/courses/d649d56dba27fe300/STA5066';
/*01 Create Accumulating Variables*/
proc contents data=orion.aprsales;run;
proc print data=orion.aprsales;run;
data mnthtot;
set orion.aprsales;
Mth2Dte=Mth2Dte+SaleAmt;
run;
proc print data=mnthtot;
run;
data mnthtot;
set orion.aprsales;
retain Mth2Dte 0;
Mth2Dte=Mth2Dte+SaleAmt;
run;
proc print data=mnthtot;
run;
proc print data=orion.aprsales2;
run;
data mnthtot;
set orion.aprsales2;
retain Mth2Dte 0;
Mth2Dte=Mth2Dte+SaleAmt;
run;
proc print data=mnthtot;
run;
data mnthtot;
set orion.aprsales2;
retain Mth2Dte 0;
Mth2Dte=sum(Mth2Dte,SaleAmt);
run;
proc print data=mnthtot;
run;
data mnthtot2;
set orion.aprsales2;
Mth2Dte+SaleAmt;
run;
proc print data=mnthtot2;
run;
/*02 Accumulating Totals for Groups*/
data example;
call streaminit(57186921);
do id=1 to 6;
num=int(rand("uniform")*5+1); /*num will be in (1,2,3,4,5,6)*/
do obs=1 to num;
x=round(rand("normal",69,3));
output;
end;
end;
drop num obs;
run;
proc print data=example;
run;
data demo;
set example;
by id; /*this creates the
first. and last.
variables*/
first=first.id;
last=last.id;
run;
proc print data=demo;
run;
data numobs;
set example;
by id;
if first.id then numobs=0;
numobs+1;/*automatically retains numobs*/
if last.id then output;
run;
proc print data=numobs;
run;
proc contents data=orion.specialsals;run;
proc print data=orion.specialsals;run;
proc sort data=orion.specialsals
out=salsort;
by Dept;
run;
data deptsals;
set salsort;
by Dept;
first=first.dept;
last=last.dept;
run;
proc print data=deptsals;
run;
proc sort data=orion.specialsals out=salsort;
by Dept;
run;
data deptsals(keep=Dept DeptSal);
set SalSort;
by Dept;
if First.Dept then DeptSal=0;
DeptSal+Salary;
if Last.Dept;
run;
proc print data=deptsals;
run;
proc contents data=orion.projsals;run;
proc print data=orion.projsals;run;
proc sort data=orion.projsals
out=projsort;
by Proj Dept;
run;
proc print data=projsort;run;
data pdsals;
set projsort;
by Proj Dept;
firstproj=first.proj;
lastproj=last.proj;
firstdept=first.dept;
lastdept=last.dept;
run;
proc print data=pdsals;
run;
data pdsals(keep= Proj Dept
DeptSal NumEmps);
set projsort;
by Proj Dept;
if First.Dept then do;
DeptSal=0;
NumEmps=0;
end;
DeptSal+Salary;
NumEmps+1;
if Last.Dept;
run;
proc print data=pdsals noobs;
run;
/*03 Do Loop Processing*/
data compound;
Amount=50000;
Rate=.045;
Yearly=Amount*Rate;
/*Sum statement: variable + expression
The sum statement ignores a missing value for the expression
i.e. the previous total remains unchanged.
*/
Quarterly+((Quarterly+Amount)*Rate/4);/*end of 1st qtr*/
Quarterly+((Quarterly+Amount)*Rate/4);/*end of 2nd qtr*/
Quarterly+((Quarterly+Amount)*Rate/4);/*end of 3rd qtr*/
Quarterly+((Quarterly+Amount)*Rate/4);/*end of 4th qtr*/
run;
proc print data=compound noobs;
title "Interest earned in one year, yearly & quarterly compounding";
run;
title;
data compound;
Amount=50000;
Rate=.045;
Yearly=Amount*Rate;
do i=1 to 4;
Quarterly+((Quarterly+Amount)*Rate/4);
end;
run;
proc print data=compound noobs;
title "Interest earned in one year, yearly & quarterly compounding";
run;
title;
/* why the (drop=i)?*/
data compound(drop=i);
Amount=50000;
Rate=.045;
do i=1 to 20;
Yearly +(Yearly+Amount)*Rate;
end;
do i=1 to 80;
Quarterly+((Quarterly+Amount)*Rate/4);
end;
run;
proc print data=compound;
run;
data invest;
do Year=2018 to 2020;
Capital+5000;
Capital+(Capital*.045);
end;
run;
proc print data=invest;
title "Capital after 3 years at 4.5% annual interest";
run;
title;
proc print data=orion.growth;
run;
data forecast;
set orion.growth;
do Year=1 to 6;
Total_Employees=Total_Employees*(1+Increase);
output;
end;
run;
proc print data=forecast noobs;
run;
data invest;
do until(Capital>1000000);
Year+1;
Capital+5000;
Capital+(Capital*.045);
output;
end;
run;
proc print data=invest noobs;
format Capital dollar14.2;
run;
data invest;
do year=1 to 100 until(Capital>250000 or year>30 );
Capital+5000;
Capital+(Capital*.045);
end;
run;
proc print data=invest noobs;
format Capital dollar14.2;
run;
data invest;
do Year=1 to 100 while (Capital lt 250000 or year=30);
Capital+5000;
Capital+(Capital*.045);
output;
end;
run;
proc print data=invest noobs;
format capital dollar14.2;
run;
title "Output statement outside do loop";
data tmp1;
do i=1 to 3;
end;
output;
run;
proc print data=tmp1;
run;
title "Output statement inside do loop";
data tmp2;
do i=1 to 3;
output;
end;
run;
proc print data=tmp2;
run;
title;
data invest (drop=i) ;
do Year=1 to 5;
Capital+5000;
do i=1 to 4;
Capital+(Capital*.045/4);
end;
output;
end;
run;
proc print data=invest noobs;
format capital dollar14.2;
run;
title "Non-integer increment in do loop";
data tmp;
do i=1 to 3 by .5;/*non-integer increment*/
x=i**2;
output;
end;
run;
proc print data=tmp;
run;
title;
title "Negative increment in do loop";
data tmp;
do i=4 to 1 by -.5;/*negative increment*/
x=i**2;
output;
end;
run;
proc print data=tmp;
run;
title;
data tmp;
do i=1,5,9,14;
x=i**2;
output;
end;
proc print data=tmp;
run;
title "Do Loop with Character Index";
data tmp;
do i='M','F','X';
output;
end;
run;
proc print data=tmp;
run;
title;
/*04 Example, Binary Search (Interval Halving)*/
data tmp;
low=1;
high=2;
do i=1 to 100;
midpt=(high+low)/2;
sqrt3=midpt;
if midpt*midpt<3 then low=midpt;
else high=midpt;
output;
end;
tsqrt3=sqrt(3);
output;
run;
proc print data=tmp;
run;
data tmp;
do i = 1, 1, 2, 3, 5, 8, 13, 21;
x = lag(i);
output;
end;
run;
proc print data=tmp;
run;
data tmp;
do i = 1, 1, 2, 3, 5, 8, 13, 21;
x = lag2(i);
output;
end;
run;
proc print data=tmp;
run;
data tmp;
low=1;
high=2;
do i=1 to 1000 until (abs(diff)<.00001 and diff ne .);
midpt=(high+low)/2;
sqrt3=midpt;
diff=sqrt3-lag(sqrt3);
if midpt*midpt<3 then low=midpt;
else high=midpt;
output;
end;
tsqrt3=sqrt(3);
output;
run;
proc print data=tmp;
run;
/*05 Data Step Arrays*/
title "The Employee Donations Dataset";
proc print data=orion.employee_donations (obs=10);
run;
title;
data charity;
set orion.employee_donations;
keep employee_id qtr1-qtr4;
Qtr1=Qtr1*1.25;
Qtr2=Qtr2*1.25;
Qtr3=Qtr3*1.25;
Qtr4=Qtr4*1.25;
run;
proc print data=charity (obs=10);
run;
data charity;
set orion.employee_donations;
keep employee_id qtr1-qtr4;
array Contrib{4} qtr1-qtr4;
do i=1 to 4;
Contrib{i}=Contrib{i}*1.25;
end;
run;
proc print data=charity (obs=10);
run;
data test;
set orion.employee_donations;
array val{4} qtr1-qtr4;
Tot1=sum(of qtr1-qtr4);
Tot2=sum(of val{*});
run;
proc print data=test;
var employee_id tot1 tot2;
run;
data charity;
set orion.employee_donations;
keep employee_id qtr1-qtr4;
array Contrib{*} qtr1-qtr4;
do i=1 to dim(contrib);
Contrib{i}=Contrib{i}*1.25;
end;
run;
proc print data=charity (obs=10);
run;
data percent(drop=i);
set orion.employee_donations;
array Contrib{4} qtr1-qtr4;
array Percent{4};
Total=sum(of contrib{*});
do i=1 to 4;
percent{i}=contrib{i}/total;
end;
run;
proc print data=percent (obs=10) noobs ;
var Employee_ID percent1-percent4;
format percent1-percent4 percent6.;
run;
data change;
set orion.employee_donations;
drop i;
array Contrib{4} qtr1-qtr4;
array Diff{3};
do i=1 to 3;
diff{i}=contrib{i+1}-contrib{i};
end;
run;
proc print data=change (obs=10) noobs ;
var Employee_ID Diff1-Diff3;
run;
data compare(drop=i Goal1-Goal4);
set orion.employee_donations;
array Contrib{4} Qtr1-Qtr4;
array Diff{4};
array Goal{4} (10,20,20,15);
do i=1 to 4;
Diff{i}=Contrib{i}-Goal{i};
end;
run;
proc print data=compare (obs=10) noobs ;
run;
data compare(drop=i Goal1-Goal4);
set orion.employee_donations;
array Contrib{4} Qtr1-Qtr4;
array Diff{4};
array Goal{4} (10,20,20,15);
do i=1 to 4;
Diff{i}=sum(Contrib{i},-Goal{i});
end;
run;
proc print data=compare (obs=10) noobs;
var Employee_id diff1-diff4 qtr1-qtr4;
run;
data compare(drop=i);
set orion.employee_donations;
array Contrib{4} Qtr1-Qtr4;
array Diff{4};
array Goal{4} _temporary_ (10,20,20,15);
do i=1 to 4;
Diff{i}=Contrib{i}-Goal{i};
end;
run;
proc print data=compare noobs;
var employee_id diff1-diff4;
run;
%let demographics=seqn hsageir hssex dmaethnr dmaracer dmarethn;
libname nhanes3 '/courses/d649d56dba27fe300/STA5066';
proc format ;
value f_HSSEX 1="Male" 2="Female";
value f_RACER 1="White" 2="Black" 3="Other" 8="Mexican-American of unknown race";
value f_rethn 1="Non-Hispanic white" 2="Non-Hispanic black" 3="Mexican-American" 4="Other";
value f_ethnr 1="Mexican-American" 2="Other Hispanic" 3="Not Hispanic";
data bone1;
set nhanes3.exam (keep=&demographics BD:);
/* keep protocol scans and those not qc rejected*/
where bdpexflr=0 and bdpscan ne 2; /*eligible and acceptable scan*/
format hssex f_hssex. dmaracer f_racer. dmarethn f_rethn. dmaethnr f_ethnr.;
run;
proc means data=bone1;
run;
title "Fix Fill-values Femor Neck Data";
data bone2 (drop=i);
set bone1 (drop=BDPTECH BDPEXFLR BDPSCAN);
array bone{17} BDPFNARE--BDPD0;
array unk{17} _temporary_ (8888,16*88888);
do i=1 to 17;
if bone{i} eq unk{i} then bone{i}=.;
end;
run;
proc means data=bone2;
run;
title;
/*06 Longitudinal Data*/
proc contents data=nhanes3.sbp100;
run;
proc print data=nhanes3.sbp100 (obs=10);
run;
data sbp100_long (keep= id exam sbp dth);
set nhanes3.sbp100;
id=_n_;
array s{15} spf1-spf15;
do exam=1 to 15;
if dth eq exam then leave;
sbp=s{exam};
output;
end;
run;
proc print data=sbp100_long (obs=75);
run;