A Macro that can Search and Replace String in your SAS Programs

Similar documents
A Macro that can Search and Replace String in your SAS Programs

Let SAS Help You Easily Find and Access Your Folders and Files

A Macro that Creates U.S Census Tracts Keyhole Markup Language Files for Google Map Use

CC13 An Automatic Process to Compare Files. Simon Lin, Merck & Co., Inc., Rahway, NJ Huei-Ling Chen, Merck & Co., Inc., Rahway, NJ

A SAS Macro Utility to Modify and Validate RTF Outputs for Regional Analyses Jagan Mohan Achi, PPD, Austin, TX Joshua N. Winters, PPD, Rochester, NY

SAS Macros of Performing Look-Ahead and Look-Back Reads

Reading in Data Directly from Microsoft Word Questionnaire Forms

Macro Method to use Google Maps and SAS to Geocode a Location by Name or Address

Using DDE with Microsoft Excel and SAS to Collect Data from Hundreds of Users

Using GSUBMIT command to customize the interface in SAS Xin Wang, Fountain Medical Technology Co., ltd, Nanjing, China

Base and Advance SAS

Program Validation: Logging the Log

Out of Control! A SAS Macro to Recalculate QC Statistics

Using SAS to Control the Post Processing of Microsoft Documents Nat Wooding, J. Sargeant Reynolds Community College, Richmond, VA

PROBLEM FORMULATION, PROPOSED METHOD AND DETAILED DESCRIPTION

INTRODUCTION TO SAS HOW SAS WORKS READING RAW DATA INTO SAS

A Tool to Compare Different Data Transfers Jun Wang, FMD K&L, Inc., Nanjing, China

Dictionary.coumns is your friend while appending or moving data

Electricity Forecasting Full Circle

A Macro to Create Program Inventory for Analysis Data Reviewer s Guide Xianhua (Allen) Zeng, PAREXEL International, Shanghai, China

Top Coding Tips. Neil Merchant Technical Specialist - SAS

Your Own SAS Macros Are as Powerful as You Are Ingenious

Plot Your Custom Regions on SAS Visual Analytics Geo Maps

Useful Tips When Deploying SAS Code in a Production Environment

Paper CC16. William E Benjamin Jr, Owl Computer Consultancy LLC, Phoenix, AZ

Run your reports through that last loop to standardize the presentation attributes

SAS 101. Based on Learning SAS by Example: A Programmer s Guide Chapter 21, 22, & 23. By Tasha Chapman, Oregon Health Authority

How to use UNIX commands in SAS code to read SAS logs

PDF Multi-Level Bookmarks via SAS

A Time Saver for All: A SAS Toolbox Philip Jou, Baylor University, Waco, TX

PharmaSUG Paper CC02

Reducing SAS Dataset Merges with Data Driven Formats

Building Sequential Programs for a Routine Task with Five SAS Techniques

Using SAS Enterprise Guide to Coax Your Excel Data In To SAS

Write SAS Code to Generate Another SAS Program A Dynamic Way to Get Your Data into SAS

Procedure for Stamping Source File Information on SAS Output Elizabeth Molloy & Breda O'Connor, ICON Clinical Research

SAS CURRICULUM. BASE SAS Introduction

ABSTRACT INTRODUCTION MACRO. Paper RF

Cover the Basics, Tool for structuring data checking with SAS Ole Zester, Novo Nordisk, Denmark

PhUSE US Connect 2018 Paper CT06 A Macro Tool to Find and/or Split Variable Text String Greater Than 200 Characters for Regulatory Submission Datasets

Macros are a block of code that can be executed/called on demand

Simplifying Your %DO Loop with CALL EXECUTE Arthur Li, City of Hope National Medical Center, Duarte, CA

Clinical Data Visualization using TIBCO Spotfire and SAS

A Macro To Generate a Study Report Hany Aboutaleb, Biogen Idec, Cambridge, MA

A SAS Solution to Create a Weekly Format Susan Bakken, Aimia, Plymouth, MN

SAS Graphics & Code. stat 480 Heike Hofmann

Macros for Two-Sample Hypothesis Tests Jinson J. Erinjeri, D.K. Shifflet and Associates Ltd., McLean, VA

How a Code-Checking Algorithm Can Prevent Errors

The Output Bundle: A Solution for a Fully Documented Program Run

Using Dynamic Data Exchange

The Demystification of a Great Deal of Files

ABSTRACT INTRODUCTION THE GENERAL FORM AND SIMPLE CODE

