/*---------------------------------------------------------------------------------- © Copyright 2011-2013 University of Manitoba This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ----------------------------------------------------------------------------------- Programmer : Say Hong Based on the previous work by Mahmoud Azimaee Please direct questions and comments to info@cpe.umanitoba.ca This macro generates a metadata dataset to be used in data quality and documentation processes. The output dataset will be saved as Metadata in the work directory Feb. 27, 2013 Macro Parameters: Domain = Libname of the dataset Db = Dataset name or prefix fmt = Location of a text file containing dataset and variables name with their associated formats Modify to make it compatible with SAS 9.4 04 September 2015. ----------------------------------------------------------------------------------*/ %macro dq_meta(domain=, db=, fmt=); proc datasets nolist memtype=data; delete metadata; quit; proc sql noprint; select memname into :dsn separated by ' ' from dictionary.members where libname = %upcase("&domain") and substr(memname, 1, %length(&db)) = %upcase("&db"); quit; %let n_dsn = %sysfunc(countw(&dsn)); %let wrkdir = %sysfunc(getoption(work)); filename fmtout "&wrkdir\varfmt.txt"; %do i = 1 %to &n_dsn; data chk; infile &fmt delimiter = '09'x; file fmtout; input dsname :$40. varname :$40. fmtname :$40.; if upcase(dsname) = %upcase("%scan(&dsn, &i)") then do; put varname fmtname; output; end; run; data _null_; if 0 then set chk nobs=nobs; call symputx('n', nobs); stop; run; %if &n = 0 %then %do; %put WARNING: Dataset name %upcase(%scan(&dsn, &i)) does not appear in &fmt text file; %put WARNING: No metadata will be generated for %upcase(%scan(&dsn, &i)) dataset; %put; %end; data tmp; set &domain..%scan(&dsn, &i)(obs=0); *format _all_; %if &n > 0 %then %do; format %include "&wrkdir\varfmt.txt"; ; %end; run; proc contents data=tmp out=contents&i (keep=memlabel name type length label format formatl informat informl rename=(type=type1)) noprint; run; data contents&i; length memname $32; retain libname %upcase("&domain") memname "%upcase(%scan(&dsn, &i))"; set contents&i; run; data _null_; fileref="file2del"; extfile=catx('\', pathname('work'), 'varfmt.txt'); rc=filename(fileref, extfile); if rc = 0 and fexist(fileref) then rc = fdelete(fileref); rc=filename(fileref); run; %end; data metadata; length type $ 4 formatdot $ 40 informatdot $ 40; set contents1-contents&n_dsn; if type1=1 then do; type='Num'; if formatl > 0 then CALL CATS (formatdot, format, put(formatl,12.),'.'); else CALL CATS (formatdot, format, '.'); if informl > 0 then CALL CATS (informatdot, informat, put(informl,12.),'.'); else CALL CATS (informatdot, informat, '.'); end; else do; type='Char'; CALL CATS (formatdot, format, '.'); CALL CATS (informatdot, informat, '.'); end; if formatdot in ('$.','.') then formatdot=''; if informatdot in ('$.','.') then informatdot=''; drop type1 formatl informl; run; proc datasets nolist memtype=data lib=work; delete contents1-contents&n_dsn chk tmp; quit; %mend dq_meta;