Presentation is loading. Please wait.

Presentation is loading. Please wait.

Automate & Zip files Using MACRO

Similar presentations


Presentation on theme: "Automate & Zip files Using MACRO"— Presentation transcript:

1 Automate & Zip files Using MACRO
By: Arjun Shrestha

2 Requirement Architectural design Implementation and code
Overview Requirement Architectural design Implementation and code Objective of this presentation is to demonstrate automating zipping process using advance SAS MACRO. I will discuss translating requirements into architectural design and SAS macro approach to get desired outcome

3 Requirement & Architectural Design
Pickup files based on dates modified from different folder and dump into zipped folder

4 Code PROC PRINTTO; RUN; %global FILE_NAME; %global zipped_folder_name;
%global numoflocation; %global numofftozip; %LET path = EXCEL_FILE_LOCATION; %LET logPATH = LOG_FILE_LOCATION; DATA _NULL_; CALL SYMPUT('LOGTIME', STRIP(COMPRESS(PUT(DATE(),YYMMDD10.),'-'))||'_'||STRIP(COMPRESS(PUT(TIME(),TOD9.),':')) ); PROC PRINTTO LOG="&logPATH\SAS_LOG\zipp_&LOGTIME..LOG"; DATA INPUT_DATA; CALL SYMPUT('FNAME','''' || 'DIR "'|| "&PATH"|| '" /B' || ''''); %PUT FNAME = &FNAME;

5 Code Continue… filename DIRLIST pipe &FNAME; %MACRO FIND_EXCEL_FILE;
%LET FILE_NAME = ''; %LET BOL_FILEFOUND = 0; data dirlist ; infile DIRLIST lrecl=200 truncover; input file_name $200.; IF INDEX(upcase(file_name),"XLSX") ne 0 THEN DO; CALL SYMPUT('FILE_NAME',FILE_NAME); call symput('zipped_folder_name',scan(FILE_NAME,1,'.')); OUTPUT; END; run; %LET BOL_FILEFOUND = &SYSNOBS; %PUT BOL_FILEFOUND = &BOL_FILEFOUND.; %LET FILE_NAME = &FILE_NAME; %PUT FILE_NAME = &FILE_NAME; %put zipped_folder_name = &zipped_folder_name. ; %MEND FIND_EXCEL_FILE; %FIND_EXCEL_FILE;

6 Code Continue… %MACRO READEXCEL;
PROC IMPORT DATAFILE = %IF %LENGTH("&FILE_NAME.") > 3 %THEN %DO; "&PATH\&FILE_NAME." %END; %ELSE %DO; "&PATH\&HARDCODED_FILE_NAME." DBMS = XLSX OUT = inputfolder(where=(input_file_location is not null)) REPLACE; SHEET = sheet1; RUN; %MEND READEXCEL; %READEXCEL; proc sql; create table DUMMY as select input_file_location from inputfolder; quit; %let numoflocation = &sqlobs.; %put numoflocation = &numoflocation.;

7 Code Continue… proc sql; select input_file_location , start_date
, end_date , input_file_type , output_file_location , output_zipped_folder_name into :input_file_location1-:input_file_location&numoflocation. , :start_dt1-:start_dt&numoflocation. , :end_dt1-:end_dt&numoflocation. , :input_file_type1-:input_file_type&numoflocation. , :output_file_location1-:output_file_location&numoflocation. , :output_zipped_folder_name1-:output_zipped_folder_name&numoflocation. from inputfolder; quit; filename newfile "&&output_file_location&numoflocation..\&zipped_folder_name..zip"; data _null_; if (fexist('newfile')) then rc = fdelete('newfile'); run; filename newfile clear;

8 Code Continue… options mprint mlogic; %macro RUN_PACKAGE;
ods package(&zipped_folder_name.) open nopf; %do i=1 %to &numoflocation.; %macro zipfiles(input_file_location= ,output_file_location= ,zipped_folder_name= ,start_dt= ,end_dt= ,input_file_type=); %let input_file_location = &input_file_location.; %let output_file_location = &output_file_location.; %let start_dt = &start_dt.; %let end_dt = &end_dt.; %let input_file_type=&input_file_type; DATA run_mon_&i.; CALL SYMPUT('FNAME'||strip(put(&i.,4.)), '''' || 'DIR "'|| "&input_file_location.\" || '" /B' || ''''); RUN; %PUT FNAME&i. = &&FNAME&i..; filename DIRLIST pipe &&FNAME&i..;

9 Code Continue… data test&i.; infile dirlist pad lrecl=250;
filename $250.; rcs = filename("fileref", "&input_file_location.\" || trim(filename)); msgs = sysmsg(); fid=fopen('fileref'); Bytes=finfo(fid,'File Size (bytes)'); crdate=finfo(fid,'Create Time'); moddate=finfo(fid,'Last Modified'); moddate_check=compress(put(input(substr(moddate,1,9),date9.),yymmdd10.),'-'); rc = fclose(fid); rcc = filename("fileref"); if moddate_check ge "&start_dt" and moddate_check le "&end_dt." and INDEX(upcase(filename),upcase("&input_file_type.")) ne 0 then do; call execute (catx (' ', 'ods package(&zipped_folder_name.)', 'add file=', quote("&input_file_location.\" || trim(filename)), ';' ) ); end; run; %mend zipfiles;

10 Code Continue… %zipfiles(input_file_location=&&input_file_location&i.., output_file_location=&&output_file_location&i.., zipped_folder_name=&zipped_folder_name., start_dt=&&start_dt&i.., end_dt=&&end_dt&i.., input_file_type=&&input_file_type&i.. ); %end; ods package(&zipped_folder_name.) publish archive properties(archive_name="&zipped_folder_name..zip" archive_path="&&output_file_location&numoflocation.."); ods package(&zipped_folder_name.) close; %mend RUN_PACKAGE; %RUN_PACKAGE;

11 Questions ?


Download ppt "Automate & Zip files Using MACRO"

Similar presentations


Ads by Google