/* Name: verifyVariables.sas */ /* Author: John Cantrell */ /* Date: 11/12/04 */ /* */ /* Change Log: */ /* Programmer Name Initials Date */ /* */ /* */ /* */ /* Description: */ /* Checks variables in pairs of */ /* datasets prior to dataset */ /* combination. */ /* For combination by concatenation or */ /* interleaving (using SET statement), */ /* each dataset in the pair is checked */ /* to see if character variables of */ /* the same name in the two datasets */ /* are of consistent length. If */ /* inconsistencies in length are found,*/ /* an ERROR message is written in */ /* the log. */ /* For combination by merging (using */ /* MERGE statement), each dataset in */ /* the pair is first checked to see if */ /* there are any variables with the */ /* same name. If variables with the */ /* same name occur in both datasets, */ /* an ERROR message is written to the */ /* log. Then each dataset in the pair */ /* is checked to see if character */ /* variables of the same name in the */ /* two datasets are of consistent */ /* length. If inconsistencies in */ /* length are found, an ERROR message */ /* is written in the log. */ /* */ /* Inputs: Defined by parameters. */ /* */ /* Outputs: Error statement to log concerning */ /* variable lengths. */ /* */ /* Parameters: */ /* "dsn1" is input dataset name, either*/ /* temporary or permanent (qualified */ /* with a libname). */ /* "dsn2" is input dataset name, either*/ /* temporary or permanent (qualified */ /* with a libname). */ /* "statementType" specifies the type */ /* ofdataset combination that the macro*/ /* is checking for. */ /* For concatenation and interleaving */ /* combination, */ /* specifystatementType=SET. */ /* For merging combination, specity */ /* statementType=MERGE. */ /* */ %macro verifyVariables(dsn1,dsn2,statementType); /* Error checking for presence of required parameters. */ %if &dsn1= or &dsn2= or &statementType= %then %do; %put ERROR: You must supply parameter dsn1, dsn2, and statementType to macro verifyVariables.; %put ERROR: dsn1 and dsn2 are datasets (with libnames if appropriate). statementType is; %put ERROR: either:; %put ERROR: "merge" or "m" if a MERGE statement follows the macro; %put ERROR: or "set" or "s" if a SET statement follows the macro.; %goto exit; %end; ods listing close; /* Don't need to print anything*/ proc contents /* Get variable list for first dataset.*/ data=&dsn1 out=contentXVVS1 nodetails; run; data contentXVVS1 (drop=oldname); /* Organize variable list from first dataset. */ set contentXVVS1 (keep=name type length /* Keep only important variables.*/ rename=(name=oldname type=type1 length=length1)); name=upcase(oldname); run; proc sort data=contentXVVS1; /* Sort by Name to prepare for merge. */ by name; run; proc contents /* Get variable list for second dataset.*/ data=&dsn2 out=contentXVVS2 nodetails; run; data contentXVVS2 (drop=oldname); /* Organize variable list from second dataset. */ set contentXVVS2 (keep=name type length /* Keep only important variables.*/ rename=(name=oldname type=type2 length=length2)); name=upcase(oldname); run; proc sort data=contentXVVS2; /* Sort by Name to prepare for merge. */ by name; run; /* Merge first and second datasets. Keep only records where variable name */ /* is in both datasets. */ data bothXVVS; merge contentXVVS1 (in=a) contentXVVS2 (in=b); by name; if a and b; run; /* In character variables, check for inconsistencies in variable length */ /* If inconsistencies are found, put an ERROR statement in the log. */ data bothXVVS; set bothXVVS; if upcase("&statementType")="SET" or upcase("&statementType")="S" then do; if type1=2 and length1^=length2 then do; put; put "ERROR: The character variable " name " is in both datasets &dsn1 and &dsn2 but is of" " inconsistent length. If datasets &dsn1 and &dsn2 are combined data loss by truncation may" " occur. To prevent data loss re-size the smaller of the two character variables named " name " before combining datasets &dsn1 and &dsn2.."; put ; _ERROR_+1; end; end; if upcase("&statementType")="MERGE" or upcase("&statementType")="M" then do; put; put "ERROR: The variable " name " is in both datasets &dsn1 and &dsn2. If variable" name " is to be used in a BY statement following a MERGE statement, then there" " is no problem. However, if variable " name " is not to be used in a BY" " statement, and if datasets &dsn1 and &dsn2 are merged, the data in one" " of the variables named " name " will overwrite the data in the other" " variable named " name ", causing data loss. To avoid this data loss," " re-name one of the variables named " name "."; put; _ERROR_+1; if type1=2 and length1^=length2 then do; put; put "ERROR: The character variable " name " is in both datasets &dsn1 and &dsn2 but is of" " inconsistent length. If datasets &dsn1 and &dsn2 are combined and neither of the variables" " named " name " are re-named then data loss by truncation may occur. To prevent data loss" " increase the size of the smaller of the two character variables named " name " so that both" " variables named " name " are of the same length before combining datasets &dsn1 and &dsn2.."; put ; _ERROR_+1; end; end; run; /* Clean up work library by deleting datasets used in macro. */ proc datasets; delete bothXVVS contentXVVS1 contentXVVS2; quit; ods listing; %exit: %mend verifyVariables;