-
Notifications
You must be signed in to change notification settings - Fork 5
/
A56516.sas
1757 lines (1341 loc) · 48.5 KB
/
A56516.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
/*-------------------------------------------------------------------*/
/* SAS(r) Macro Programming Made Easy */
/* by Michele Burlew */
/* Copyright(c) 1998 by SAS Institute Inc., Cary, NC, USA */
/* SAS Publications order # 56516 */
/* ISBN 1-58025-343-1 */
/*-------------------------------------------------------------------*/
/* */
/* This material is provided "as is" by SAS Institute Inc. There */
/* are no warranties, expressed or implied, as to merchantability or */
/* fitness for a particular purpose regarding the materials or code */
/* contained herein. The Institute is not responsible for errors */
/* in this material as it now exists or will exist, nor does the */
/* Institute provide technical support for it. */
/* */
/*-------------------------------------------------------------------*/
/* Questions or problem reports concerning this material may be */
/* addressed to the author: */
/* */
/* SAS Institute Inc. */
/* Books by Users */
/* Attn: Michele Burlew */
/* SAS Campus Drive */
/* Cary, NC 27513 */
/* */
/* */
/* If you prefer, you can send email to: sasbbu@sas.com */
/* Use this for subject field: Comments for Michele Burlew */
/* */
/*-------------------------------------------------------------------*/
/**********************************************************************/
/* Following are programs from SAS Macro Programming Made Easy */
/**********************************************************************/
/**********************************************************************/
/* Create the data set used throughout the book. */
/**********************************************************************/
data books.ytdsales;
keep section--salepric;
attrib section length=$26 label='Section'
saleid length=8 label='Sale ID'
format=8.
saleinit length=$3 label='Sales Person Initials'
datesold length=4 label='Date Book Sold'
format=mmddyy8. informat=mmddyy8.
title length=$50 label='Title of Book'
author length=$50 label='First Author'
publishr length=$50 label='Publisher'
cost length=8 label='Wholesale Cost'
format=dollar9.2
listpric length=8 label='List Price'
format=dollar9.2
salepric length=8 label='Sale Price'
format=dollar9.2;
array jan{5} jan1-jan5 (105,40,110,60,85);
array feb{5} feb1-feb5 (120,40,130,45,150);
array mar{5} mar1-mar5 (115,60,175,75,145);
array apr{5} apr1-apr5 (145,55,132,60,131);
array may{5} may1-may5 (190,60,165,90,135);
array jun{5} jun1-jun5 (160,56,168,84,143);
array jul{5} jul1-jul5 (138,50,149,72,140);
array aug{5} aug1-aug5 (139,40,153,68,142);
array sep{5} sep1-sep5 (150,58,159,80,150);
array oct{5} oct1-oct5 (157,52,163,83,155);
array nov{5} nov1-nov5 (168,63,173,88,170);
array dec{5} dec1-dec5 (190,75,200,95,210);
array mos{60} jan1--dec5;
array momax{12} momax1-momax12
(30,27,30,29,30,29,30,30,29,30,29,30);
array sname{5} $ 26 ('Internet' 'Networks and Communication'
'Operating Systems'
'Programming Languages' 'Web Design');
array prices{13} p1-p13
(15,18,19,22,24,28,29,32,35,39,42,41,76);
do m=1 to 12;
do i=1 to 5;
section=sname{i};
do j=1 to mos{(m-1)*5+i};
day=round(momax{m}*uniform(3),1)+1;
datesold=mdy(m,day,1998);
title=trim(sname{i}) || ' Title ' || put(j,3.);
pval=round(2*normal(3),1) + 7;
if pval > 13 then pval=13;
else if pval < 1 then pval=1;
listpric=prices{pval} + .95;
salepric=listpric;
if mod(j,8)=0 then salepric=listpric*.9;
if mod(j,17)=0 and mod(j,8) ne 0
then salepric=listpric*.8;
cost=.7*listpric;
if mod(j,12)=0 then cost=.8*listpric;
person=mod(day,3);
if mod(day,12)=0 then person=1;
if mod(day,20)=0 then person=2;
if mod(day,15)=0 then person=0;
if person=0 then saleinit='MJM';
else if person=1 then saleinit='BLT';
else if person=2 then saleinit='JMB';
output;
end;
end;
end;
run;
/**********************************************************************/
/* CHAPTER 1 EXAMPLES */
/**********************************************************************/
/**********************************************************************/
/* Page 5: Example for output in Figure 1.2 */
/**********************************************************************/
%let repmonth=4;
%let repyear=1998;
%let repmword=%sysfunc(mdy(&repmonth,1,&repyear),monname9.);
data month&repmonth;
set books.ytdsales;
mosale=month(datesold);
label mosale='Month of Sale';
run;
proc tabulate data=month&repmonth;
title "Sales During &repmword &repyear";
where mosale=&repmonth and year(datesold)=&repyear;
class section;
var salepric listpric cost;
tables section all='**TOTAL**',
(salepric listpric cost)*(n*f=4. sum*f=dollar9.2);
run;
proc gchart data=month&repmonth
(where=(mosale < %eval(&repmonth+1) and
year(datesold)=&repyear));
title "Sales Through &repmword &repyear";
pie section / coutline=black percent=arrow
sumvar=salepric noheading ;
run;
quit;
/**********************************************************************/
/* Page 11: Example for output in Figure 1.4 */
/**********************************************************************/
title "Sales Report";
title2 "As of &systime &sysday &sysdate";
title3 "Using SAS Version: &sysver";
proc means data=books.ytdsales n sum;
var salepric;
run;
/**********************************************************************/
/* Page 12: Example for output in Figure 1.5 */
/**********************************************************************/
%macro daily;
proc means data=books.ytdsales(where=(datesold=today()))
maxdec=2 sum;
title "Daily Sales Report for &sysdate";
class section;
var salepric;
run;
%if &sysday=Friday %then %do;
proc means data=books.ytdsales
(where=(today()-6 le datesold le today()))
sum maxdec=2;
title "Weekly Sales Report Week Ending &sysdate";
class section;
var salepric;
run;
%end;
%mend daily;
%daily
/**********************************************************************/
/* Page 14: Example of iterative processing */
/**********************************************************************/
%macro makesets;
data
%do i=1 %to 12;
month&i
%end;
;
set books.ytdsales;
mosale=month(datesold);
if mosale=1 then output month1;
%do i=2 %to 12;
else if mosale=&i then output month&i;
%end;
run;
%mend makesets;
%makesets
/**********************************************************************/
/* Page 15: Example for output in Figure 1.6 */
/**********************************************************************/
data temp;
set books.ytdsales end=lastobs;
retain sumintwb 0;
if section in ('Internet','Web Design') then
sumintwb=sumintwb + salepric;
if lastobs then
call symput('INTWEBSL',put(sumintwb,dollar10.2));
run;
proc gchart data=temp;
title "Internet and Web Design Sales: &intwebsl";
title2 "As of &enddate";
hbar section / sumvar=salepric;
format salepric dollar10.2;
run;
quit;
/**********************************************************************/
/* Page 16: Example for output in Figure 1.7 */
/**********************************************************************/
%macro dsreport(dsname);
title "Report on Data Set &dsname";
%let dsid=%sysfunc(open(&dsname));
%*----How many obs are in the data set?;
%let nobs=%sysfunc(attrn(&dsid,nobs));
%*----When was the data set created?;
%let when = %sysfunc(putn(
%sysfunc(attrn(&dsid,crdte)),datetime9.));
title2 "Num Obs: &nobs Date Created: &when";
proc means data=&dsname sum maxdec=2;
class section;
var salepric;
run;
%mend dsreport;
%dsreport(books.ytdsales)
/**********************************************************************/
/* CHAPTER 3 EXAMPLES */
/**********************************************************************/
/**********************************************************************/
/* Page 52: Example for output in Figure 3.2 */
/**********************************************************************/
data web;
set books.ytdsales;
if section='Web Design' and datesold > "&sysdate"d-6;
run;
proc print data=web;
title "Web Design Titles Sold in the Past Week";
title2 "Report Date: &sysday &sysdate &systime";
footnote1 "Data Set Used: &syslast SAS Version: &sysver";
var title datesold salepric;
run;
/**********************************************************************/
/* Page 53: Example of using the %LET statement */
/**********************************************************************/
%let nocalc=53*21 + 100.1;
%let value1=982;
%let value2=813;
%let result=&value1 + &value2;
%let reptext=This report is for *** Department XYZ ***;
%let region=Region 3;
%let text=Sales Report;
%let moretext="Sales Report";
%let reptitle=&text ®ion;
%let reptitl2=&moretext ®ion;
%let sentence= This one started with leading blanks.;
%let chars=Symbols: !@#$%^&*;
%let novalue=;
%let holdvars=varnames;
%let &holdvars=title author datesold;
/**********************************************************************/
/* Page 56: Example of placing text before a macro variable reference*/
/**********************************************************************/
%let mosold=4;
%let level=25;
data book&mosold&level;
set books.ytdsales(where=(month(datesold)=&mosold));
attrib over&level length=$3 label="Cost > $&level";
if cost > &level then over&level='YES';
else over&level='NO';
run;
proc freq data=book&mosold&level;
title "Frequency Count of Books Sold During Month &mosold";
title2 "Grouped by Cost Over $&level";
tables over&level;
run;
/**********************************************************************/
/* Page 61: Example of resolving two ampersands */
/**********************************************************************/
%let section1=Internet;
%let section2=Networking and Communication;
%let section3=Operating Systems;
%let section4=Programming Languages;
%let section5=Web Design;
*----Look for section number defined by macro var n;
%let n=3;
proc means data=books.ytdsales;
title "Sales for Section: &§ion&n";
where section="&§ion&n";
var salepric;
run;
/**********************************************************************/
/* Page 64: Example of resolving multiple ampersands */
/**********************************************************************/
options symbolgen;
%let section1=Internet;
%let section2=Networking and Communication;
%let section3=Operating Systems;
%let section4=Programming Languages;
%let section5=Web Design;
%let dept1=Computer;
%let dept2=Reference;
%let dept3=Science;
%let n=3;
%let wherevar=section;
proc means data=books.ytdsales;
title "Sales for &wherevar: &&&wherevar&n";
where &wherevar="&&&wherevar&n";
var salepric;
run;
/**********************************************************************/
/* CHAPTER 4 EXAMPLES */
/**********************************************************************/
/**********************************************************************/
/* Page 72: Example for output in Figure 4.3 */
/**********************************************************************/
%macro sales;
title "Sales Report for Week Ending &sysdate";
proc gchart data=temp;
where today()-6 <= datesold <= today();
hbar section / sumvar=profit type=sum;
run;
%mend sales;
data temp;
set books.ytdsales;
attrib profit label='Sale Price-Cost' format=dollar8.2;
profit=salepric-cost;
run;
%sales
/**********************************************************************/
/* Page 76: Example of defining a macro program with positional */
/* parameters */
/**********************************************************************/
options mprint mlogic;
%macro listparm(start,stop,opts);
title "Books Sold by Section Between &start and &stop";
proc means data=books.ytdsales &opts;
where "&start"d le datesold le "&stop"d;
class section;
var salepric;
run;
%mend listparm;
*----First call to LISTPARM, all 3 parameters specified;
%listparm(01JUN1998,15JUN1998,n sum)
*----Second call to LISTPARM, first 2 parameters specifed and;
*----third parameter is null;
%listparm(01SEP1998,15SEP1998,)
/**********************************************************************/
/*Page 79: Example of defining a macro program with keyword parameters*/
/**********************************************************************/
options mprint mlogic;
%macro keyparm(start=01JAN1998,stop=31DEC1998,
opts=N SUM MIN MAX);
title "Books Sold by Section Between &start and &stop";
proc means data=books.ytdsales &opts;
where "&start"d le datesold le "&stop"d;
class section;
var salepric;
run;
%mend keyparm;
*----First call to KEYPARM: specify all keyword parameters;
%keyparm(start=01JUN1998,stop=15JUN1998,opts=n sum)
*----Second call to KEYPARM: specify start and stop,;
*----opts is null: should see default stats for PROC MEANS;
%keyparm(start=01SEP1998,stop=15SEP1998,opts=)
*----Third call to KEYPARM: use defaults for start and stop,;
*----specify opts;
%keyparm(opts=n sum)
/**********************************************************************/
/* Page 82: Example of defining a macro program with positional */
/* parameters and keyword parameters. */
/**********************************************************************/
options mprint mlogic;
%macro mixdparm(stats,othropts,start=01JAN1998,stop=31DEC1998);
title "Books Sold by Section Between &start and &stop";
proc means data=books.ytdsales &stats &othropts;
where "&start"d le datesold le "&stop"d;
class section;
var salepric;
run;
%mend mixdparm;
%mixdparm(,missing,start=01DEC1998)
/**********************************************************************/
/* CHAPTER 5 EXAMPLES */
/**********************************************************************/
/**********************************************************************/
/* Page 87: Example for output in Figure 5.2 */
/**********************************************************************/
options symbolgen mprint;
%let subset=Internet;
%macro makeds;
data temp;
set books.ytdsales(where=(section="&subset"));
attrib mosold label='Month of Sale';
mosold=month(datesold);
run;
%mend makeds;
%makeds
proc tabulate data=temp;
title "Book Sales as of &sysdate";
class mosold;
var salepric listpric;
tables mosold all,
(salepric listpric)*(n*f=6. sum*f=dollar12.2) /
box="Section: &subset";
keylabel all='** Total **';
run;
/**********************************************************************/
/* Page 90: Example for output in Figure 5.4 */
/**********************************************************************/
options symbolgen mprint;
%macro makeds(subset);
data temp;
set books.ytdsales(where=(section="&subset"));
attrib mosold label='Month of Sale';
mosold=month(datesold);
run;
%mend makeds;
%makeds(Internet)
proc tabulate data=temp;
title "Book Sales as of &sysdate";
class mosold;
var salepric listpric;
tables mosold all,
(salepric listpric)*(n*f=6. sum*f=dollar12.2) /
box="Section: &subset";
keylabel all='** Total **';
run;
/**********************************************************************/
/* Page 94: Example corresponding to Figure 5.6 */
/**********************************************************************/
options symbolgen mprint;
%macro makeds(subset);
%global glsubset;
%let glsubset=⊂
data temp;
set books.ytdsales(where=(section="&subset"));
attrib mosold label='Month of Sale';
mosold=month(datesold);
run;
%mend makeds;
%makeds(Internet)
proc tabulate data=temp;
title "Book Sales as of &sysdate";
class mosold;
var salepric listpric;
tables mosold all,
(salepric listpric)*(n*f=6. sum*f=dollar12.2) /
box="Section: &glsubset";
keylabel all='** Total **';
run;
/**********************************************************************/
/* Page 100: Example for output in Figure 5.9 */
/**********************************************************************/
options symbolgen mprint;
%global subset;
%let subset=Internet;
%macro loclmvar;
%local subset;
%let subset=Web Design;
proc means data=books.ytdsales n sum maxdec=2;
title "Book Sales as of &sysdate";
title2 "Uses LOCAL SUBSET macro variable: &subset";
where section="&subset";
var salepric;
run;
%mend loclmvar;
%loclmvar
proc means data=books.ytdsales n sum maxdec=2;
title "Book Sales as of &sysdate";
title2 "Uses GLOBAL SUBSET macro variable: &subset";
where section="&subset";
var salepric;
run;
/**********************************************************************/
/* CHAPTER 6 EXAMPLES */
/**********************************************************************/
/**********************************************************************/
/* Page 122 Example of %SUBSTR function */
/**********************************************************************/
proc means data=book.ytdsales;
title "Sales for %substr(&sysdate,3,3) through &sysdate";
where "01%substr(&sysdate,3)"d le datesold le "&sysdate"d;
class section;
var salepric;
run;
/**********************************************************************/
/* Page 122 Example of %SCAN function */
/**********************************************************************/
%let months=January February March April May June;
%let repmonth=3;
proc print data=books.ytdsales;
title "Sales Report for %scan(&months,&repmonth)";
where month(datesold)=&repmonth;
var title author salepric;
run;
/**********************************************************************/
/* Page 123 Example of %UPCASE function */
/**********************************************************************/
%macro listtext(keytext);
%let keytext=%upcase(&keytext);
proc print data=books.ytdsales;
title "Book Titles Sold Containing Text String &keytext";
where index(upcase(title),"&keytext") > 0;
var title author salepric;
run;
%mend;
%listtext(web)
/**********************************************************************/
/* Page 127 Example of %SYSFUNC function */
/**********************************************************************/
title
"Sales for %sysfunc(date(),monname.) %sysfunc(date(),year.)";
/**********************************************************************/
/* Page 127 Example of %SYSFUNC function */
/**********************************************************************/
%macro getopt(whatopt);
%let optvalue=%sysfunc(getoption(&whatopt));
%put Option &whatopt = &optvalue;
%mend getopt;
%getopt(ps)
%getopt(ls)
%getopt(date)
%getopt(symbolgen)
%getopt(compress)
/**********************************************************************/
/* Page 128 Example for output in Figure 6.1 */
/**********************************************************************/
%let dsid=%sysfunc(open(books.ytdsales));
%let nobs=%sysfunc(attrn(&dsid,nobs));
%let rc=%sysfunc(close(&dsid));
proc means data=books.ytdsales sum maxdec=2;
title "Year-to-Date Sales Report: &sysdate";
title2 "Number of Books Sold: &nobs";
class section;
var salepric;
run;
/**********************************************************************/
/* Page 135 Example of using logical expressions */
/**********************************************************************/
%macro compares(value1,value2);
%put COMPARISON 1:;
%if &value1 ne &value2 %then
%put &value1 is not equal to &value2..;
%else %put &value1 equals &value2..;
%put COMPARISON 2:;
%if &value1 > &value2 %then
%put &value1 is greater than &value2..;
%else %if &value1 < &value2 %then
%put &value1 is less than &value2..;
%else %put &value1 equals &value2..;
%put COMPARISON 3:;
%let result=%eval(&value1 > &value2);
%if &result=1 %then
%put EVAL result of &value1 > &value2 is TRUE.;
%else %put EVAL result of &value1 > &value2 is FALSE.;
%put COMPARISON 4:;
%let result=%sysevalf(&value1 > &value2);
%if &result=1 %then
%put SYSEVALF result of &value1 > &value2 is TRUE.;
%else %put SYSEVALF result of &value1 > &value2 is FALSE.;
%mend compares;
*----First call to COMPARES;
%compares(3,4)
*----Second call to COMPARES;
%compares(3.0,3)
*----Third call to COMPARES;
%compares(X,x)
/**********************************************************************/
/* Page 127 Example of selecting SAS steps with macro */
/* language statements */
/**********************************************************************/
%macro reports(reptype,repmonth);
%let lblmonth=
%sysfunc(mdy(&repmonth,1,%substr(&sysdate,6,2)),monname.);
%*----Begin summary report section;
%if %upcase(&reptype)=SUMMARY %then %do;
%*----Do summary report for report month;
proc tabulate data=books.ytdsales;
title "Sales for &lblmonth";
where month(datesold)=&repmonth;
class section;
var listpric salepric;
tables section,
(listpric salepric)*(n*f=6. sum*f=dollar12.2);
run;
%*----If end of quarter, also do summary report for qtr;
%if &repmonth=3 or &repmonth=6 or &repmonth=9
or &repmonth=12 %then %do;
%let qtrstart=%eval(&repmonth-2);
%let strtmo=
%sysfunc(mdy(&qtrstart,1,%substr(&sysdate,6,2)),monname.);
proc tabulate data=books.ytdsales;
title "Sales for Quarter from &strtmo to &lblmonth";
where &strtmo le datesold le &repmonth;
class section;
var listpric salepric;
tables section,
(listpric salepric)*(n*f=6. sum*f=dollar12.2);
run;
%end;
%end;
%*----End summary report section;
%*----Begin detail report section;
%else %if %upcase(&reptype)=DETAIL %then %do;
%*----Do detail report for month;
proc print data=books.ytdsales;
where month(datesold)=&repmonth;
var title cost listpric salepric;
sum cost listpric salepric;
run;
%end;
%*----End detail report section;
%mend reports;
*----First call to REPORTS does a Summary report for September;
%reports(Summary,9)
*----Second call to REPORTS does a Detail report for October;
%reports(Detail,10)
/**********************************************************************/
/* Page 138 Example of selecting SAS statements with */
/* %IF-%THEN/%ELSE statements */
/**********************************************************************/
%macro sales(classvar);
title "Sales Year-to-Date";
proc tabulate data=books.ytdsales;
%*----When there is a classification variable, issue a;
%*----CLASS statement;
%if &classvar ne %then %do;
class &classvar;
%end;
var listpric salepric;
tables
%*----When there is a classification variable, add the;
%*----classification variable to the TABLES statement;
%if &classvar ne %then %do;
&classvar all,
%end;
(listpric salepric)*(n*f=5. sum*f=dollar12.2);
run;
%*----When there is a classification variable, call;
%*----PROC SORT;
%if &classvar ne %then %do;
proc sort data=books.ytdsales;
by &classvar;
run;
%end;
%*----When there is a classification variable, issue a;
%*----BY statement;
proc univariate data=books.ytdsales;
%if &classvar ne %then %do;
by &classvar;
%end;
var listpric salepric;
run;
%mend sales;
*----First call to SALES processes the data by section;
%sales(section)
*----Second call to SALES summarizes the data overall;
%sales()
/**********************************************************************/
/* Page 141 Example of iterative %DO */
/**********************************************************************/
%macro multrep(strtyear,stopyear);
%do yrvalue=&strtyear %to &stopyear;
title "Sales Report for &yrvalue";
proc means data=sales.year&yrvalue;
class section;
var cost listpric salepric;
run;
proc gchart data=sales.year&yrvalue;
hbar section / sumvar=salepric type=sum;
run;
quit;
%end;
%mend multrep;
*----Produce 3 sets of reports: one for 1997, one for 1998,
*----and one for 1999;
%multrep(1997,1999)
/**********************************************************************/
/* Page 142 Example of iterative %DO */
/**********************************************************************/
%macro multchrt(strtyear,stopyear);
data allyears;
set
%do yrvalue=&strtyear %to &stopyear;
sales.year&yrvalue
%end;
;
run;
%let yrstrng=;
%do yrvalue=&strtyear %to &stopyear;
%let yrstrng=&yrstrng &i;
%end;
proc gchart data=allyears;
title "Charts Analyze Data for: &yrstrng";
hbar section / sumvar=salepric type=sum;
run;
%mend multchrt;
*----Concatenate three data sets: one from 1997, one from 1998,
*----and one from 1999;
%multchrt(1997,1999)
/**********************************************************************/
/* Page 143 Example of %DO %UNTIL */
/**********************************************************************/
%macro mosales(months);
%let i=1;
%do %until (%scan(&months,&i) eq );
%let repmonth=%scan(&months,&i);
proc means data=books.ytdsales n sum;
%if &repmonth ne %then %do;
title "Sales during month &repmonth";
where month(datesold)=&repmonth;
%end;
%else %do;
title "Overall Sales";
%end;
class section;
var salepric;
run;
%let i=%eval(&i+1);
%end;
%mend;
*----First call to MOSALES: produce stats for March, May, and
*----October;
%mosales(3 5 10)
*----Second call to MOSALES: produce overall stats;
%mosales()
/**********************************************************************/
/* Page 145 Example of %DO % */
/**********************************************************************/
%macro staff(reps,repmonth);
%let i=1;
%do %while (%scan(&reps,&i) ne );
%let inits=%scan(&reps,&i);
proc means data=books.ytdsales n sum;
title "Sales for &inits during month &repmonth";
where saleinit="&inits" and month(datesold)=&repmonth;
class section;
var salepric;
run;
%let i=%eval(&i+1);
%end;
%mend;
%staff(MJM BLT JMB,5)
/**********************************************************************/
/* Page 146 Example of %GOTO */
/**********************************************************************/
%macro detail(dsname,listvars);
%let foundit=%sysfunc(exist(&dsname));
%if &foundit le 0 %then %goto nodata;
title "PROC PRINT of &dsname";
proc print data=&dsname;
var &listvars;
run;
%goto finished;
%nodata:
%put **** Data set &dsname not found. ****;
proc datasets library=books details;
run;
quit;
%finished:
%mend;
*----First call to DETAIL, data set exists;
%detail(books.ytdsales,saledate title salepric)
*----Second call to DETAIL, data set does not exist;
%detail(books.ydtsales,saledate title salepric)
/**********************************************************************/
/* CHAPTER 7 EXAMPLES */
/**********************************************************************/
/**********************************************************************/
/* Page 161 Example for output in Figure 7.1 */
/**********************************************************************/
%let webfctr=1.20;
%let intfctr=1.35;
data temp;
set books.ytdsales(where=(
section in ('Web Design', 'Internet'));
if section='Web Design' then costfctr=symget('webfctr');
else if section='Internet' then costfctr=symget('intfctr');
newprice=costfctr*cost;
run;
proc print data=temp;
title "Prices based on COSTFCTR";
var section cost costfctr newpric;
format newpric dollar8.2;
run;
/**********************************************************************/
/* Page 162 Example for output in Figure 7.2 */
/**********************************************************************/
%let internet=1.25;
%let networks=1.20;
%let operatin=1.30;
%let programm=1.28;
%let webdesig=1.33;
data temp;
set books.ytdsales;
costsect=substr(compress(section),1,8);
costfctr=symget(costsect);
newprice=cost*costfctr;
run;
proc print data=temp;
title "Prices based on COSTFCTR";
var section costsect cost costfctr newprice;
format newprice dollar8.2;
run;
/**********************************************************************/
/* Page 164 Example for output in Figure 7.3 */
/**********************************************************************/
%let adj1997=1.10;
%let adj1998=1.15;
%let adj1999=1.18;
%let adj2000=1.05;
proc means data=books.ytdsales sum noprint nway;
class section;
var salepric;
output out=sumsales sum=salepric;
run;
data temp;
set sumsales;
array adj{4} adj1997-adj2000;
array sale{4} sale1997-sale2000;
do year=1997 to 2000;
idx=year-1996;
adj{idx}=symget('adj' || put(year,4.));
sale{idx}=salepric*adj{idx};
end;
run;
proc print data=temp;
title 'Adjusted Sales';
var section salepric adj1997 sale1997 adj1998 sale1998
adj1999 sale1999 adj2000 sale2000;
format sale1997-sale2000 dollar10.2;
run;
/**********************************************************************/
/* Page 166 Example of executing CALL SYMPUT multiple times */
/**********************************************************************/
data books;
input title $ 1-40;
call symput('booktitl',trim(title));
cards;
Wonderful Web Pages
Easy Networks
Jiffy Java
Web Sites of the Rich and Famous
;
%put The value of macro variable BOOKTITL is &booktitl..;
/**********************************************************************/
/* Page 167 Example of creating several macro variables */
/* with CALL SYMPUT */
/**********************************************************************/
proc freq data=books.ytdsales noprint;
tables section / out=sectname;
run;
data _null_;
set sectname;
call symput('name' || put(_n_,1.),section);
call symput('n' || put(_n_,1.),count);
run;
%put _user_;
/**********************************************************************/
/* Page 171 Example of using CALL EXECUTE to conditionally */