Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mcf_string2file.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Adds a string to a file
4 @details Creates an fcmp function for appending a string to an external file.
5 If the file does not exist, it is created.
6
7 The function itself takes the following (positional) parameters:
8
9 | PARAMETER | DESCRIPTION |
10 |------------|-------------|
11 | filepath $ | full path to the file|
12 | string $ | string to add to the file |
13 | mode $ | mode of the output - either APPEND (default) or CREATE |
14
15 It returns 0 if successful, or -1 if an error occured.
16
17 Usage:
18
19 %mcf_string2file()
20
21 data _null_;
22 rc=mcf_string2file(
23 "%sysfunc(pathname(work))/newfile.txt"
24 , "This is a test"
25 , "CREATE");
26 run;
27
28 data _null_;
29 infile "%sysfunc(pathname(work))/newfile.txt";
30 input;
31 putlog _infile_;
32 run;
33
34 @param [out] wrap= (YES) Choose NO to omit the proc fcmp wrapper.
35 @param [out] lib= (work) The output library in which to create the catalog.
36 @param [out] cat= (sasjs) The output catalog in which to create the package.
37 @param [out] pkg= (utils) The output package in which to create the function.
38 Uses a 3 part format: libref.catalog.package
39
40 <h4> SAS Macros </h4>
41 @li mcf_init.sas
42
43 <h4> Related Programs </h4>
44 @li mcf_stpsrv_header.test.sas
45 @li mp_init.sas
46
47**/
48
49%macro mcf_string2file(wrap=YES
50 ,lib=WORK
51 ,cat=SASJS
52 ,pkg=UTILS
53)/*/STORE SOURCE*/;
54%local i var cmpval found;
55%if %mcf_init(mcf_string2file)=1 %then %return;
56
57%if &wrap=YES %then %do;
58 proc fcmp outlib=&lib..&cat..&pkg;
59%end;
60
61function mcf_string2file(filepath $, string $, mode $);
62 if mode='APPEND' then fmode='a';
63 else fmode='o';
64 length fref $8;
65 rc=filename(fref,filepath);
66 if (rc ne 0) then return( -1 );
67 fid = fopen(fref,fmode);
68 if (fid = 0) then return( -1 );
69 rc=fput(fid, string);
70 rc=fwrite(fid);
71 rc=fclose(fid);
72 rc=filename(fref);
73 return(0);
74endsub;
75
76
77%if &wrap=YES %then %do;
78 quit;
79%end;
80
81/* insert the CMPLIB if not already there */
82%let cmpval=%sysfunc(getoption(cmplib));
83%let found=0;
84%do i=1 %to %sysfunc(countw(&cmpval,%str( %(%))));
85 %let var=%scan(&cmpval,&i,%str( %(%)));
86 %if &var=&lib..&cat %then %let found=1;
87%end;
88%if &found=0 %then %do;
89 options insert=(CMPLIB=(&lib..&cat));
90%end;
91
92%mend mcf_string2file;