Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mf_writefile.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Creates a text file using pure macro
4 @details Creates a text file of up to 10 lines. If further lines are
5 desired, feel free to [create an issue](
6 https://github.com/sasjs/core/issues/new), or make a pull request!
7
8 The use of PARMBUFF was considered for this macro, but it would have made
9 things problematic for writing lines containing commas.
10
11 Usage:
12
13 %mf_writefile(&sasjswork/myfile.txt,l1=some content,l2=more content)
14 data _null_;
15 infile "&sasjswork/myfile.txt";
16 input;
17 list;
18 run;
19
20 @param [in] fpath Full path to file to be created or appended to
21 @param [in] mode= (O) Available options are A or O as follows:
22 @li A APPEND mode, writes new records after the current end of the file.
23 @li O OUTPUT mode, writes new records from the beginning of the file.
24 @param [in] l1= () First line
25 @param [in] l2= () Second line (etc through to l10)
26
27 <h4> Related Macros </h4>
28 @li mf_writefile.test.sas
29
30 @version 9.2
31 @author Allan Bowe
32**/
33/** @cond */
34
35%macro mf_writefile(fpath,mode=O,l1=,l2=,l3=,l4=,l5=,l6=,l7=,l8=,l9=,l10=
36)/*/STORE SOURCE*/;
37%local fref rc fid i total_lines;
38
39/* find number of lines by reference to first non-blank param */
40%do i=10 %to 1 %by -1;
41 %if %str(&&l&i) ne %str() %then %goto continue;
42%end;
43%continue:
44%let total_lines=&i;
45
46%if %sysfunc(filename(fref,&fpath)) ne 0 %then %do;
47 %put &=fref &=fpath;
48 %put %str(ERR)OR: %sysfunc(sysmsg());
49 %return;
50%end;
51
52%let fid=%sysfunc(fopen(&fref,&mode));
53
54%if &fid=0 %then %do;
55 %put %str(ERR)OR: %sysfunc(sysmsg());
56 %return;
57%end;
58
59%do i=1 %to &total_lines;
60 %let rc=%sysfunc(fput(&fid, &&l&i));
61 %let rc=%sysfunc(fwrite(&fid));
62%end;
63%let rc=%sysfunc(fclose(&fid));
64%let rc=%sysfunc(filename(&fref));
65
66%mend mf_writefile;
67/** @endcond */