Cutting the SAS LOG down to size Malachy J. Foley, University of North Carolina at Chapel Hill, NC

Hidden in plain sight: my top ten underpublicized enhancements in SAS Versions 9.2 and 9.3

PharmaSUG 2013 CC26 Automating the Labeling of X- Axis Sanjiv Ramalingam, Vertex Pharmaceuticals, Inc., Cambridge, MA

SAS Institute Exam A SAS Advanced Programming Version: 6.0 [ Total Questions: 184 ]

SAS automation techniques specification driven programming for Lab CTC grade derivation and more

Use That SAP to Write Your Code Sandra Minjoe, Genentech, Inc., South San Francisco, CA

A Quick and Gentle Introduction to PROC SQL

Moving Data and Results Between SAS and Excel. Harry Droogendyk Stratia Consulting Inc.

ABSTRACT: INTRODUCTION: WEB CRAWLER OVERVIEW: METHOD 1: WEB CRAWLER IN SAS DATA STEP CODE. Paper CC-17

Using Templates Created by the SAS/STAT Procedures

DSCI 325: Handout 15 Introduction to SAS Macro Programming Spring 2017

Validation Summary using SYSINFO

Using UNIX Shell Scripting to Enhance Your SAS Programming Experience

Give me EVERYTHING! A macro to combine the CONTENTS procedure output and formats. Lynn Mullins, PPD, Cincinnati, Ohio

A Macro to Keep Titles and Footnotes in One Place

Unlock SAS Code Automation with the Power of Macros

Sending SAS Data Sets and Output to Microsoft Excel

Tweaking your tables: Suppressing superfluous subtotals in PROC TABULATE

A SAS Macro for Producing Benchmarks for Interpreting School Effect Sizes

An Efficient Tool for Clinical Data Check

INTRODUCTION THE FILENAME STATEMENT CAPTURING THE PROGRAM CODE

Uncommon Techniques for Common Variables

Essential ODS Techniques for Creating Reports in PDF Patrick Thornton, SRI International, Menlo Park, CA

When Powerful SAS Meets PowerShell TM

Automating Comparison of Multiple Datasets Sandeep Kottam, Remx IT, King of Prussia, PA

A SAS Macro to Create Validation Summary of Dataset Report

The Proc Transpose Cookbook

Implementing external file processing with no record delimiter via a metadata-driven approach

Prove QC Quality Create SAS Datasets from RTF Files Honghua Chen, OCKHAM, Cary, NC

Hypothesis Testing: An SQL Analogy

Internet, Intranets, and The Web

Tracking Dataset Dependencies in Clinical Trials Reporting

%Addval: A SAS Macro Which Completes the Cartesian Product of Dataset Observations for All Values of a Selected Set of Variables

%MISSING: A SAS Macro to Report Missing Value Percentages for a Multi-Year Multi-File Information System

HAVE YOU EVER WISHED THAT YOU DO NOT NEED TO TYPE OR CHANGE REPORT NUMBERS AND TITLES IN YOUR SAS PROGRAMS?

Data Quality Review for Missing Values and Outliers

Data Edit-checks Integration using ODS Tagset Niraj J. Pandya, Element Technologies Inc., NJ Vinodh Paida, Impressive Systems Inc.

SAS Online Training: Course contents: Agenda:

BUSINESS ANALYTICS. 96 HOURS Practical Learning. DexLab Certified. Training Module. Gurgaon (Head Office)

So Much Data, So Little Time: Splitting Datasets For More Efficient Run Times and Meeting FDA Submission Guidelines

Please Don't Lag Behind LAG!

Missing Pages Report. David Gray, PPD, Austin, TX Zhuo Chen, PPD, Austin, TX

ODS DOCUMENT, a practical example. Ruurd Bennink, OCS Consulting B.V., s-hertogenbosch, the Netherlands

Chapter 6: Modifying and Combining Data Sets

How to validate clinical data more efficiently with SAS Clinical Standards Toolkit

WRITE SAS CODE TO GENERATE ANOTHER SAS PROGRAM

Debugging. Where to start? John Ladds, SAS Technology Center, Statistics Canada.

Logging the Log Magic: Pulling the Rabbit out of the Hat

Transcription:

