Macros for SAS Application Developers
https://github.com/sasjs/core
mf_readfile.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Reads the first line of a file using pure macro
4  @details Reads the first line of a file and returns it. Future versions may
5  read each line into a macro variable array.
6 
7  Generally, reading data into macro variables is not great as certain
8  nonprintable characters (such as CR, LF) may be dropped in the conversion.
9 
10  Usage:
11 
12  %mf_writefile(&sasjswork/myfile.txt,l1=some content,l2=more content)
13 
14  %put %mf_readfile(&sasjswork/myfile.txt);
15 
16 
17  @param [in] fpath Full path to file to be read
18 
19  <h4> Related Macros </h4>
20  @li mf_deletefile.sas
21  @li mf_writefile.sas
22  @li mf_readfile.test.sas
23 
24  @version 9.2
25  @author Allan Bowe
26 **/
27 /** @cond */
28 
29 %macro mf_readfile(fpath
30 )/*/STORE SOURCE*/;
31 %local fref rc fid fcontent;
32 
33 /* check file exists */
34 %if %sysfunc(filename(fref,&fpath)) ne 0 %then %do;
35  %put &=fref &=fpath;
36  %put %str(ERR)OR: %sysfunc(sysmsg());
37  %return;
38 %end;
39 
40 %let fid=%sysfunc(fopen(&fref,I));
41 
42 %if &fid=0 %then %do;
43  %put %str(ERR)OR: %sysfunc(sysmsg());
44  %return;
45 %end;
46 
47 %if %sysfunc(fread(&fid)) = 0 %then %do;
48  %let rc=%sysfunc(fget(&fid,fcontent,65534));
49  &fcontent
50 %end;
51 
52 /*
53 %do %while(%sysfunc(fread(&fid)) = 0);
54  %let rc=%sysfunc(fget(&fid,fcontent,65534));
55  &fcontent
56 %end;
57 */
58 
59 %let rc=%sysfunc(fclose(&fid));
60 %let rc=%sysfunc(filename(&fref));
61 
62 %mend mf_readfile;
63 /** @endcond */