Skip to content

Commit 6accbc7

Browse files
authored
Merge pull request #44 from lexjansen/github_issue_09_filerefs
Issue #9: Support file references
2 parents 09a5955 + 43f1a50 commit 6accbc7

File tree

3 files changed

+63
-33
lines changed

3 files changed

+63
-33
lines changed

macros/read_datasetjson.sas

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
%macro read_datasetjson(
22
jsonpath=,
3+
jsonfref=,
34
datalib=,
45
dropseqvar=Y,
56
savemetadata=Y,
@@ -27,7 +28,6 @@
2728

2829
%* Check for missing parameters ;
2930
%let _Missing=;
30-
%if %sysevalf(%superq(jsonpath)=, boolean) %then %let _Missing = &_Missing jsonpath;
3131
%if %sysevalf(%superq(datalib)=, boolean) %then %let _Missing = &_Missing datalib;
3232
%if %sysevalf(%superq(dropseqvar)=, boolean) %then %let _Missing = &_Missing dropseqvar;
3333
%if %sysevalf(%superq(savemetadata)=, boolean) %then %let _Missing = &_Missing savemetadata;
@@ -38,13 +38,36 @@
3838
%goto exit_macro;
3939
%end;
4040

41+
%* Spoecify either jsonpath or jsonfref;
42+
%if %sysevalf(%superq(jsonpath)=, boolean) and %sysevalf(%superq(jsonfref)=, boolean) %then %do;
43+
%put ERR%str(OR): [&sysmacroname] Both jsonpath and jsonfref are missing. Specify one of them.;
44+
%goto exit_macro;
45+
%end;
46+
47+
48+
%* Spoecify either jsonpath or jsonfref;
49+
%if %sysevalf(%superq(jsonpath)=, boolean)=0 and %sysevalf(%superq(jsonfref)=, boolean)=0 %then %do;
50+
%put ERR%str(OR): [&sysmacroname] Specify either jsonpath or jsonfref, but not both.;
51+
%goto exit_macro;
52+
%end;
53+
54+
%* Check for non-existing jsonpath;
55+
%if %sysevalf(%superq(jsonpath)=, boolean)=0 %then %do;
56+
%if not %sysfunc(fileexist(&jsonpath)) %then %do;
57+
%put ERR%str(OR): [&sysmacroname] JSON file jsonpath=&jsonpath does not exist.;
58+
%goto exit_macro;
59+
%end;
60+
%end;
61+
62+
%* Check if datalib has been assigned ;
4163
%if %sysevalf(%superq(datalib)=, boolean)=0 %then %do;
4264
%if (%sysfunc(libref(&datalib)) ne 0 ) %then %do;
4365
%put ERR%str(OR): [&sysmacroname] datalib library &datalib has not been assigned.;
4466
%goto exit_macro;
4567
%end;
4668
%end;
4769

70+
%* Check if metadatalib has been assigned ;
4871
%if %sysevalf(%superq(metadatalib)=, boolean)=0 %then %do;
4972
%if (%sysfunc(libref(&metadatalib)) ne 0 ) %then %do;
5073
%put ERR%str(OR): [&sysmacroname] metadatalib library &metadatalib has not been assigned.;
@@ -75,13 +98,16 @@
7598
%let _SaveOptions2 = %sysfunc(getoption(compress, keyword)) %sysfunc(getoption(reuse, keyword));
7699
options compress=Yes reuse=Yes;
77100

78-
filename json&_Random "&jsonpath";
101+
%if %sysevalf(%superq(jsonpath)=, boolean)=0 %then
102+
filename json&_Random "&jsonpath";;
103+
%if %sysevalf(%superq(jsonfref)=, boolean)=0 %then
104+
filename json&_random "%sysfunc(pathname(&jsonfref))";;
105+
79106
filename mapmeta "../maps/map_meta.map";
80-
filename map&_Random "%sysfunc(pathname(work))/map_%scan(&jsonpath, -2, %str(.\/)).map";
81-
libname out_&_Random "%sysfunc(pathname(work))/%scan(&jsonpath, -2, %str(.\/))";
107+
filename map&_Random "%sysfunc(pathname(work))/map_%scan(%sysfunc(pathname(json&_random)), -2, %str(.\/)).map";
108+
libname out_&_Random "%sysfunc(pathname(work))/%scan(%sysfunc(pathname(json&_random)), -2, %str(.\/))";
82109

83-
libname json&_Random json map=map&_Random automap=create fileref=json&_Random
84-
%if "%substr(%upcase(&savemetadata),1,1)" ne "Y" %then noalldata; ordinalcount=none;
110+
libname json&_Random json map=map&_Random automap=create fileref=json&_Random noalldata ordinalcount=none;
85111
proc copy in=json&_Random out=out_&_Random;
86112
run;
87113

@@ -437,8 +463,8 @@
437463

438464
%exit_macro_no_rows:
439465

440-
libname json&_Random clear;
441466
filename json&_Random clear;
467+
libname json&_Random clear;
442468
filename map&_Random clear;
443469
filename mapmeta clear;
444470

macros/write_datasetjson.sas

+28-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
%macro write_datasetjson(
22
dataset=,
33
jsonpath=,
4+
jsonfref=,
45
usemetadata=N,
56
metadatalib=,
67
decimalVariables=,
7-
iso8601Variables=,
88
datasetJSONVersion=1.1.0,
99
fileOID=,
1010
originator=,
@@ -59,7 +59,6 @@
5959
%* Check for missing parameters ;
6060
%let _Missing=;
6161
%if %sysevalf(%superq(dataset)=, boolean) %then %let _Missing = &_Missing dataset;
62-
%if %sysevalf(%superq(jsonpath)=, boolean) %then %let _Missing = &_Missing jsonpath;
6362
%if %sysevalf(%superq(usemetadata)=, boolean) %then %let _Missing = &_Missing usemetadata;
6463

6564
%if %length(&_Missing) gt 0
@@ -68,6 +67,26 @@
6867
%goto exit_macro;
6968
%end;
7069

70+
%* Check for non-existing dataset ;
71+
%if not %sysfunc(exist(&dataset)) %then %do;
72+
%put ERR%str(OR): [&sysmacroname] Dataset dataset=&dataset does not exist.;
73+
%goto exit_macro;
74+
%end;
75+
76+
%* Spoecify either jsonpath or jsonfref;
77+
%if %sysevalf(%superq(jsonpath)=, boolean) and %sysevalf(%superq(jsonfref)=, boolean) %then %do;
78+
%put ERR%str(OR): [&sysmacroname] Both jsonpath and jsonfref are missing. Specify one of them.;
79+
%goto exit_macro;
80+
%end;
81+
82+
83+
%* Spoecify either jsonpath or jsonfref;
84+
%if %sysevalf(%superq(jsonpath)=, boolean)=0 and %sysevalf(%superq(jsonfref)=, boolean)=0 %then %do;
85+
%put ERR%str(OR): [&sysmacroname] Specify either jsonpath or jsonfref, but not both.;
86+
%goto exit_macro;
87+
%end;
88+
89+
7190
%* Rule: usemetadata has to be Y or N *;
7291
%if "%substr(%upcase(&usemetadata),1,1)" ne "Y" and "%substr(%upcase(&usemetadata),1,1)" ne "N" %then
7392
%do;
@@ -92,15 +111,15 @@
92111
%* Rule: when usemetadata eq Y then metadata datasets need to exist in the metadatalib library *;
93112
%if "%substr(%upcase(&usemetadata),1,1)" eq "Y" %then %do;
94113
%if not %sysfunc(exist(&metadatalib..metadata_study)) %then %do;
95-
%put ERR%str(OR): [&sysmacroname] When usemetadata=Y, then &metadatalib..metadata_study must exist.;
114+
%put ERR%str(OR): [&sysmacroname] usemetadata=Y, but &metadatalib..metadata_study does not exist.;
96115
%goto exit_macro;
97116
%end;
98117
%if not %sysfunc(exist(&metadatalib..metadata_tables)) %then %do;
99-
%put ERR%str(OR): [&sysmacroname] When usemetadata=Y, then &metadatalib..metadata_tables must exist.;
118+
%put ERR%str(OR): [&sysmacroname] usemetadata=Y, but &metadatalib..metadata_tables does not exist.;
100119
%goto exit_macro;
101120
%end;
102121
%if not %sysfunc(exist(&metadatalib..metadata_columns)) %then %do;
103-
%put ERR%str(OR): [&sysmacroname] When usemetadata=Y, then &metadatalib..metadata_columns must exist.;
122+
%put ERR%str(OR): [&sysmacroname] usemetadata=Y, but &metadatalib..metadata_columns does not exist.;
104123
%goto exit_macro;
105124
%end;
106125
%end;
@@ -109,8 +128,6 @@
109128
%if "%substr(%upcase(&usemetadata),1,1)" eq "Y" %then %do;
110129
%if %sysevalf(%superq(decimalVariables)=, boolean)=0
111130
%then %put WAR%str(NING): [&sysmacroname] When macro parameter usemetadata=&usemetadata then parameter decimalVariables will not be used.;
112-
%if %sysevalf(%superq(iso8601Variables)=, boolean)=0
113-
%then %put WAR%str(NING): [&sysmacroname] When macro parameter usemetadata=&usemetadata then parameter iso8601Variables will not be used.;
114131
%end;
115132

116133
%* Rule: allowed versions *;
@@ -405,22 +422,6 @@
405422
where (datatype in ('datetime', 'date', 'time')) and (targetdatatype = 'integer');
406423
quit;
407424

408-
%if %sysevalf(%superq(iso8601Variables)=, boolean)=0 %then %do;
409-
data work.column_metadata;
410-
set work.column_metadata;
411-
%do _count=1 %to %sysfunc(countw(&iso8601Variables, %str(' ')));
412-
if upcase(name)=upcase("%scan(&iso8601Variables, &_count)") then do;
413-
if missing(displayFormat) then
414-
putlog "WAR" "NING: [&sysmacroname] &dataset.." name +(-1) ": variable has no format attached." name= dataType= targetDataType=;
415-
else do;
416-
putlog name= dataType= targetDataType= displayFormat=;
417-
end;
418-
end;
419-
%end;
420-
run;
421-
%let _iso8601_variables=&iso8601Variables;
422-
%end;
423-
424425
%if %sysevalf(%superq(_iso8601_variables)=, boolean)=0 %then %do;
425426
%put NOTE: [&sysmacroname] &dataset: numeric ISO 8601 variables converted to strings: &_iso8601_variables;
426427
%end;
@@ -486,7 +487,10 @@
486487
;
487488
quit;
488489

489-
filename json&_random "&jsonpath";
490+
%if %sysevalf(%superq(jsonpath)=, boolean)=0 %then
491+
filename json&_random "&jsonpath";;
492+
%if %sysevalf(%superq(jsonfref)=, boolean)=0 %then
493+
filename json&_random "%sysfunc(pathname(&jsonfref))";;
490494

491495
data work.column_metadata;
492496
retain itemOID name label dataType targetDataType length displayFormat keySequence;

programs/06_compare_data.sas

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ data _null_;
9999
run;
100100

101101
ods html5 close;
102-
ods html5 file="&project_folder/programs" file="07_compare_data_summary_&today_iso8601..html";
102+
ods html5 path="&project_folder/programs" file="07_compare_data_summary_&today_iso8601..html";
103103

104104
proc print data=results.dataset_compare_results label;
105105
title01 "Compare Summary - &now_iso8601";
@@ -114,7 +114,7 @@ proc delete data=work.members;
114114
run;
115115

116116
/*
117-
libname dataaadam clear;
117+
libname dataadam clear;
118118
libname outadam clear;
119119
libname datasdtm clear;
120120
libname outsdtm clear;

0 commit comments

Comments
 (0)