ABSTRACT MWSUG 2016 - Paper BB27 A Macro that can Search and Replace String in your SAS Programs Ting Sa, Cincinnati Children s Hospital Medical Center, Cincinnati, OH In this paper, a SAS macro is introduced that can search and replace any string in the SAS programs. To use the macro, the user only needs to pass the folder name, the search string to it. If the user wants to use the replacement function, the user also needs to pass the replacement string to the macro. The macro will check all the SAS programs in this folder and the subfolders to find out which files contain the search string. The macro will generate new SAS files for the replacement so that the old files will not be affected. An html report will be generated by the macro to include the original file locations, the line number of the SAS codes that contain the search string and the SAS codes with search string highlighted in yellow. If you use the replacement string function, the html report will also include the location information for the new SAS files. The location information in the html report is created with hyperlinks, so the user can directly open the files from the report. INTRODUCTION Sometimes, we want to change some codes in the SAS programs, like a variable name has been changed and we need to update the programs. If we have a lot of SAS files and we don t know which files contain the string we are looking for, it is time consuming to open each file, do the search and change. It will be nice if we have a tool available that can automatically do the work no matter how many SAS files we have. The macro presenting in this paper can do the search and replacement of any string in the SAS programs and create a user-friendly html report to access the information. Error! Reference source not found. shows you a sample html report generated by the macro, the search string in this example is %macro and the search folders are C:\Program Files\SASHome\x86\SASFoundation\9.4\or\sample and its subfolders. Figure 1. The Screenshot of a Sample HTML Report for the Search Function 1

If we pass the replacement string to the macro, the macro will create new files for those files that contain the search string, replace the search string with the replacement string in those new files. The new files names will have the prefix n_ followed by the original file names. The new files will be saved in the same folder as the original files. Figure 2 shows you a sample html report for the replacement function. The search string is %macro, the replacement string is *This is a macro.;%macro and the search folders are C:\Program Files\SASHome\x86\SASFoundation\9.4\or\sample and its subfolders. Figure 2. The Screenshot of a Sample HTML Report for the Replacement Function The user can click hyperlinks in the report to directly open the files. THE MACRO PARAMETERS You can find the whole codes of the macro at the end of the paper. Below is the layout of the macro: %macro SearchReplace(foldernm=,searchstring=,replacestring=%str(),htmldir= %str(c:\result.html)); /*the codes are at the end of the paper*/ %mend; The foldernm is used to indicate the name of the search folder. The searchstring is used to save the search string. 2

