Skip to content

Commit

Permalink
docs: include documentation for smile_replace_in_text
Browse files Browse the repository at this point in the history
Documentation has been udpated including next to the macro documentation also an example program and the corresponding documentation of that as well.
  • Loading branch information
KatjaGlassConsulting committed Mar 5, 2021
1 parent d53fee2 commit b1a4d6e
Show file tree
Hide file tree
Showing 21 changed files with 2,379 additions and 1,881 deletions.
45 changes: 27 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# SMILE - Smart SAS Macros

SMILE stands for <b>S</b>mart SAS <b>M</b>acros - an <b>I</b>ntuitive <b>L</b>ibrary <b>E</b>xtension

Smile contains various small SAS macros supporting various tasks of a SAS programmer. Some macros are inspired by other open source macros and some by available papers. A complete overview can be seen below. More detailed documentation can be found in the macro headers or on the [documentation page](https://katjaglassconsulting.github.io/SMILE-SmartSASMacros/).

# Macro Overview

## Macro Overview
The following SAS macros are currently available:

Macro | Description
---|---
smile_attr_var |Function-style macro to return a variable attribute of a dataset. The following attributes are available: VARTYPE, VARLEN, VARLABEL, VARFMT and VARINFMT
Expand All @@ -15,28 +16,36 @@ smile_attrn |Function-style macro to return a numeric attribute of a dataset. Th
smile_ods_document_flat_label |Flat navigation and optionally re-label navigation for ODS DOCUMENT. The navigation bookmark level is reduced to one level only. Optionally a label can be applied to all content items or the navigation label can be removed completely.
smile_pdf_merge |Merge multiple PDF files and create one bookmark entry per PDF file with PROC GROOVY and open-source Tool PDFBox
smile_pdf_read_bookmarks |Read PDF Bookmarks into a SAS dataset with the variables level, title and page
smile_replace_in_text_files |Replace text from all files contained in a folder with a different text
smile_url_check |Check existence of URL and store result in return code, information can optionally be printed to the log
smile_url_download |Downloads a file from an URL and store it locally on OUTFILE. Additionally return code can be stored and information can optionally be printed to the log.

# Getting Started

The macros can be cloned or downloaded directly from the GitHub repository to use this within any SAS environment. The macros has been developed under SAS 9.4 Unix, but are likely to run on other operating systems and also under other SAS versions like from SAS 9.2.

## Getting Started
The macros can be cloned or downloaded directly from the GitHub repository to use this within any SAS environment. The macros has been developed under SAS 9.4 Unix, but are likely to run on other operating systems and also under other SAS versions like from SAS 9.2.
To use the macros in any program, these can be included single by single via `%INCLUDE "<macro>";` or the folder where the macros are located can be included into the SASAUTOS path `OPTIONS SASAUTOS=(<path>, SASAUTOS);`. The macros can also be stored into a SAS Macro store and used from there.

The repository is using the following structure:
The macros could easily be made available in any SAS environment with internet connection with the following two statements:

```sas
FILENAME macros URL "https://raw.githubusercontent.com/KatjaGlassConsulting/SMILE-SmartSASMacros/main/all.sas";
%INCLUDE macros;
```

The repository is using the following structure:

Folder | Content
--- |---
macros | This folder contains the core macros
programs | General programs can be found here - test_* programs are available to check the functionality of macros
results | Outputs generated through programs

# License

## License
The macros and programs are using the MIT-License. See [LICENSE](https://github.com/KatjaGlassConsulting/SMILE-SmartSASMacros/blob/main/LICENSE) for more information.

# Contact

## Contact
These macros has been developed by Katja Glass Consulting. Feel free to reach me through my [website](https://www.glacon.eu) or via [LinkedIn](https://www.linkedin.com/in/katja-glass-369022167/).

40 changes: 39 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
# Overview
# SMILE - Smart SAS Macros

SMILE stands for <b>S</b>mart SAS <b>M</b>acros - an <b>I</b>ntuitive <b>L</b>ibrary <b>E</b>xtension

The macros could easily be made available in any SAS environment with internet connection with the following two statements:

```sas
FILENAME macros URL "https://raw.githubusercontent.com/KatjaGlassConsulting/SMILE-SmartSASMacros/main/all.sas";
%INCLUDE macros;
```

## Macro Overview

The following macros are available in the SMILE - Smart SAS Macros - an Intuitive Library Extension.

Expand All @@ -10,6 +21,7 @@ smile_attrn |Function-style macro to return a numeric attribute of a dataset. Th
smile_ods_document_flat_label |Flat navigation and optionally re-label navigation for ODS DOCUMENT. The navigation bookmark level is reduced to one level only. Optionally a label can be applied to all content items or the navigation label can be removed completely.
smile_pdf_merge |Merge multiple PDF files and create one bookmark entry per PDF file with PROC GROOVY and open-source Tool PDFBox
smile_pdf_read_bookmarks |Read PDF Bookmarks into a SAS dataset with the variables level, title and page
smile_replace_in_text_files |Replace text from all files contained in a folder with a different text
smile_url_check |Check existence of URL and store result in return code, information can optionally be printed to the log
smile_url_download |Downloads a file from an URL and store it locally on OUTFILE. Additionally return code can be stored and information can optionally be printed to the log.

Expand Down Expand Up @@ -172,6 +184,32 @@ PDFBOX_JAR |path and jar file name for PDFBOX open source tool, e.g. &path/pdfbo
<br/>


### smile_replace_in_text_files

Key | Description
---|---
Name |smile_replace_in_text_files
Purpose |Replace text from all files contained in a folder with a different text
SAS Version |SAS 9.4
Author |Katja Glass
Date |2021-03-05
Example Program |../programs/test_smile_replace_in_text_files.sas

********************************************

The following parameters are used:

Parameter | Description
---|---
INPATH |input directory (no quotes)
OUTPATH |output directory, if same as INPATH, files are overwritten (no quotes)
REPLACE_FROM |text which should be replaced (with quotes)
REPLACE_WITH |text that should newly be added (with quotes)
FILETYPE |extension of file types which should be processed, e.g. sas or txt (optional)

<br/>


### smile_url_check

Key | Description
Expand Down
200 changes: 200 additions & 0 deletions docs/smile_replace_in_text_files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# Macro SMILE_REPLACE_IN_TEXT_FILES

Replace text from all files contained in a folder with a different text

- Author: Katja Glass
- Date: 2021-03-05
- SAS Version: SAS 9.4
- License: MIT
- Example Program: [test_smile_replace_in_text_files](test_smile_replace_in_text_files.md)

## Parameters

Parameter | Description
---|---
INPATH |input directory (no quotes)
OUTPATH |output directory, if same as INPATH, files are overwritten (no quotes)
REPLACE_FROM |text which should be replaced (with quotes)
REPLACE_WITH |text that should newly be added (with quotes)
FILETYPE |extension of file types which should be processed, e.g. sas or txt (optional)

<br/>


## Examples

```
%* replace the default root path in all example files;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/programs,
outpath = /home/u49641771/smile/programs,
replace_from = '%LET root = /folders/myshortcuts/git/SMILE-SmartSASMacros;',
replace_to = '%LET root = /home/u49641771/smile;');
%* replace all tabls with four spaces;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/programs,
outpath = /home/u49641771/smile/programs/blub,
replace_from = '09'x,
replace_to = ' ');
%* replace all zero numbers with x in the output;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/results,
outpath = /home/u49641771/smile/results/mod,
replace_from = '0',
replace_to = 'x',
filetype = lst);
```

## Macro

``` sas linenums="1"
%************************************************************************************************************************;
%* Project : SMILE - SAS Macros, Intuitive Library Extension
%* Macro : smile_replace_in_text_files
%* Parameters : INPATH - input directory (no quotes)
%* OUTPATH - output directory, if same as INPATH, files are overwritten (no quotes)
%* REPLACE_FROM - text which should be replaced (with quotes)
%* REPLACE_WITH - text that should newly be added (with quotes)
%* FILETYPE - extension of file types which should be processed, e.g. sas or txt (optional)
%*
%* Purpose : Replace text from all files contained in a folder with a different text
%*
%* ExampleProg: ../programs/test_smile_replace_in_text_files.sas
%*
%* Author : Katja Glass
%* Creation : 2021-03-05
%* License : MIT
%*
%* SAS Version: SAS 9.4
%*
%************************************************************************************************************************;
/*
Examples:
%* replace the default root path in all example files;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/programs,
outpath = /home/u49641771/smile/programs,
replace_from = '%LET root = /folders/myshortcuts/git/SMILE-SmartSASMacros;',
replace_to = '%LET root = /home/u49641771/smile;');
%* replace all tabls with four spaces;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/programs,
outpath = /home/u49641771/smile/programs/blub,
replace_from = '09'x,
replace_to = ' ');
%* replace all zero numbers with x in the output;
%smile_replace_in_text_files(
inpath = /home/u49641771/smile/results,
outpath = /home/u49641771/smile/results/mod,
replace_from = '0',
replace_to = 'x',
filetype = lst);
*/
%************************************************************************************************************************;
%MACRO smile_replace_in_text_files(
inpath = ,
outpath = ,
replace_from = ,
replace_to = ,
filetype =
);
%LOCAL macro;
%LET macro = &sysmacroname;
%IF %SYSFUNC(FILEEXIST("&inpath")) < 1
%THEN %DO;
%PUT %STR(ERR)OR: &macro - INPATH folder does not exist: &inpath.;
%RETURN;
%END;
%IF %SYSFUNC(FILEEXIST("&outpath")) < 1
%THEN %DO;
%PUT %STR(ERR)OR: &macro - OUTPATH folder does not exist: &inpath.;
%RETURN;
%END;
%IF %LENGTH(&replace_from) = 0
%THEN %DO;
%PUT %STR(ERR)OR: &macro - REPLACE_FROM must be provided.;
%RETURN;
%END;
%IF %LENGTH(&replace_to) = 0
%THEN %DO;
%PUT %STR(ERR)OR: &macro - REPLACE_FROM must be provided.;
%RETURN;
%END;
%* read all files (ignore those without dot (folders));
FILENAME myPath "&inpath";
DATA _dir (KEEP=filename);
ATTRIB filename FORMAT=$200.;
list = DOPEN('myPath');
IF list > 0
THEN DO;
DO i = 1 to dnum(list);
filename = TRIM(DREAD(list,i));
%IF %LENGTH(&filetype) > 0
%THEN %DO;
IF INDEX(UPCASE(filename),"%UPCASE(.&filetype)") > 0
THEN OUTPUT;
%END;
%ELSE %DO;
IF INDEX(filename,".") > 0
THEN OUTPUT;
%END;
END;
END;
rc = CLOSE(list);
RUN;
FILENAME myPath;
%* re-create all files using a TRANWRD to perform replacement;
DATA _NULL_;
SET _dir;
ATTRIB cmd FORMAT=$1000.;
ATTRIB from FORMAT=$1000.;
ATTRIB to FORMAT=$1000.;
from = SYMGET('replace_from');
to = SYMGET('replace_to');
%* create three filenames (input, output, temporary in-between (needed if input=output));
CALL EXECUTE('FILENAME _rep_tmp TEMP;');
cmd = "FILENAME _rep_in '&inpath/" || STRIP(filename) || "';";
CALL EXECUTE(cmd);
cmd = "FILENAME _rep_out '&outpath/" || STRIP(filename) || "';";
CALL EXECUTE(cmd);
%* replace texts and create temporary file;
CALL EXECUTE('DATA _NULL_;');
CALL EXECUTE(' INFILE _rep_in LRECL=2000 END=_eof;');
CALL EXECUTE(' FILE _rep_tmp LRECL=2000;');
CALL EXECUTE(' ATTRIB line FORMAT=$2000.;');
CALL EXECUTE(' INPUT;');
CALL EXECUTE(' line = _INFILE_;');
cmd = "line = TRANWRD(line," || STRIP(from) || ", " || STRIP(to) || ');';
PUT cmd = ;
CALL EXECUTE(cmd);
CALL EXECUTE(' IF LENGTHN(line) = 0 THEN PUT;');
CALL EXECUTE(' ELSE DO;');
CALL EXECUTE(' pos = LENGTHN(line) - LENGTHN(STRIP(line)) + 1;');
CALL EXECUTE(' PUT @pos line;');
CALL EXECUTE(' END;');
CALL EXECUTE('RUN;');
%* store temporary file as final file;
CALL EXECUTE('DATA _NULL_;');
CALL EXECUTE(" rc=FCOPY('_rep_tmp', '_rep_out');");
CALL EXECUTE('RUN;');
RUN;
PROC DATASETS LIB=WORK NOLIST;
DELETE _dir;
RUN;
%MEND smile_replace_in_text_files;
```

Loading

0 comments on commit b1a4d6e

Please sign in to comment.