
/*****************************************************************/
/* S A S S A M P L E L I B R A R Y */
/* */
/* NAME: EXPRTSCH */
/* TITLE: Export a scheme data set to DataFlux' dfPower .SCH */
/* file format */
/* PRODUCT: SAS Data Quality - Cleanse */
/* SYSTEM: Windows, UNIX */
/* KEYS: export scheme .sch dataflux dfpower data set */
/* PROCS: */
/* DATA: EXTERNAL */
/* */
/* UPDATE: 22MAY2001 */
/* REF: */
/* */
/* MISC: dfPower 4.0 and earlier stores schemes in .SCH */
/* files. SAS Data Quality - Cleanse stores schemes */
/* in SAS data sets. This code defines a macro */
/* that exports the SAS scheme data set to the .SCH */
/* file format needed by DataFlux' dfPower product. */
/* */
/* After the scheme is exported from SAS, open dfPower */
/* Studio, select Vault->Import... and choose the file */
/* that was just created by this macro. This will */
/* copy the file into the Scheme Vault, register the */
/* meta data in dfPower, and thus make the scheme */
/* available for use. */
/* */
/* USAGE: The EXPRTSCH macro has 3 parameters: */
/* */
/* 1) The first parameter is a positional parameter */
/* named SCHEMEDS. This parameter represents the */
/* name of the SAS scheme data set that you want to */
/* export. If the data set is stored in the WORK */
/* library you can specify a one-level name, */
/* otherwise you must specify a two-level name */
/* (libref.dataset) in an allocated library. This */
/* parameter is required. */
/* */
/* 2) The second parameter is a keyword parameter */
/* named SCHEMEDIR. This parameter represents */
/* the directory in which the scheme file should be */
/* created (omit the trailing slash/backslash). If */
/* not specified, this will default to the */
/* "C:\Schemes_Exported_from_SAS" directory, */
/* although you need to create this directory in */
/* advance (the macro will not create the directory */
/* for you). The name of the dfPower scheme file */
/* will not be specified by the user as it will be */
/* the same as that of the original SAS scheme data */
/* set. */
/* */
/* 3) The third parameter is a keyword parameter */
/* named MODE. This parameter indicates whether */
/* the scheme was designed to be applied to entire */
/* phrases (MODE=PHRASE) or to elements of phrases */
/* (MODE=ELEMENT). dfPower uses this information */
/* as the default when applying the scheme. If not */
/* specified, this will default to MODE=PHRASE. */
/* */
/* EXAMPLES: The following examples will export a SAS scheme */
/* data set to create a scheme file named */
/* C:\Schemes_Exported_From_SAS\STATES.sch, which can */
/* then be imported into the dfPower Scheme Vault: */
/* */
/* */
/* 1) To export a SAS scheme from a data set named */
/* STATES in the WORK library to the default directory */
/* as a "phrase" scheme, you would invoke the macro in */
/* one of these ways: */
/* %EXPRTSCH(states) */
/* %EXPRTSCH(states, mode=phrase) */
/* %EXPRTSCH(work.states) */
/* %EXPRTSCH(work.states, mode=phrase) */
/* */
/* 2) To export a SAS scheme from a data set named */
/* STATES in the WORK library to the default directory */
/* as an "element" scheme, you would invoke the macro */
/* in one of these ways: */
/* %EXPRTSCH(states, mode=element) */
/* %EXPRTSCH(work.states, mode=element) */
/* */
/* 3) To export a SAS scheme from a data set named */
/* STATES in the SASUSER library to the default */
/* directory as a "phrase" scheme, you would invoke */
/* the macro in one of these ways: */
/* %EXPRTSCH(sasuser.states) */
/* %EXPRTSCH(sasuser.states, mode=phrase) */
/* */
/* 4) To export a SAS scheme from a data set named */
/* STATES in the SASUSER library to the default */
/* directory as an "element" scheme, you would invoke */
/* the macro in this way: */
/* %EXPRTSCH(sasuser.states, mode=element) */
/* */
/* 5) The difference in this example is that you */
/* specify the directory in which to create the */
/* the scheme file, so this example creates a scheme */
/* file named C:\MySchemes\STATES.sch */
/* %EXPRTSCH(sasuser.states, */
/* schemedir=C:\MySchemes, */
/* mode=element) */
/* */
/*****************************************************************/
%macro exprtsch(
schemeds,
schemedir=C:\Schemes_Exported_from_SAS,
mode=phrase);
/* Check to make sure that the schemeds was specified. */
%if %length(%trim(&schemeds)) eq 0 %then %do;
%put;
%put ERROR: You must specify the name of a SAS Scheme data set;
%put ERROR: as the first parameter. EXPRTSCH macro terminating.;
%put;
%goto FINE;
%end;
/* Check to make sure that the schemedir exists. */
filename testit "&schemedir";
data _null_;
rc=fileref('testit');
call symput('fileexist', trim(left(put(rc,9.))));
run;
%if &fileexist ne 0 %then %do;
%put;
%put ERROR: The scheme directory, &schemedir,;
%put ERROR: could not be allocated. Make sure that;
%put ERROR: the directory exists and re-run the macro.;
%put ERROR: EXPRTSCH macro terminating.;
%put;
%goto FINE;
%end;
/* Check each character in the scheme data set name to see that it
is alpha, numeric, and underscore or a period. If so, it is
a valid data set name. */
%let alphas=ABCDEFGHIJKLMNOPQRSTUVWXYZ;
%let nums=1234567890;
%let underscore=_;
%let period=.;
%let schemeds=%upcase(&schemeds);
%if %verify(%trim(&schemeds),
&alphas&nums&underscore&period) ne 0 %then %do;
%put;
%put ERROR: The data set that you specified, &schemeds, is invalid.;
%put ERROR: EXPRTSCH macro terminating.;
%put;
%goto FINE;
%end;
/* Do some validation on the SAS scheme data set. It must not be
empty, it must contain a character column named DATA and a
character column named STANDARD. If not, produce error messages
and terminate the macro. */
/* One-level data set name. */
%if %index(&schemeds,.) eq 0 %then %do;
%let libname=WORK;
%let memname=&schemeds;
%end;
/* Two-level data set name. */
%else %do;
%let libname=%scan(&schemeds, 1, .);
%let memname=%substr(&schemeds,%index(&schemeds,.)+1);
%end;
proc sql noprint;
select nobs into :nobs
from dictionary.tables
where libname = "&libname" and memname="&memname";
select nvar into :nvar
from dictionary.tables
where libname = "&libname" and memname="&memname";
quit;
%if &nobs eq 0 %then %do;
%put;
%put ERROR: SAS scheme data set, &schemeds, is empty.;
%put ERROR: EXPRTSCH macro terminating.;
%put;
%goto FINE;
%end;
%else %if &nvar < 2 %then %do;
%put;
%put ERROR: SAS scheme data set, &schemeds, must contain at least;
%put ERROR: two variables.;
%put ERROR: EXPRTSCH macro terminating.;
%put;
%goto FINE;
%end;
/* Check to see that the data set has variables named
DATA and STANDARD. */
%if &nvar ne 0 %then %do;
proc sql noprint;
create table _cols as
select name, type
from dictionary.columns
where libname="&libname" and memname="&memname";
quit;
data _null_;
set _cols nobs=nobs;
if upcase(name) in ('DATA' 'STANDARD') then do;
if upcase(type) = 'CHAR' then found+1;
end;
if _n_=nobs then do;
if found = 2 then call symput('varcheck', 'OK');
else call symput('varcheck', 'notOK');
end;
run;
%if &varcheck ne OK %then %do;
%put;
%put ERROR: SAS scheme data set, &schemeds, does not contain the;
%put ERROR: required variables. Data set must contain;
%put ERROR: a character variable named DATA and a;
%put ERROR: character variable named STANDARD.;
%put ERROR: EXPRTSCH macro terminating.;
%put;
%goto FINE;
%end;
%end;
/* If the supplied SCHEMEDIR has a slash at the end, remove it. */
%if %verify(%substr(&schemedir, %length(&schemedir)),/\) eq 0 %then
%let schemedir=%substr(&schemedir, 1, %length(&schemedir)-1);
/* Validate the supplied MODE. If it is not set to PHRASE or
EXTENSION, generate a warning and default to PHRASE. */
%let mode=%upcase(&mode);
%if (&mode eq PHRASE) %then %let mode=P;
%else %if (&mode eq ELEMENT) %then %let mode=E;
%else %do;
%put;
%put WARNING: A value of &mode for the MODE parameter is invalid.;
%put WARNING: The default value of MODE=PHRASE will be used instead.;
%put;
%let mode=P;
%end;
/* Place NOTEs in the log. */
%put;
%put NOTE: Beginning to export the SAS scheme data set &libname..&memname;
%put NOTE: to the DataFlux Scheme File:;
%put NOTE: "&schemedir\&memname..sch";
%put;
/* Allocate the scheme file to export to. */
filename schfile "&schemedir/%upcase(&memname).sch";
/* Actually do the export. */
data _null_;
set &libname..&memname;
file schfile;
if _n_=1 then do;
put "# Do not edit this file directly -- " @;
put "Exported from SAS data set &libname..&memname -- " @;
put "&mode";
put "%left(&nobs)";
end;
put data $char255.;
put standard $char255.;
run;
/* Check to see if it worked. */
%if &SYSERR ne 0 %then %do;
%put;
%put ERROR: An error occurred when exporting the scheme file.;
%put ERROR: The exact error message can be found in the SAS Log.;
%put ERROR: EXPRTSCH macro terminating.;
%put;
%goto FINE;
%end;
%FINE:
filename testit clear;
filename schfile clear;
%mend;
|