Last active
November 3, 2017 17:18
-
-
Save Cesar-Urteaga/2489eff4d5b639c4591846d7ef598edf to your computer and use it in GitHub Desktop.
From a set of tables, creates a summary of attributes for each one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*-----------------------------------------------------------------------------| | |
| Description : From a set of tables, creates a summary of attributes for each | | |
| one. | | |
| Assumptions : The file extension must be "sas7bdat", and the table names are | | |
| stored as they are in the libraries (i.e., paths and file | | |
| names should be case-sensitive). | | |
| Parameters : InputTable - Table with the paths and file names. | | |
| OutputTable - Table with the file attributes | | |
| (Default: InputTable). | | |
| PathNameVariables - String with the variable names separated | | |
| by spaces. The first one must be the path | | |
| of the table, and the second one should be | | |
| the name of the file (Default: INPUT_PATH | | |
| INPUT_TABLE). | | |
| Output : A table with the attributes for each file. | | |
|-----------------------------------------------------------------------------*/ | |
/* Examples: | |
* Creates the file(s) and the table(s) so as to execute the examples. ; | |
* Creates a table with the file's information. ; | |
%LET _MLT_WORK_PATH = %SYSFUNC(GETOPTION(WORK)); %PUT &_MLT_WORK_PATH.; | |
DATA TABLES; | |
LENGTH INPUT_TABLE $11; | |
INPUT INPUT_TABLE $; | |
INPUT_PATH = "&_MLT_WORK_PATH."; | |
DATALINES; | |
temporal_01 | |
temporal_02 | |
temporal_03 | |
; | |
RUN; | |
* Creates the tables to be loaded. ; | |
DATA TEMPORAL_01; X = 1; OUTPUT; X = 2; OUTPUT; RUN; | |
DATA TEMPORAL_02; Y = 1; RUN; | |
DATA TEMPORAL_03; X = 1; Y = 2; Z = 3; RUN; | |
* Ex_01; | |
%MGetTableAttributes(InputTable = TABLES); | |
* Output: Adds attribute fields to the table "TABLES". ; | |
*/ | |
/*-----------------------------------------------------------------------------| | |
| Date Author Description | | |
|------------------------------------------------------------------------------| | |
| December 19, 2016 Cesar R. Urteaga-Reyesvera Creation. | | |
|-----------------------------------------------------------------------------*/ | |
%MACRO MGetTableAttributes(InputTable = /* Table with the paths and file | |
names. */, | |
OutputTable = &InputTable. /* Table with the | |
file attributes. */, | |
PathNameVariables = INPUT_PATH INPUT_TABLE | |
/* String with the variable names | |
separated by spaces. The | |
first one must be the path of | |
the table, and the second one | |
should be the name of the | |
file. */ | |
); | |
* We define the table variables with the path and name of the tables. ; | |
%LET _MLT_PATH_VARIABLE = %SCAN(&PathNameVariables., 1); | |
%LET _MLT_NAME_VARIABLE = %SCAN(&PathNameVariables., 2); | |
* We store the paths and the table names in local macro variables. ; | |
DATA _NULL_; | |
SET &InputTable. NOBS = _NO_TABLES END = _LAST_RECORD; | |
ARRAY VARIABLES[*] &_MLT_PATH_VARIABLE. | |
&_MLT_NAME_VARIABLE.; | |
DO I = 1 TO HBOUND(VARIABLES); | |
CALL SYMPUTX(COMPRESS(VNAME(VARIABLES[I])||'_'||_N_), | |
VARIABLES[I], | |
'L'); | |
END; | |
IF _LAST_RECORD THEN CALL SYMPUTX('_MLT_NO_TABLES', _NO_TABLES, 'L'); | |
RUN; | |
* We create the attributes for each table. ; | |
%DO I = 1 %TO &_MLT_NO_TABLES.; | |
* We create a temporary library. ; | |
%IF %SYSFUNC(LIBNAME(______TL, | |
&&&_MLT_PATH_VARIABLE._&I., , | |
ACCESS = READONLY)) %THEN | |
%PUT %SYSFUNC(SYSMSG()); | |
* We create a table only if the table exists. ; | |
%IF %SYSFUNC(EXIST(______TL.&&&_MLT_NAME_VARIABLE._&I..)) %THEN | |
%DO; | |
* We calculate the size of each file. ; | |
%LET _MLT_FILEPATH = &&&_MLT_PATH_VARIABLE._&I./&&&_MLT_NAME_VARIABLE._&I...sas7bdat; | |
%LET _MLT_OPEN_CLOSE_FILEREF = %SYSFUNC(FILENAME(_MLT_FILEREF, | |
&_MLT_FILEPATH) | |
); | |
%LET _MLT_OPEN_FILE = %SYSFUNC(FOPEN(&_MLT_FILEREF)); | |
%LET _MLT_FILE_SIZE_BYTES = %SYSFUNC(FINFO(&_MLT_OPEN_FILE, | |
File size (bytes) | |
) | |
); | |
%LET _MLT_CLOSE_FILE = %SYSFUNC(FCLOSE(&_MLT_OPEN_FILE)); | |
%LET _MLT_OPEN_CLOSE_FILEREF = %SYSFUNC(FILENAME(_MLT_FILEREF)); | |
* We store the attributes in a table. ; | |
DATA ______TABLE_&I.(DROP = _OPEN_TABLE | |
_CLOSE_TABLE); | |
_RECORD = &I.; | |
SET &InputTable POINT = _RECORD; | |
FORMAT TABLE_EXISTS 8. | |
CREATION_DATE | |
MODIFIED_DATE DATETIME16. | |
DAYS_BETWEEN_DATES 8. | |
NUMBER_OBSERVATIONS | |
NUMBER_VARIABLES COMMA32. | |
SIZE_MEGABYTES COMMA32.2; | |
TABLE_EXISTS = 1; | |
_OPEN_TABLE = OPEN(COMPRESS("______TL."||&_MLT_NAME_VARIABLE.)); | |
CREATION_DATE = ATTRN(_OPEN_TABLE, 'CRDTE'); | |
MODIFIED_DATE = ATTRN(_OPEN_TABLE, 'MODTE'); | |
DAYS_BETWEEN_DATES = INTCK('DAY', | |
DATEPART(CREATION_DATE), | |
DATEPART(MODIFIED_DATE) | |
); | |
NUMBER_OBSERVATIONS = ATTRN(_OPEN_TABLE, 'NOBS'); | |
NUMBER_VARIABLES = ATTRN(_OPEN_TABLE, 'NVARS'); | |
SIZE_MEGABYTES = &_MLT_FILE_SIZE_BYTES / 2 ** 20; | |
_CLOSE_TABLE = CLOSE(_OPEN_TABLE); | |
OUTPUT; | |
STOP; | |
RUN; | |
* We close the temporary library. ; | |
%LET _MLT_CLOSE_LIBRARY = %SYSFUNC(LIBNAME(______TL)); | |
%END; | |
%END; | |
DATA &OutputTable.; | |
SET | |
%DO I = 1 %TO &_MLT_NO_TABLES; | |
______TABLE_&I. | |
%END; | |
; | |
RUN; | |
* We eliminate the auxiliary tables. ; | |
PROC SQL; | |
DROP TABLE | |
%DO I = 1 %TO &_MLT_NO_TABLES; | |
______TABLE_&I. | |
%IF &I. < &_MLT_NO_TABLES %THEN ,; | |
%END; | |
; | |
QUIT; | |
%MEND MGetTableAttributes; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment