-
Notifications
You must be signed in to change notification settings - Fork 5
/
A55135.sas
2779 lines (2298 loc) · 85.2 KB
/
A55135.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
***********************************************************************
This file contains selected programs from the book, "Reporting
from the Field: SAS(R) Software Experts Present Real-World Report-Writing
Applications" (pubcode 55135)
Copyright (C) 1994 by SAS Institute Inc., Cary, NC, USA.
SAS(R) is a registered trademark of SAS Institute Inc.
SAS Institute does not assume responsibility for the accuracy of any
material presented in this file.
***********************************************************************
---------
CHAPTER 1
---------
/* The following code appears in Chapter 1, p. 5 */
data uniform;
do i = 1 to 1000;
point=round(ranuni(0) * 10 +.5),1);
output;
end;
proc freq;
tables point;
/* The following code appears in Chapter 1, p. 6 */
proc format;
value typefmt
1='Abdominal'
2='Head & Neck'
3='Extremities' 4='OB/Gyn'
5='Thoracic';
data report;
set hospital;
keep type hospital group state;
array hosp hosp1-hosp5;
array grp group1-group5;
array st state1-state5;
do _i_=1 to 5;
type=put(_i_,typefmt.);
hospital=hosp;
group=grp;
state=st;
output;
end;
/* The following code appears in Chapter 1, pp. 11-12 */
data schedule;
i=0;
do fireman= 'A','B','C','D';
begin= '02jan94'd;
/**** Prevent D from a pre-schedule start. ****/
if (fireman='D') then do;
begin = intnx('WEEK',begin,3);
i=0;
end;
do shift=1 to 4;
on=begin;
off = intnx('WEEK',on,9);
/**** Initiate staggered reliefs. ****/
if (shift=1) then off = intnx('WEEK',on,9 - (3 * i));
begin = intnx('WEEK',off,3);
output;
end;
i+1;
end;
proc print;
by fireman;
id shift;
var on off;
format on off date9.;
title 'Volunteer Fireman Schedule';
/* The following code appears in Chapter 1, p. 13 */
data pick;
retain sample 10 pop 100;
do i=1 to 100;
random=ranuni(0);
if (random< sample / pop) then do;
sample=sample - 1; output;
end;
pop=pop - 1;
end;
/* The following code appears in Chapter 1, p. 18 */
data setup;
infile in;
input age gender $;
if (age=10) then age1=1;
else if (age=27) then age2=1;
else if (age=42) then age3=1;
else if (age=47) then age4=1;
else if (age=72) then age5=1;
if (gender='F') then gen1=1;
else if (gender='M') then gen2=1;
proc means noprint;
var age age1-age5 gen1-gen2;
output out=stats mean(age)= sum(age1-gen2)=;
data report;
set stats;
array pct age1-age5 gen1-gen2;
do over pct;
pct=round((100 * pct) / _freq_),.1);
end;
proc print;
title 'Mean Age Plus Age & Gender Percents';
---------
CHAPTER 2
---------
/* The following code appears in Chapter 2, pp. 30-31 */
/**** A format designating selected zipcodes is created. ****/
proc format;
value $zip
'84001' = 'Y'
'84005' = 'Y'
'84010' = 'Y'
'84013' = 'Y'
'84050' = 'Y'
'84070' = 'Y'
'84088' = 'Y'
'84100' = 'Y';
/**** A 10000 observation data set with incremental zipcode values is
generated ****/
data zips;
do j=1 to 100;
do i=1 to 100;
zip=put(84000 + i, 5.);
output;
end;
end;
/**** A subset is selected using the subsetting IF statement. ****/
data sub_if;
set zips;
if (put(zip,$zip.)='Y');
/**** A subset is selected using the WHERE data set option. ****/
data wheredso;
set zips(where=(put(zip,$zip.)='Y'));
/**** A subset is slected using the DELETE statement. ****/
data deletes;
set zips;
if (put(zip,$zip.)^='Y') then delete;
/**** A subset is selected using the OUTPUT statement. ****/
data outputs;
set zips;
if (put(zip,$zip.)='Y') then output;
---------
CHAPTER 3
---------
/* The following code appears in Chapter 3, p. 40 */
proc format;
value $zip0fmt
'0001' = '0011000'
'0002' = '0011000'
'0003' = '0011031'
'0005' = '0011020'
;
data temp;
set zips;
length zipc $1 coutny $3 tract $4;
zipc = substr(zip,1);
if (zipc='0') then string = put(zip, $zip0fmt.);
else if (zipc='1') then string = put(zip, $zip1fmt.);
else if (zipc='2') then string = put(zip, $zip2fmt.);
else if (zipc='3') then string = put(zip, $zip3fmt.);
else if (zipc='4') then string = put(zip, $zip4fmt.);
else if (zipc='5') then string = put(zip, $zip5fmt.);
else if (zipc='6') then string = put(zip, $zip6fmt.);
else if (zipc='7') then string = put(zip, $zip7fmt.);
else if (zipc='8') then string = put(zip, $zip8fmt.);
else if (zipc='9') then string = put(zip, $zip9fmt.);
county = substr(string,1,3);
track = substr(string,4,4);
/* The following code appears in Chapter 3, pp. 42-43 */
/**** Create a format for related variables. ****/
proc format;
value $group
'A'='Age of Buyer'
'E'='Education Level'
'M'='Marital Status';
/**** Format original variable names to comply with row descriptions. ****/
value $rows
'AGE1'='Less than 30'
'AGE2'='30-34'
'AGE3'='35-39'
'AGE4'='40-44'
'AGE5'='45-49'
'AGE6'='50-54'
'AGE7'='55-59'
'AGE8'='60-64'
'AGE9'='65 and over'
'AGEM'='Median Age'
'ED1'='High School Graduate'
'ED2'='Some College'
'ED3'='College Graduate'
'ED4'='Technical, Other'
'MS1'='Married'
'MS2'='Single'
'MS3'='Other';
data demo;
set trucks (keep=age educ ms seg);
/**** Create category freqency variables. ****/
if (age < 28) then age1=1;
else if (age=32) then age2=1;
else if (age=37) then age3=1;
else if (age=42) then age4=1;
else if (age=47) then age5=1;
else if (age=52) then age6=1;
else if (age=57) then age7=1;
else if (age=62) then age8=1;
else if (age=68) then age9=1;
agem = age + 3;
if (ms='M') then ms1 =1;
else if (ms='S') then ms2=1;
else if (ms='O') then ms3=1;
keep age1--ms3 seg agem;
/**** Summarize the data. ****/
proc means data=demo nway noprint;
class seg;
var age1--ms3;
output out=sdemo sum(age1--ms3)=;
/**** Invoke the Percentile Macro. ****/
%pctl(50,agem,by=seg,out=medage)
/**** Compute percents using the category frequencies. ****/
data pct;
length age1-age9 agem ed1-ed4 ms1-ms3;
merge sdemo medage;
by seg;
array pct age1-age9 ed1--ms3;
do over pct;
pct = (100 * pct) / _freq_;
end;
/**** Transpose the data orientation. ****/
proc transpose data=pct out-tdemo;
id seg;
var age1--ms3;
/**** create logical grouping variable tans sort if necessary. ****/
data tdemo;
set tdemo;
group = substr(_name_,1,1);
data _null_;
file out ll=lines print header=top;
set tdemo;
by group;
/**** Govern spacing between groups with FIRST. processing. ****/
if (first.group) then do;
if (lines<20) then put _age);
else put //;
put @1 group $group. / @1 15 * '-';
/**** Conditionally output statistics witht he appropriate format. ****/
if (_name_='AGEM') then put
@3 _name_ $rows. @23 (bus--utility) (5.2 +3);
else put @3 _name_ $rows. @23 (bus--utility) (5.1 +3);
return;
top:
put 'Demographic Characteristics by Vehicle Type' /
@15 43 * '-' /
@24 'BUS MINIVAN PICKUP UTILITY';
return;
run;
/* The following code appears in Chapter 3, pp. 48-49 */
/**** Macro for Computing Percentiles of Categorical Data ****/
%MACRO PCTL(PCTL,ON,BY=,IN=&SYSLAST,OUT=DATA1,WEIGHT=);
%IF (%STR(&BY)^=%STR()) %THEN %DO;
%LET BYSTR=&BY;
%LET NULL=%STR( );
%LET LAST=%INDEX(&BYSTR,&NULL);
%DO %WHILE (&LAST ^=0);
%LET BYSTR=%SUBSTR(&BYSTR,&LAST);
%LET LAST=%INDEX(&BYSTR,&NULL);
%END;
%LET FIRSTBY=FIRST.&BYSTR;
%LET LASTBY=LAST.&BYSTR;
%STR(PROC SORT DATA=&IN;);
%STR(BY &BY;);
%END;
%ELSE %DO;
%LET FIRSTBY=_N_=1;
%LET LASTBY=EOF;
%END;
%STR(PROC FREQ DATA=&IN;);
%IF (%STR(&BY)^=%STR()) %THEN %DO;
%STR(BY &BY;);
%END;
%STR(TABLES &ON / NOPRINT OUT=&OUT;);
%IF (%STR(&WEIGHT)^=%STR()) %THEN %STR(WEIGHT &WEIGHT;);
DATA &OUT;
SET &OUT(WHERE=(&ON^=.)) END=EOF;
KEEP &ON &BY;
%IF (%STR(&BY)^=%STR()) %THEN %DO;
%STR(BY &BY;);
%END;
LASTON = LAG(&ON);
LASTPCT = LAG(PERCENT);
IF (&FIRSTBY) THEN SUMPCT=0;
IF (SUMPCT>=&PCTL) THEN DELETE;
SUMPCT + PERCENT;
IF (SUMPCT>=&PCTL) THEN DO;
IF (&FIRSTBY | &LASTBY) THEN DO;
IF (&FIRSTBY) THEN CELL='RIGHT';
ELSE CELL='LEFT';
PUT ">> WARNING: &PCTL.th PERCENTILE OF &ON IS IN "
CELL $5. "-MOST CELL.";
PUT '>> A MISSING VALUE WILL BE RETURNED';
%IF (%STR(&BY)^=%STR()) %THEN %DO;
PUT 'FOR BY VALUES ' &BY;
%END;
&ON=.;
OUTPUT;
END;
ELSE DO;
IF(SUMPCT>&PCTL) THEN &ON = ((&PCTL - LASTPCT) / (SUMPCT - LASTPCT))
* (&ON - LASTON +1) + LASTON;
OUTPUT;
END;
END;
LABEL &ON="&ON &PCTL.th PCT EST. FROM CUM. DIST.";
%mend;
---------
CHAPTER 5
---------
/* The following code appears in Chapter 5, p. 75 */
proc tabulate data=repwrite.health;
class year gender age smoker chol200 exercise;
table year, gender*age, chol200 smoker exercise;
run;
/*-----------------------------------------------------------------------*/
proc tabulate data=repwrite.health;
class year gender age smoker chol200 exercise;
var weight systol diast chol;
table year,
gender*age,
weight systol diast chol chol200 smoker exercise;
run;
/* The following code appears in Chapter 5, p. 76 */
proc tabulate data=repwrite.health;
class year gender age smoker chol200 exercise;
var weight systol diast chol;
table year,
gender*age,
n weight*mean systol*mean diast*mean
chol*mean chol*max
chol200*pctn smoker*pctn exercise*pctn;
run;
/* The following code appears in Chapter 5, p. 78 */
proc tabulate data=repwrite.health;
class year gender age smoker chol200 exercise;
var weight systol diast chol;
table year,
all gender*(age all),
n weight*mean systol*mean diast*mean
chol*mean chol*max
chol200*pctn smoker*pctn exercise*pctn;
run;
/*-----------------------------------------------------------------------*/
proc tabulate data=repwrite.health;
class year gender age smoker chol200 exercise;
var weight systol diast chol;
table year,
all gender*(age all),
n weight*mean systol*mean diast*mean
chol*mean chol*max
chol200*pctn<chol200> smoker*pctn<smoker>
exercise*pctn<exercise>;
run;
/* The following code appears in Chapter 5, p. 81 */
title1 'ANNUAL SUMMARY OF HEALTH ASSESSMENT PROGRAM';
title2 'BY GENDER AND AGE';
options pageno=1 ls=117 ps=45 center;
proc format;
value agefmt 21-35='21-35'
36-55='36-55'
56-high='56+';
value $gendfmt 'F'='Female'
'M'='Male';
value $ynfmt 'N'='No'
'Y'='Yes';
run;
proc tabulate data=repwrite.health missing;
class year gender age smoker chol200 exercise;
var weight systol diast chol;
table year,
all gender*(age all),
n*f=4.
(weight systol diast)*mean='Mean'*f=9.
chol*(mean='Mean'*f=7. max='Maximum'*f=7.)
chol200*pctn<chol200>*f=7.1 smoker*pctn<smoker>*f=7.1
exercise*pctn<exercise>*f=7.1 /
box=_page_ rts=18 misstext='0';
keylabel all='Total'
pctn='Percent';
format age agefmt. gender $gendfmt. chol200 smoker exercise $ynfmt.;
run;
/* The following code appears in Chapter 5, p. 84 */
proc tabulate data=repwrite.health;
class year gender age chol200;
var chol;
table gender*age, year*chol*mean year*chol200*pctn;
run;
/*-----------------------------------------------------------------------*/
proc tabulate data=repwrite.health;
class year gender age chol200;
var chol;
table all gender*(age all),
year*chol*mean year*chol200*pctn;
run;
/* The following code appears in Chapter 5, p. 85 */
proc tabulate data=repwrite.health;
class year gender age chol200;
var chol;
table all gender*(age all),
year*chol*mean year*chol200*pctn<chol200>;
run;
/* The following code appears in Chapter 5, p. 86 */
title 'CHOLESTEROL RESULTS BY YEAR FOR HEALTH ASSESSMENT PROGRAM';
options pageno=1 ls=110 ps=45 center;
proc format;
value agefmt 21-35='21-35'
36-55='36-55'
56-high='56+';
value $gendfmt 'F'='Female'
'M'='Male';
value $ynfmt 'N'='No'
'Y'='Yes';
run;
proc tabulate data=repwrite.health;
class year gender age chol200;
var chol;
table all gender*(age all),
year*chol='Mean Cholesterol'*mean*f=12.
year*chol200*pctn<chol200>*f=9.1 /
rts=21 misstext='0';
format gender $gendfmt. age agefmt. chol200 $ynfmt.;
keylabel pctn='Percent'
mean=' '
all='Total';
run;
---------
CHAPTER 6
---------
/* The following code appears in Chapter 6, p. 94 */
/* ******************************************* */
/* Write to more than one file */
filename inv 'invoice';
filename sales 'rpt';
data _null_;
date = '04jun78'd;
name = 'Jones';
code = '123';
cost = 4285.32;
file inv;
put @4 'Buyer ' name cost;
file sales;
put @2 'item code ' code 'cost' cost;
run;
/* ********************************************** */
/* Creates a report with one observation per page */
data claims;
client='Industrial Hardware';
cases=437;
clchrg= 42798;
allowed=36731;
savings=clchrg-allowed;
opcases=153;
opchrg=15364;
opallow=14871;
opsav= opchrg-opallow;
run;
options ls=65;
/* The following code appears in Chapter 6, pp. 97-98 */
data _null_;
set claims;
file print;
link title;
put // 'Summary of all Cases';
put / @5 'Number of claims: ' @25 cases;
put @10 'Claim amount: ' @25 clchrg dollar11.0;
put @10 'Allowed amount: ' @25 allowed comma11.0;
put @10 'Savings: ' @25 savings dollar11.0;
link foot;
return;
title:
offset = (60-length(client))/2;
put @offset client //;
return;
foot:
string = 'Savings realized during 1994';
offset = (60-length(string))/2;
put // @offset string _page_;
return;
run;
filename allcase '\author\datanull\allcase.txt';
filename opcase '\author\datanull\opcase.txt';
/* The following code appears in Chapter 6, p. 99 */
data _null_;
set claims;
file allcase;
link title;
put // 'Summary of all Cases';
put / @5 'Number of claims: ' @25 cases;
put @10 'Claim amount: ' @25 clchrg dollar11.0;
put @10 'Allowed amount: ' @25 allowed comma11.0;
put @10 'Savings: ' @25 savings dollar11.0;
link foot;
file opcase;
link title;
put // 'Summary of out patient cases';
put / @5 'Out patient claims: ' @25 opcases;
put @10 'Claim amount: ' @25 opchrg dollar11.0;
put @10 'Allowed amount: ' @25 opallow comma11.0;
put @10 'Savings: ' @25 opsav dollar11.0;
link foot;
return;
title:
offset = (60-length(client))/2;
put @offset client //;
return;
foot:
string = 'Savings realized during 1994';
offset = (60-length(string))/2;
put // @offset string _page_;
return;
run;
/* The following code appears in Chapter 6, p. 100 */
/* ********************************************** */
/* Creates a multi-column report. */
libname sasclass '\training\sas\sas608\data';
options nonumber nodate ps=50;
data phnums (keep=lname fname ext);
set sasclass.clinics(keep=lname fname ssn);
ext= substr(ssn,1,4);
run;
proc sort data=phnums nodupkey;
by lname fname;
run;
filename phonelst '\author\datanull\phonelst.txt';
data _null_;
file phonelst n=ps ls=65 notitle;
string = 'Industrial Manufacturing Employee Extensions';
center=(60-length(string))/2;
put @center string;
do col = 1, 31;
do row=4 to 40;
set phnums;
name = trim(lname) ||', '|| fname;
put #row @col ext name;
end;
end;
put _page_;
run;
---------
CHAPTER 7
---------
/* The following code appears in Chapter 7, pp. 112-113 */
client:
method;
* this data is included as an example;
lname='Jones';
fname='Laura';
date = '27sep94'd;
reviewer = 'Clint Smith';
company = 'ABC Manufacturing';
rc = filename('clmrpt',
'<<system specific information\>>clmrpt.txt');
flout = fopen('clmrpt','o',81,'p');
pad = int((50-length(company))/2);
if pad lt 0 then pad=0;
rc = fpos(flout,pad+10);
rc = fput(flout,company);
rc = fwrite(flout,'1');
rc = fwrite(flout,' ');
rc = fpos(flout,5);
rc = fput(flout,'Client name');
rc = fwrite(flout, ' ');
if lname = ' ' then line1=' ';
else line1 = trim(fname) ||' '|| lname;
rc = fpos(flout,5);
rc = fput(flout,line1);
rc = fwrite(flout, ' ');
rc = fwrite(flout, ' ');
rc = fpos(flout,10);
rc = fput(flout,'Completed By: ');
rc = fpos(flout,27);
rc = fput(flout,reviewer);
rc = fwrite(flout,' ');
rc = fclose(flout);
endmethod;
---------
CHAPTER 8
---------
/* The following code appears in Chapter 8, p. 117 */
DATA A;
INPUT LEVEL RESPONSE;
CARDS;
1 21
1 23
2 41
2 51
3 33
3 29
;
PROC TABULATE;
VAR RESPONSE;
CLASS LEVEL;
TABLE LEVEL ALL, RESPONSE * MEAN;
RUN;
/* Use your own data set or key this in and then use the above
TABULATE commands to produce the result shown above. */
/* The following code appears in Chapter 8, p. 118 */
DATA B;
DO SKILL= 'A' , 'B';
DO AGE= 9 TO 11;
INPUT WT @@;
OUTPUT;
END;
END;
CARDS;
3 4 5 8 7 6
PROC TABULATE;
FREQ WT;
CLASS SKILL AGE;
TABLE AGE, SKILL*(N PCTN);
RUN;
/* Modify the above code to produce column percent with the following:/
TABLE AGE, SKILL*(N PCTN<SKILL>);
/*then modify the code to produce row percent with the following:/
TABLE AGE, SKILL*(N PCTN<AGE>);
/* The following code appears in Chapter 8, p. 119 */
DATA BPRESS;
INPUT DBP SBP;
CARDS;
88 149
81 122
PROC TABULATE;
VAR SBP DBP;
TABLE SBP DBP, N MEAN STD MIN MAX;
RUN;
/*-----------------------------------------------------------------------*/
PROC TABULATE;
VAR SBP DBP;
KEYLABEL N='SAMPLE SIZE' STD='STANDARD DEVIATION' MIN='MINIMUM'
MAX='MAXIMUM';
TABLE SBP DBP, N MEAN STD MIN MAX/RTS=5;
RUN;
/* The following code appears in Chapter 8, p. 120 */
PROC TABULATE FORMCHAR='
/* The following code appears in Chapter 8, p. 121 */
DATA MISS;
INPUT DISEASE RISK X @@;
CARDS;
0 1 1 1 0 1 0 0 1
1 1 . 0 1 . 1 1 .
1 0 . 0 0 . 1 1 .
PROC TABULATE;
CLASS DISEASE RISK X;
TABLE DISEASE, RISK;
RUN;
/*-----------------------------------------------------------------------*/
DATA MISS;
INPUT DISEASE RISK X @@;
CARDS;
. 1 3. 1 0 2. 0 0 1.
1 1 . 0 1 . 1 1 .
1 0 . 0 0 . 1 1 .
PROC TABULATE MISSING;
CLASS DISEASE RISK X ;
TABLE DISEASE, RISK;
RUN;
/* The following code appears in Chapter 8, p. 122 */
DATA AGEDIST;
INPUT AGE WT @@;
CARDS;
19 3 27 4 33 5 54 4 59 3 63 4 74 3 83 1
PROC FORMAT;
VALUE AGE 10-<20 = '10 TO 20'
20-<40 = '20 TO 40'
40-<65 = '40 TO 65'
65-<999= '65 AND UP'
;
PROC TABULATE;
CLASS AGE;
FORMAT AGE AGE.;
TABLE AGE;
FREQ WT;
/* The following code appears in Chapter 8, p. 123 */
DATA;
SET;
WT=WT*100000;
PROC TABULATE;
VAR WT;
CLASS AGE;
FORMAT AGE AGE.;
TABLE AGE,WT*MEAN*F=DOLLAR12.2;
RUN;
----------
CHAPTER 10
----------
/* The following code appears in Chapter 10, p. 153 */
01 /* ----------------------------------------------------------*/
02 /* Create the BTEQ program to get the data from the Teradata.*/
03 /* This is done in an interactive environment where the */
04 /* user's answers to SAS/AF SCL questions are used to create */
05 /* a customized BTEQ program which contains SQL code. */
06 /* ----------------------------------------------------------*/
07 /* The purpose of this example is not to teach you how to */
08 /* write TeraData code, so the proper code isn't shown here. */
09 /* ----------------------------------------------------------*/
/* Once the TeraData program has been created, we execute it from
within SAS. */
10
11 /* ----------------------------------------------------------*/
12 /* Execute the BTEQ program and create the TERA file */
13 /* ----------------------------------------------------------*/
14 x 'runbteq WREXAMPL';
15
/* Lines 16 through 30 Now you get to the heart of the matter. You have created
a varying-length file whose records consist of a small fixed portion, followed
by two potentially very large text fields. The first one might be as large as
5000 bytes, whereas the second one is limited to 2000 bytes. */
16 /* ----------------------------------------------------------*/
17 /* Process the TERA file into a SAS data set */
18 /* ----------------------------------------------------------*/
/* Lines 19 through 27 read the fixed portion of each record using a FILENAME
statement and a DATA step consisting of INFILE and INPUT statements. */
19 filename TERA 'J4EHEWL.TERA.EXTRACT';
20 data RPT;
21 infile TERA;
22 input
23 VAR1 $char30.
24 VAR2 ib4.
25 VAR3 pd3.4
26 VAR4 $char10.
27 @;
/* Note the @ sign in line 27. This leaves the SAS pointer positioned at the
beginning of the first varying-length text field. */
/* Line 28 uses the macro VARCHAR to process the first text field into an array
called LONG1. Note that the length parameter is set to 200. Although the
macro allows you to use any length less than 201, you don't need to use any
length other than 200. The third parameter in line 28 is 25. The first
varying-length text field can be 5000 bytes long. 5000/200=25 Therefore,
25 array variables 200 characters long are required to hold the maximum
possible field. For more information about the VARCHAR macro, see
"Explanation of the VARCHAR Macro" and Appendix B later in this chapter. */
28 %varchar(LONG1,200,25);
/* The following code appears in Chapter 10, p. 154 */
/* Line 29 processes the second text field into an array called LONG2. Since
the second text field is limited to 2000 bytes, the third parameter is 10
2000/200). */
29 %varchar(LONG2,200,10);
30 run;
31 /* ----------------------------------------------------------*/
32 /* Write the Report */
33 /* ----------------------------------------------------------*/
/* Lines 34 through 37 You now have a SAS data set where each SAS record
contains two arrays plus the four fixed length fields). The following
statements set up the DATA _NULL_ step used to write the report. */
34 data _null_;
35 set RPT end=EOF;
36 file PRINT header=HEADING linesleft=LINES notitles
37 linesize=120;
/* Lines 38 through 40 use the WORDRAP macro to turn the 200 byte array called
LONG1 into a 110 byte array called TEXT1. INLEN is set to 200 because the
second parameter in line 28 is 200. INVARS is set to 25 because the third
parameter in line 28 is 25. OUTLEN is set to the amount of room you want to
allow for printing, in this case 110. This is the variable that changes in each
use of WORDRAP; sometimes you have a wide area to print in, and sometimes it's
fairly narrow. */
/* Note that OUTVARS is larger than just text-field length divided by OUTLEN. In
other words, 5000 / 110 = 46, but 50 is used here. You should always make
OUTVARS about 10% bigger than the exact calculation. If you used 46, you
would not be guaranteed that the reformatted text would fit in 46 array
variables. */
38 %wordrap(inbase=LONG1,outbase=TEXT1,inlen=200,outlen=110,
39 invars=25,outvars=50);
40 MAX1 = WORDRAOP;
/* Line 40 is critical and line 43 is used for consistency. You must save the
value in WORDRAOP in line 40 before you use WORDRAP a second time
(in line 41). */
/* Lines 41 through 43 turn the array LONG2 into the array TEXT2. This time,
INVARS is set to 10 because the third parameter of line 29 is 10. */
41 %wordrap(inbase=LONG2,outbase=TEXT2,inlen=200,outlen=110,
42 invars=10,outvars=20);
43 MAX2 = WORDRAOP;
44 if LINES < 6 then link FOOTING;
/* For more information on the WORDRAP macro, see Appendix A. */
/* Lines 45 through 49. You print the 4 fixed variables in the normal manner,
using a PUT statement. */
45 put @1 VAR1
46 @33 VAR2
47 @45 VAR3
48 @65 VAR4
49 /;
/* The following code appears in Chapter 10, p. 155 */
/* Lines 50 through 53. These statements print the first text field. As many
rows as necessary are used to print your text; however, remember the width of
the field is limited to 110. */
50 if MAX1 > 0 then do I = 1 to MAX1;
51 put @11 TEXT1(I);
52 if LINES < 4 then link FOOTING;
53 end;
/* Line 54. If there is a first text field, this statement creates a blank line
between it and the second text field. Remember, it's possible for the first
text field to be empty and the second text field to contain some data. */
54 if MAX1 > 0 then put;
/* Lines 55 through 58. These statements print the second text field. Again, only
the correct number f rows are used to print the field. */
55 if MAX2 > 0 then do I = 1 to MAX2;
56 put @11 TEXT2(I);
57 if LINES < 4 then link FOOTING;
58 end;
/* Lines 59 through 76. This code prints a title, column headings, and footnotes
on each page. */
59 if EOF then link FOOTING;
60 return;
61 HEADING:
62 put @31 'Title';
63 put @1 'Variable 1'
64 @33 'Variable 2'
65 @45 'Variable 3'
66 @65 'Variable 4'
67 /;
68 return;
69 FOOTING:
70 do while(LINES > 2);
71 put;
72 end;
73 put @21 'Footing Line 1';
74 put @21 'Footing Line 2';
75 return;
76 run;