-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
all.sas
1102 lines (990 loc) · 40.8 KB
/
all.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
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Purpose : This file contains all the macros in a single file - which means it can be 'included' in SAS with just 2 lines of code:
%* filename mc url "https://raw.githubusercontent.com/KatjaGlassConsulting/SMILE-SmartSASMacros/main/all.sas";
%* %INCLUDE mc;
%* Author : Katja Glass
%************************************************************************************************************************;
OPTIONS NOQUOTELENMAX;
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_attrc
%* Parameters : DATA - name of the SAS dataset
%* ATTRIB - SAS ATTRC keyword (e.g. TYPE, LIB, LABEL, SORTEDBY, ...)
%*
%* Purpose : Function-style macro to return a character attribute of a dataset. The following attributes are available:
%* CHARSET, COMPRESS, DATAREP, ENCODING, ENCRYPT, ENGINE, LABEL, LIB, MEM, MODE, MTYPE, SORTEDBY, SORTLVL,
%* SORTSEQ, TYPE
%*
%* ExampleProg: ../programs/test_smile_attrc.sas
%*
%* Author : Katja Glass
%* Creation : 2021-02-19
%* License : MIT
%*
%* Reference : Main programming parts are coming from attrc.sas macro from Roland Rashleigh-Berry who
%* has published his code under the unlicence license in his utility package
%* (http://www.datasavantconsulting.com/roland/Spectre/download.html)
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
%PUT library of dataset: %smile_attrc(sashelp.class, lib);
PROC SORT DATA=sashelp.class OUT=class; BY sex name; RUN;
%PUT class is sorted by: %smile_attrc(class, SORTEDBY);
%PUT sashelp.class is sorted by: %smile_attrc(sashelp.class, SORTEDBY);
*/
%************************************************************************************************************************;
%MACRO smile_attrc(data, attrib) / MINOPERATOR MINDELIMITER=',';
%LOCAL dsid rc macro;
%LET macro = &sysmacroname;
%* check: ATTRIB must contain valid options;
%IF NOT (%UPCASE(&attrib) IN (CHARSET,COMPRESS,DATAREP,ENCODING,ENCRYPT,ENGINE,LABEL,LIB,MEM,MODE,MTYPE,
SORTEDBY,SORTLVL,SORTSEQ,TYPE))
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - Invalid value for ATTRIB (&attrib).;
-1
%RETURN;
%END;
%* perform action and put value for processing;
%LET dsid=%SYSFUNC(OPEN(&data,is));
%IF &dsid EQ 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DATA (&data) does not exist.;
-1
%END;
%ELSE %DO;
%SYSFUNC(attrc(&dsid,&attrib))
%LET rc=%SYSFUNC(CLOSE(&dsid));
%END;
%MEND smile_attrc;
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_attrn
%* Parameters : DATA - name of the SAS dataset
%* ATTRIB - SAS ATTRN keyword (e.g. NOBS, CRDTE, ...)
%*
%* Purpose : Function-style macro to return a numeric attribute of a dataset. The following attributes are available:
%* ALTERPW, ANOBS, ANY, ARAND, ARWU, AUDIT, AUDIT_DATA, AUDIT_BEFORE, AUDIT_ERROR, CRDTE, ICONST, INDEX,
%* ISINDEX, ISSUBSET, LRECL, LRID, MAXGEN, MAXRC, MODTE, NDEL, NEXTGEN, NLOBS, NLOBSF, NOBS, NVARS, PW, RADIX,
%* READPW, REUSE, TAPE, WHSTMT, WRITEPW
%*
%* ExampleProg: ../programs/test_smile_attrn.sas
%*
%* Author : Katja Glass
%* Creation : 2021-02-19
%* License : MIT
%*
%* Reference : Main programming parts are coming from attrn.sas macro from Roland Rashleigh-Berry who
%* has published his code under the unlicence license in his utility package
%* (http://www.datasavantconsulting.com/roland/Spectre/download.html)
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
%PUT Number of observations: %smile_attrn(sashelp.class, nobs);
%IF %smile_attrn(sashelp.class, nvars) > 0 %THEN %PUT Dataset has variables;
*/
%************************************************************************************************************************;
%MACRO smile_attrn(data, attrib) / MINOPERATOR MINDELIMITER=',';
%LOCAL dsid rc macro;
%LET macro = &sysmacroname;
%* check: ATTRIB must contain valid options;
%IF NOT (%UPCASE(&attrib) IN (ALTERPW,ANOBS,ANY,ARAND,ARWU,AUDIT,AUDIT_DATA,AUDIT_BEFORE,AUDIT_ERROR,CRDTE,ICONST,INDEX,
ISINDEX,ISSUBSET,LRECL,LRID,MAXGEN,MAXRC,MODTE,NDEL,NEXTGEN,NLOBS,NLOBSF,NOBS,NVARS,PW,RADIX,
READPW,REUSE,TAPE,WHSTMT,WRITEPW))
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - Invalid value for ATTRIB (&attrib).;
-1
%RETURN;
%END;
%* perform action and put value for processing;
%LET dsid=%SYSFUNC(OPEN(&data,is));
%IF &dsid EQ 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DATA (&data) does not exist.;
-1
%END;
%ELSE %DO;
%SYSFUNC(attrn(&dsid,&attrib))
%LET rc=%SYSFUNC(CLOSE(&dsid));
%END;
%MEND smile_attrn;
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_attr_var
%* Parameters : DATA - name of the SAS dataset
%* VAR - name of variable
%* ATTRIB - SAS variable attrib keyword (e.g. VARTYPE, VARLEN, VARLABEL, VARFMT and VARINFMT)
%*
%* Purpose : Function-style macro to return a variable attribute of a dataset. The following attributes are available:
%* VARTYPE, VARLEN, VARLABEL, VARFMT and VARINFMT
%*
%* ExampleProg: ../programs/test_smile_attr_var.sas
%*
%* Author : Katja Glass
%* Creation : 2021-02-19
%* License : MIT
%*
%* Reference : Main programming parts are coming from attrv.sas macro from Roland Rashleigh-Berry who
%* has published his code under the unlicence license in his utility package
%* (http://www.datasavantconsulting.com/roland/Spectre/download.html)
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
%PUT VARTYPE for name: %smile_attr_var(sashelp.class, name, vartype);
%PUT VARTYPE for age: %smile_attr_var(sashelp.class, age, vartype);
%PUT VARLABEL for name: %smile_attr_var(sashelp.class, name, varlabel);
%PUT VARLEN for name: %smile_attr_var(sashelp.class, name, varlen);
*/
%************************************************************************************************************************;
%MACRO smile_attr_var(data, var, attrib);
%LOCAL dsid rc macro varnum;
%LET macro = &sysmacroname;
%* check: ATTRIB must contain valid options;
%IF %UPCASE(&attrib) NE VARTYPE AND
%UPCASE(&attrib) NE VARLEN AND
%UPCASE(&attrib) NE VARLABEL AND
%UPCASE(&attrib) NE VARFMT AND
%UPCASE(&attrib) NE VARINFMT
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - Invalid value for ATTRIB (&attrib) - only the following are supported:;
%PUT ¯o - VARTYPE, VARLEN, VARLABEL, VARFMT and VARINFMT;
-1
%RETURN;
%END;
%* perform action and put value for processing;
%LET dsid=%SYSFUNC(OPEN(&data,is));
%IF &dsid EQ 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DATA (&data) does not exist.;
-1
%END;
%ELSE %DO;
%LET varnum = %SYSFUNC(VARNUM(&dsid,&var));
%IF &varnum LT 1
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - Variable VAR (&var) does not exist in DATA (&data).;
-1
%RETURN;
%END;
%SYSFUNC(&attrib(&dsid,&varnum))
%LET rc=%SYSFUNC(CLOSE(&dsid));
%END;
%MEND smile_attr_var;
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_ods_document_flat_label
%* Parameters : DOCUMENT - ODS Document item store
%* LABEL - One label to apply on first element, all other labels are removed (optional),
%* if not provided, labels are just rearranged and additional BY-labels removed
%* BOOKMARKLABEL - Indicator whether to use Bookmark Labels if none is specified (YES Default),
%* if LABEL is missing and BOOKMARKLABEL = NO, all labels are removed
%*
%* Purpose : Flat navigation and optionally re-label navigation for ODS DOCUMENT. The navigation bookmark level is
%* reduced to one level only. Optionally a label can be applied to all content items or the navigation label
%* can be removed completely.
%* Comment : The navigation in PDF documents can be one level only with this macro. CONTENTS="" must be applied to
%* the PROC REPORT as option and additionally for a BREAK BEFORE PAGE option.
%* Issues : The table of contents created per default by SAS (ODS PDF option TOC) is not linking the pages correctly when
%* using BY groups and having one ODS DOCUMENT with multiple outputs, using single ODS DOCUMENTS (one per each output)
%* then this is working correctly. In such a case, do use not a TOC or create an own TOC, e.g. like described here:
%* https://www.mwsug.org/proceedings/2012/S1/MWSUG-2012-S125.pdf
%*
%* ExampleProg: ../programs/test_smile_ods_document_flat_label.sas
%*
%* Author : Katja Glass
%* Creation : 2021-02-15
%* License : MIT
%*
%* Reference : A nice overview of ODS DOCUMENT can be found here: https://support.sas.com/resources/papers/proceedings12/273-2012.pdf
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
%*This will flatten the navigation and use the labels originally set with ODS PROCLABEL;
%smile_ods_document_flat_label(document=doc_reports);
%*This will flatten the navigation and use the label "Table 1.1.1" applied to all items of this store;
%smile_ods_document_flat_label(document=doc_report1, label=Table 1.1.1);
%*This will flatten the navigation and use no navigation label at all (no navigation link at all for these items);
%smile_ods_document_flat_label(document=doc_report1, label=, bookmarklabel = NO);
*/
%************************************************************************************************************************;
%MACRO smile_ods_document_flat_label(document=, label=, bookmarkLabel = yes);
%LOCAL macro _temp;
%LET macro = &sysmacroname;
%LET _temp = -1;
%*;
%* Error handling I;
%*;
%* check: DOCUMENT must be specified;
%IF %LENGTH(&document) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DOCUMENT parameter is required. Macro will abort;
%RETURN;
%END;
%* check: BOOKMARKLABEL must be YES or NO;
%IF %UPCASE(&bookmarkLabel) NE YES AND %UPCASE(&bookmarkLabel) NE NO
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - BOOKMARKLABEL parameter must either be NO or YES. Macro will abort;
%RETURN;
%END;
%* check - DOCUMENT must be an existing item store in WORK;
PROC SQL NOPRINT;
SELECT 1 INTO :_temp FROM SASHELP.VMEMBER
WHERE libname="WORK" AND memname="%UPCASE(&document)" AND memtype = "ITEMSTOR";
RUN;QUIT;
%IF &_temp NE 1
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DOCUMENT (&document) is no existing ODS DOCUMENT. Macro will abort;
%RETURN;
%END;
%*;
%* Read information;
%*;
ODS SELECT NONE;
PROC DOCUMENT NAME = &document;
LIST / DETAILS LEVELS=all;
ODS OUTPUT PROPERTIES = _props;
RUN;QUIT;
ODS SELECT ALL;
%*;
%* Error handling II;
%*;
%* check: DOCUMENT must contain content;
%LET _temp = -1;
PROC SQL NOPRINT;
SELECT nobs INTO :_temp FROM SASHELP.VTABLE WHERE libname="WORK" AND memname="_PROPS";
RUN;QUIT;
%IF &_temp = 0
%THEN %DO;
%PUT %STR(WAR)NING: ¯o - DOCUMENT (&document) does not contain any observations - no action done;
%RETURN;
%END;
%*;
%* Processing;
%*;
%* create a generic processing for PROC DOCUMENT;
%* Step 1: move all reports to /all and apply label;
DATA _NULL_;
SET _props END=_eof;
ATTRIB coreLabel FORMAT=$200.;
RETAIN coreLabel;
RETAIN _count 1 _first 0 _core 0;
IF _N_ = 1
THEN DO;
CALL EXECUTE("PROC DOCUMENT NAME=&document;");
END;
IF COUNT(path,"\") = 1 AND type = "Dir"
THEN DO;
_core = 1;
%IF %UPCASE(&bookmarkLabel) NE YES
%THEN %DO;
CALL MISSING(coreLabel);
PUT "updateOdsDocument - No label is used";
%END;
%ELSE %IF %LENGTH("&label") > 2
%THEN %DO;
coreLabel = "&label";
%END;
%ELSE %DO;
coreLabel = label;
_first = 0;
%END;
END;
IF type NE "Dir" AND _core
THEN DO;
CALL EXECUTE('MOVE ' || STRIP(path) || ' to \all;');
IF _first = 0
THEN CALL EXECUTE('SETLABEL \all#' || STRIP(PUT(_count,BEST.)) || " '" || STRIP(coreLabel) || "';");
_first = 1;
_count = _count + 1;
END;
IF _eof
THEN DO;
CALL EXECUTE('RUN;QUIT;');
END;
RUN;
%* Step 2: remove all old hierarchies;
DATA _NULL_;
SET _props END=_eof;
IF _N_ = 1
THEN CALL EXECUTE("PROC DOCUMENT NAME=&document;");
IF COUNT(path,"\") = 1 AND type = "Dir"
THEN CALL EXECUTE('DELETE ' || STRIP(path) || ";");
IF _eof
THEN CALL EXECUTE('RUN;QUIT;');
RUN;
%*;
%* Cleanup;
%*;
PROC DATASETS LIB=WORK;
DELETE _props;
RUN;
%MEND smile_ods_document_flat_label;
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_pdf_merge
%* Parameters : DATA - Input dataset containing INFILE and BOOKMARK variable,
%* INFILE containing single pdf files (one file per observation),
%* BOOKMARK containing the corresponding bookmark label for this file
%* OUTFILE - Output PDF file (not in quotes)
%* PDFBOX_JAR_PATH - Path and jar file name for PDFBOX open source tool, e.g. &path/pdfbox-app-2.0.22.jar
%* SOURCEFILE - Optional SAS program file where PROC GROOVY code is stored, default is TEMP (only temporary)
%* RUN_GROOVY - NO/YES indicator whether to run the final GROOVY code (default YES)
%*
%* Purpose : Merge multiple PDF files and create one bookmark entry per PDF file with PROC GROOVY and open-source Tool PDFBox
%* Comment : Make sure to download PDFBOX, e.g. from here https://pdfbox.apache.org/download.html - the full "app" version
%* Issues : "unable to resolve class" messages mean the PDFBOX is not provided correctly.
%* "ERROR: PROCEDURE GROOVY cannot be used when SAS is in the lock down state." means that your SAS environment
%* does not support PROC GROOVY, for this the macro cannot run the groovy code.
%* "WARNUNG: Removed /IDTree from /Names dictionary, doesn't belong there" - this message is coming from PDFBox.
%*
%* ExampleProg: ../programs/test_smile_pdf_merge.sas
%*
%* Author : Katja Glass
%* Creation : 2021-01-29
%* License : MIT
%*
%* Reference : A paper explaining how to use PDFBOX with PROC GROOVY also for TOC is available in the following paper
%* (https://www.lexjansen.com/phuse/2019/ct/CT05.pdf)
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
DATA content;
ATTRIB inFile FORMAT=$255.;
ATTRIB bookmark FORMAT=$255.;
inFile = "&inPath/output_1.pdf"; bookmark = "Table 1"; OUTPUT;
inFile = "&inPath/output_2.pdf"; bookmark = "Table 2"; OUTPUT;
inFile = "&inPath/output_3.pdf"; bookmark = "Table 3"; OUTPUT;
RUN;
%smile_pdf_merge(
data = content
, outfile = &outPath/merged_output.pdf
, pdfbox_jar_path = &libPath/pdfbox-app-2.0.22.jar
, sourcefile = &progPath/groovy_call.sas
, run_groovy = YES
);
*/
%************************************************************************************************************************;
%MACRO smile_pdf_merge(data = , outfile = , pdfbox_jar_path = , sourcefile = TEMP, run_groovy = YES);
%LOCAL macro;
%LET macro = &sysmacroname;
%*;
%* Error handling I - parameter checks;
%*;
%* check: existence of required parameters (DATA, OUTFILE, PDFBOX_JAR_PATH), abort;
%* check: existence of parameter SOURCEFILE, if not use TEMP;
%* check: RUN_GROOVY must be NO or YES, abort;
%IF %LENGTH(&data) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DATA parameter is requried. Macro will abort.;
%RETURN;
%END;
%IF %LENGTH(&outfile) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - OUTFILE parameter is requried. Macro will abort.;
%RETURN;
%END;
%IF %LENGTH(&pdfbox_jar_path) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - PDFBOX_JAR_PATH parameter is requried. Macro will abort.;
%RETURN;
%END;
%IF %LENGTH(&sourcefile) = 0
%THEN %DO;
%PUT %STR(WAR)NING: ¯o - SOURCEFILE parameter is needed - TEMP will be used.;
%LET sourcefile = TEMP;
%END;
%IF %UPCASE(&run_groovy) NE YES AND %UPCASE(&run_groovy) NE NO
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - RUN_GROOVY parameter must be NO or YES. Macro will abort.;
%RETURN;
%END;
%* check: PDFBOX_JAR_PATH must exist and must be a ".jar" file;
%IF %SYSFUNC(FILEEXIST(&pdfbox_jar_path)) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - PDFBOX_JAR_PATH file does not exist. Macro will abort.;
%RETURN;
%END;
%IF %UPCASE(%SCAN(&pdfbox_jar_path,-1,.)) NE JAR
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - PDFBOX_JAR_PATH must be a ".jar" file. Macro will abort.;
%RETURN;
%END;
%*;
%* Preparations;
%*;
%* include quotes around sourcefile if not available;
DATA _NULL_;
ATTRIB path FORMAT=$500.;
path = SYMGET('sourcefile');
IF UPCASE(STRIP(path)) NE "TEMP"
THEN DO;
IF SUBSTR(sourcefile,1,1) NE '"' AND SUBSTR(sourcefile,1,1) NE "'"
THEN DO;
CALL SYMPUT('sourcefile','"' || STRIP(path) || '"');
END;
END;
RUN;
%*;
%* Error handling II - data checks;
%*;
%LOCAL dsid rc error;
%LET error = 0;
%* check: existence of data, contains observations, contains variables infile and bookmark;
%LET dsid=%SYSFUNC(OPEN(&data,is));
%IF &dsid EQ 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DATA (&data) does not exist. Macro will abort.;
%RETURN;
%END;
%ELSE %IF %SYSFUNC(ATTRN(&dsid,NLOBS)) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DATA (&data) does not contain any observations. Macro will abort.;
%LET rc=%SYSFUNC(CLOSE(&dsid));
%RETURN;
%END;
%ELSE %IF %SYSFUNC(VARNUM(&dsid,infile)) = 0 OR %SYSFUNC(VARNUM(&dsid,bookmark)) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - DATA (&data) does not contain required variables (infile and bookmark). Macro will abort.;
%LET rc=%SYSFUNC(CLOSE(&dsid));
%RETURN;
%END;
%LET rc=%SYSFUNC(CLOSE(&dsid));
%* check: files in variable "infile" must exist;
%* update BOOKMARK labels, replace double quotes;
DATA _smile_indat;
SET &data;
RETAIN _smile_msg 0;
IF FILEEXIST(infile) = 0
THEN DO;
PUT "%STR(ERR)OR: INFILE does not exist: " infile " - Macro will abort.";
CALL SYMPUT('error','1');
END;
IF INDEX(bookmark,'"') > 0 AND _smile_msg = 0
THEN DO;
PUT "%STR(WAR)NING: Double quotes are not supported for BOOKMARK texts and are removed.";
_smile_msg = 1;
END;
bookmark = TRANWRD(bookmark,'"','');
RUN;
%IF &error NE 0
%THEN %DO;
%GOTO end_macro;
%END;
%*;
%* Create PROC GROOVY program file;
%*;
FILENAME cmd &sourcefile;
DATA _NULL_;
FILE cmd LRECL=5000;
SET _smile_indat END=_eof;
IF _N_ = 1
THEN DO;
PUT "PROC GROOVY;";
PUT " ADD CLASSPATH = ""&pdfbox_jar_path"";";
PUT " SUBMIT;";
PUT ;
PUT "import org.apache.pdfbox.multipdf.PDFMergerUtility;";
PUT "import org.apache.pdfbox.pdmodel.PDDocument;";
PUT "import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;";
PUT "import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;";
PUT "import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;";
PUT "import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;";
PUT "import java.io.File;";
PUT ;
PUT "public class PDFMerge2 {";
PUT " public static void main(String[] args) {";
PUT;
PUT @8 "//Instantiating PDFMergerUtility class";
PUT @8 "PDFMergerUtility PDFmerger = new PDFMergerUtility();";
PUT ;
PUT @8 "//Setting the destination file";
PUT @8 "PDFmerger.setDestinationFileName(""&outfile"");";
PUT ;
PUT @8 "//adding the source files";
END;
PUT @8 "PDFmerger.addSource(new File(""" inFile +(-1) """));";
IF _eof
THEN DO;
PUT @8 "PDFmerger.mergeDocuments(null);";
END;
RUN;
DATA _NULL_;
FILE cmd LRECL=5000 MOD;
ATTRIB _temp FORMAT=$200.;
SET _smile_indat END=_eof;
IF _N_ = 1
THEN DO;
PUT @8 "//Open created document";
PUT @8 "PDDocument document;";
PUT @8 "PDPageDestination pageDestination;";
PUT @8 "PDOutlineItem bookmark;";
PUT @8 "document = PDDocument.load(new File(""&outfile""));";
PUT ;
PUT @8 "//Create a bookmark outline";
PUT @8 "PDDocumentOutline documentOutline = new PDDocumentOutline();";
PUT @8 "document.getDocumentCatalog().setDocumentOutline(documentOutline);";
PUT @8 ;
PUT @8 "int currentPage = 0;";
END;
_temp = SCAN(inFile,-1,"/\");
PUT @8 "//Include file " _temp;
PUT @8 "pageDestination = new PDPageFitWidthDestination();";
PUT @8 "pageDestination.setPage(document.getPage(currentPage));";
PUT @8 "bookmark = new PDOutlineItem();";
PUT @8 "bookmark.setDestination(pageDestination);";
PUT @8 "bookmark.setTitle(""" bookmark +(-1) """);";
PUT @8 "documentOutline.addLast(bookmark);";
PUT ;
PUT @8 "//Change currentPage number";
PUT @8 "currentPage += PDDocument.load(new File(""" inFile +(-1) """)).getNumberOfPages();";
PUT ;
IF _eof
THEN DO;
PUT @8 "//save document";
PUT @8 "document.save(""&outfile"");";
PUT ;
PUT "}}";
PUT "endsubmit;";
PUT "quit;";
END;
RUN;
%*;
%* Optionally execute PROC GROOVY code;
%*;
%IF %UPCASE(&run_groovy) = YES
%THEN %DO;
%PUT ¯o: Run Groovy Program;
%PUT ¯o: The following warning might come from PDFBox: %STR(WAR)NING: Removed /IDTree from /Names dictionary ...;
%INCLUDE cmd;
%END;
%end_macro:
%*;
%* cleanup;
%*;
FILENAME cmd;
PROC DATASETS LIB=WORK NOWARN NOLIST;
DELETE _smile_indat;
RUN;
%MEND;
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_pdf_read_bookmarks
%* Parameters : PDFFILE - name of PDF file with bookmarks
%* OUTDAT - output dataset
%* PDFBOX_JAR - path and jar file name for PDFBOX open source tool, e.g. &path/pdfbox-app-2.0.22.jar
%*
%* Purpose : Read PDF Bookmarks into a SAS dataset with the variables level, title and page
%* Comment : Make sure to download PDFBOX, e.g. from here https://pdfbox.apache.org/download.html - the full "app" version
%* Issues : "unable to resolve class" messages mean the PDFBOX is not provided correctly.
%* "ERROR: PROCEDURE GROOVY cannot be used when SAS is in the lock down state." means that your SAS environment
%* does not support PROC GROOVY, for this the macro cannot run the groovy code.
%*
%* ExampleProg: ../programs/test_smile_pdf_read_bookmarks.sas
%*
%* Author : Katja Glass
%* Creation : 2021-02-13
%* License : MIT
%*
%* Reference : PDFBox contains a lot of useful functionalities (https://pdfbox.apache.org)
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
%smile_pdf_read_bookmarks(pdfFile = <path>/ods_document_flat1.pdf,
outdat = book_flat1,
pdfbox_jar_path = <path>/pdfbox-app-2.0.22.jar)
%smile_pdf_read_bookmarks(pdfFile = <path>/ods_document_noflat1.pdf,
outdat = book_noflat1,
pdfbox_jar_path = <path>/pdfbox-app-2.0.22.jar)
*/
%************************************************************************************************************************;
%MACRO smile_pdf_read_bookmarks(pdfFile = , outdat = , pdfbox_jar_path = );
%LOCAL jsonFile;
FILENAME jsonFile TEMP;
%LET jsonFile = %SYSFUNC(PATHNAME(jsonFile));
FILENAME _rdbkpd TEMP;
DATA _NULL_;
FILE _rdbkpd LRECL=5000;
PUT 'PROC GROOVY;';
PUT ' ADD CLASSPATH = "' "&pdfbox_jar_path" '";';
PUT ' SUBMIT;';
PUT ;
PUT ' import java.io.File;';
PUT ' import java.io.FileWriter;';
PUT ' import java.io.IOException;';
PUT ' import java.io.PrintWriter;';
PUT ' import java.util.ArrayList;';
PUT ;
PUT ' import org.apache.pdfbox.pdmodel.PDDocument;';
PUT ' import org.apache.pdfbox.pdmodel.PDPage;';
PUT ' import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;';
PUT ' import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;';
PUT ' import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;';
PUT ;
PUT ' public class PDFReadBookmarks {';
PUT ;
PUT ' public static void main(String[] args) throws IOException {';
PUT ' ArrayList<String> aBookmarks = new ArrayList<String>();';
PUT ;
PUT ' // Read the PDF document and investigate bookmarks into a list (JSON formatted)';
PUT ' PDDocument document = PDDocument.load(new File("' "&pdffile" '"));';
PUT ' PDDocumentOutline outline = document.getDocumentCatalog().getDocumentOutline();';
PUT ' addBookmark(aBookmarks, document, outline, 1);';
PUT ' document.close();';
PUT ;
PUT ' // Print bookmark information into a file';
PUT ' FileWriter fileWriter = new FileWriter("' "&jsonFile" '");';
PUT ' PrintWriter printWriter = new PrintWriter(fileWriter);';
PUT ' printWriter.print(aBookmarks);';
PUT ' printWriter.close();';
PUT ' }';
PUT ;
PUT ' static public void addBookmark(ArrayList<String> bookmarks, PDDocument document, PDOutlineNode bookmark, int level) throws IOException';
PUT ' {';
PUT ' PDOutlineItem current = bookmark.getFirstChild();';
PUT ' while (current != null)';
PUT ' {';
PUT ' PDPage currentPage = current.findDestinationPage(document);';
PUT ' Integer pageNumber = document.getDocumentCatalog().getPages().indexOf(currentPage) + 1;';
PUT ' String text = "\n{\"level\":" + level + ", \"title\":\"" + current.getTitle() + "\", \"page\":" + pageNumber + "}";';
PUT ' bookmarks.add(text);';
PUT ' addBookmark(bookmarks, document, current, level + 1);';
PUT ' current = current.getNextSibling();';
PUT ' }';
PUT ' }';
PUT ' }';
PUT 'ENDSUBMIT;';
PUT 'QUIT;';
RUN;
%INCLUDE _rdbkpd;
LIBNAME jsonCont JSON "&jsonFile";
DATA &outdat(DROP=ordinal_root);
SET jsonCont.root;
RUN;
%MEND smile_pdf_read_bookmarks;
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_replace_in_text_files
%* Parameters : INPATH - input directory (no quotes)
%* OUTPATH - output directory, if same as INPATH, files are overwritten (no quotes)
%* REPLACE_FROM - text which should be replaced (with quotes)
%* REPLACE_WITH - text that should newly be added (with quotes)
%* FILETYPE - extension of file types which should be processed, e.g. sas or txt (optional)
%*
%* Purpose : Replace text from all files contained in a folder with a different text
%*
%* ExampleProg: ../programs/test_smile_replace_in_text_files.sas
%*
%* Author : Katja Glass
%* Creation : 2021-03-05
%* License : MIT
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
%* replace the default root path in all example files;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/programs,
outpath = /home/u49641771/smile/programs,
replace_from = '%LET root = /folders/myshortcuts/git/SMILE-SmartSASMacros;',
replace_to = '%LET root = /home/u49641771/smile;');
%* replace all tabls with four spaces;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/programs,
outpath = /home/u49641771/smile/programs/blub,
replace_from = '09'x,
replace_to = ' ');
%* replace all zero numbers with x in the output;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/results,
outpath = /home/u49641771/smile/results/mod,
replace_from = '0',
replace_to = 'x',
filetype = lst);
*/
%************************************************************************************************************************;
%MACRO smile_replace_in_text_files(
inpath = ,
outpath = ,
replace_from = ,
replace_to = ,
filetype =
);
%LOCAL macro;
%LET macro = &sysmacroname;
%IF %SYSFUNC(FILEEXIST("&inpath")) < 1
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - INPATH folder does not exist: &inpath.;
%RETURN;
%END;
%IF %SYSFUNC(FILEEXIST("&outpath")) < 1
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - OUTPATH folder does not exist: &inpath.;
%RETURN;
%END;
%IF %LENGTH(&replace_from) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - REPLACE_FROM must be provided.;
%RETURN;
%END;
%IF %LENGTH(&replace_to) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - REPLACE_FROM must be provided.;
%RETURN;
%END;
%* read all files (ignore those without dot (folders));
FILENAME myPath "&inpath";
DATA _dir (KEEP=filename);
ATTRIB filename FORMAT=$200.;
list = DOPEN('myPath');
IF list > 0
THEN DO;
DO i = 1 to dnum(list);
filename = TRIM(DREAD(list,i));
%IF %LENGTH(&filetype) > 0
%THEN %DO;
IF INDEX(UPCASE(filename),"%UPCASE(.&filetype)") > 0
THEN OUTPUT;
%END;
%ELSE %DO;
IF INDEX(filename,".") > 0
THEN OUTPUT;
%END;
END;
END;
rc = CLOSE(list);
RUN;
FILENAME myPath;
%* re-create all files using a TRANWRD to perform replacement;
DATA _NULL_;
SET _dir;
ATTRIB cmd FORMAT=$1000.;
ATTRIB from FORMAT=$1000.;
ATTRIB to FORMAT=$1000.;
from = SYMGET('replace_from');
to = SYMGET('replace_to');
%* create three filenames (input, output, temporary in-between (needed if input=output));
CALL EXECUTE('FILENAME _rep_tmp TEMP;');
cmd = "FILENAME _rep_in '&inpath/" || STRIP(filename) || "';";
CALL EXECUTE(cmd);
cmd = "FILENAME _rep_out '&outpath/" || STRIP(filename) || "';";
CALL EXECUTE(cmd);
%* replace texts and create temporary file;
CALL EXECUTE('DATA _NULL_;');
CALL EXECUTE(' INFILE _rep_in LRECL=2000 END=_eof;');
CALL EXECUTE(' FILE _rep_tmp LRECL=2000;');
CALL EXECUTE(' ATTRIB line FORMAT=$2000.;');
CALL EXECUTE(' INPUT;');
CALL EXECUTE(' line = _INFILE_;');
cmd = "line = TRANWRD(line," || STRIP(from) || ", " || STRIP(to) || ');';
PUT cmd = ;
CALL EXECUTE(cmd);
CALL EXECUTE(' IF LENGTHN(line) = 0 THEN PUT;');
CALL EXECUTE(' ELSE DO;');
CALL EXECUTE(' pos = LENGTHN(line) - LENGTHN(STRIP(line)) + 1;');
CALL EXECUTE(' PUT @pos line;');
CALL EXECUTE(' END;');
CALL EXECUTE('RUN;');
%* store temporary file as final file;
CALL EXECUTE('DATA _NULL_;');
CALL EXECUTE(" rc=FCOPY('_rep_tmp', '_rep_out');");
CALL EXECUTE('RUN;');
RUN;
PROC DATASETS LIB=WORK NOLIST;
DELETE _dir;
RUN;
%MEND smile_replace_in_text_files;
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_url_check
%* Parameters : URL - http(s) URL which should be checked in quotes
%* RETURN - return code variable (scope should be global)
%* INFO - NO/YES indicator to print information to the log
%*
%* Purpose : Check existence of URL and store result in return code, information can optionally be printed to the log
%* Comment : Return codes are 0 - url found, 999 - no url provided,
%* 998 - url not provided in quotes, otherwise html-return code (e.g. 404 file not found)
%*
%* ExampleProg: ../programs/test_smile_url_check.sas
%*
%* Author : Katja Glass
%* Creation : 2021-02-19
%* License : MIT
%*
%* Reference : The idea from this macro is coming from a paper by Joseph Henry - The ABCs of PROC HTTP
%* (https://www.sas.com/content/dam/SAS/support/en/sas-global-forum-proceedings/2019/3232-2019.pdf)
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
OPTIONS NONOTES;
%GLOBAL rc;
%smile_url_check(url="https://github.com/phuse-org/phuse-scripts/blob/master/whitepapers/scriptathons/central/dummy.sas");
%PUT &rc;
%smile_url_check(url="https://github.com/phuse-org/phuse-scripts/blob/master/whitepapers/scriptathons/central/Box_Plot_Baseline.sas");
%PUT &rc;
*/
%************************************************************************************************************************;
%MACRO smile_url_check(url=, return=rc, info=NO);
%LOCAL macro issue;
%LET macro = &sysmacroname;
%LET issue = 0;
%* check: URL must be provided;
%IF %LENGTH(&url) = 0
%THEN %DO;
%PUT %STR(ERR)OR: ¯o - URL must be provided.;
%IF %LENGTH(&return) > 0
%THEN %DO;
%LET &return = 999;
%END;
%RETURN;
%END;
%* check: URL must be provided in quotes;
DATA _NULL_;
ATTRIB url FORMAT=$2000.;
url = SYMGET('url');
IF NOT (SUBSTR(url,1,1) IN ("'",'"') AND SUBSTR(url,LENGTH(url)) IN ("'",'"'))
THEN DO;
PUT "ERR" "OR: ¯o - URL must be provided in quotes.";
%IF %LENGTH(&return) > 0
%THEN %DO;
CALL SYMPUT("&return", "998");
%END;
CALL SYMPUT("issue", "1");
END;
RUN;
%IF &issue = 1
%THEN %RETURN;
%* perform URL check;
FILENAME out TEMP;
FILENAME hdrs TEMP;
PROC HTTP URL=&url
HEADEROUT=hdrs;
RUN;
DATA _NULL_;
INFILE hdrs SCANOVER TRUNCOVER;
INPUT @'HTTP/1.1' code 4. message $255.;
%IF %LENGTH(&return) > 0
%THEN %DO;
IF code=200
THEN CALL SYMPUT("&return", "0");
ELSE CALL SYMPUT("&return", code);
%END;
%IF %UPCASE(&info) NE NO
%THEN %DO;
PUT "Return Code: " code;
PUT "Return Message: " message;
%END;
RUN;