The replacestring is used to save the replacement string. If you don t want to use the replacement function, you don t need to pass the value to this macro variable. The htmldir is used to indicate where you want to save the html report. If you don t pass a value to this macro variable, by default, the html report will be saved as c:\result.html. CALL THE MACRO The following macro call will search the %macro in all the SAS programs located in the C:\Program Files\SASHome\x86\SASFoundation\9.4\or\sample folder and its subfolders. If the macro finds any SAS files that contain the search string, the macro will replace the search string with the replacement string *this is a macro;%macro. Then the macro will create new SAS files. The new SAS files are named as n_ followed by the original file names, for e.g, if the original file name is test. sas, the new file name will be n_test.sas. The new files will be saved in the same folder as the original files. The html report will be saved as the c:\result1.html. %SearchReplace(foldernm=%str(C:\Program Files\SASHome\x86\SASFoundation\9.4\or\sample), searchstring=%str('%macro'),replacestring=%str('*this is a macro;%macro'), htmldir=%str(c:\result1.html)); If you just want to use the search function, you don t need to pass a value to the replacestring macro variable, the following SAS codes show you an example, by calling the macro in this way, it will search the string %macro in the C:\test folder and its subfolders, the html report will be saved as c:\result.html by default: %SearchReplace(foldernm=%str(C:\test),searchstring=%str('%macro')); THE MACRO CODES Below are all the sas codes for macro SearchReplace: %macro SearchReplace (foldernm=,searchstring=,replacestring=%str(),htmldir=%str(c:\result.html)); option mprint mlogic symbolgen NOXWAIT NOXSYNC; filename ren pipe "dir ""&foldernm.\*.sas"" /b /s"; %put &foldernm.; *dirinfo is a SAS data set that saves all the file information for the searching folder; data dirinfo; infile ren pad; input wholename $250.; format filename $250.; filename=cats(scan(scan(wholename,-1,'\'),1,'.')); data _null_; set dirinfo end=end; num=cats(_n_); call symput("m" num,cats(wholename)); call symput("n" num,cats(filename)); if end then call symput("file_ct",num); %do i=1 %to &file_ct.; 3

*sasfile_&i. is the SAS data set that saves the SAS program codes; data sasfile_&i.; infile "&&m&i" delimiter = '@@' missover dsd lrecl=32767 firstobs=1 TERMSTR=CRLF; informat all $char5000. ; input all $ ; data sasfile_&i.; length wholename $200. filename $50.; wholename="&&m&i"; filename="&&n&i"; set sasfile_&i.; line_no=_n_; *contain_string_sasfile_&i. contains the SAS codes that have the search string; data contain_string_sasfile_&i.; set sasfile_&i.; if index(all,%str(&searchstring.)) >0; *search_result is the SAS data set that contains all the search_results; data search_result; set contain_string_sasfile_:; proc sort data=search_result;by wholename line_no; *use this proc sql to check if the search_result data set is empty or not; proc sql noprint; select count(*) into :search_row_ct from search_result; quit; *htmlresult the data set that contains the data to be printed to the html report; data htmlresult; length location $300; %if &replacestring. ^= and &search_row_ct. ^= 0 %then %do; length newloc $300; set search_result; location=cats("<a href='",wholename,"'>",wholename,"</a><br>"); %if &replacestring. ^= and &search_row_ct. ^= 0 %then %do; newloc=cats("<a href='",tranwrd(wholename,cats(filename,".sas"),""),cats("n_",filename,".sas")," '>",tranwrd(wholename,cats(filename,".sas"),""),cats("n_",filename,".sas"),"</a> <br>"); label newloc="new File Location"; sascodes=tranwrd(all,&searchstring.,catx(" ","^S={background=yellow font_weight=bold}",&searchstring.,"^s={}")); data htmlresult; set htmlresult; 4

rename line_no=sas_codes_line_no; drop all wholename filename; *print the result to the html report; ods html file="&htmldir."; ods escapechar='^'; %if &search_row_ct. ^= 0 %then %do; proc print data=htmlresult noobs label; /*if didn't find any files contain the search string, the following message will print to the html report;*/ %else %do; ods html text="<h1 align='center'>don't find &searchstring. in the SAS files.</h1>"; ods html close; *use the internet explorer to open the html report; x "start iexplore &htmldir."; *do the replacement function; %if &replacestring. ^= and &search_row_ct. ^= 0 %then %do; *the all_sasfiles is the SAS data set that has all the SAS codes in all the SAS programs; data all_sasfiles; set sasfile_:; * the new_result data set contains the new SAS codes that contain the search string and have been replaced by the replacement string; data new_result; set search_result; newcodes=tranwrd(all,&searchstring.,&replacestring.); /*the following codes merge the replaced SAS codes to the all SAS codes file, the data set new_sas_files contains all the SAS codes with the newly replaced SAS codes for those files that have the search string;*/ proc sql; create table sasfiles_contain_string as select * from all_sasfiles where wholename in (select distinct wholename from search_result) order by wholename,line_no; create table new_sas_files as select s.*,newcodes from sasfiles_contain_string as s left join new_result as n on s.wholename=n.wholename and s.line_no=n.line_no; quit; data new_sas_files; set new_sas_files; 5

if newcodes="" then newcodes=all; *the files data set contains the original file location and the new file location information for those files that have the search string; proc sql; create table files as select distinct wholename,cats(tranwrd(wholename,cats(filename,".sas"),""),cats("n_",filename,". sas")) as newloc from new_sas_files; quit; data _null_; set files end=end; num=cats(_n_); call symput("filenm" num,cats(wholename)); call symput("loc" num,cats(newloc)); if end then call symput("ct",num); *create new SAS files for those files that have the search string and replace the search string with the replacement string; %do i=1 %to &ct.; data f&i.; set new_sas_files; where wholename="&&filenm&i"; keep newcodes; data _null_; set f&i.; file "&&loc&i." notitles noprint; put newcodes; proc sql;drop table f&i.;quit; proc datasets lib=work kill;quit; %MEND; CONCLUSION The macro presented in this paper can be used as a helpful tool to search and replace any strings in the SAS program files. Also you can further extend the functions of the macro by searching strings with different patterns, for e.g, search for all the string that start with the same prefix, suffix or middle parts, you can refer to this paper [1] for more details about how to search for different patterns. ACKNOWLEDGMENTS The author wishes to thank the Division of Biostatistics and Epidemiology at Cincinnati Children's Hospital Medical Center for its support. 6

REFERENCES 1. Ting Sa, Yanhong Liu. A Simple Macro to Select Various Variables Lists. The Pharmaceutical Industry SAS Users Group Proceedings 2015, May17-20, 2015, Orlando, USA. CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Ting Sa Division of Biostatistics and Epidemiology, Cincinnati Children s Hospital Medical Center 5136363674 Ting.Sa@CCHMC.ORG SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are trademarks of their respective companies. 7