Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
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(wrap=YES, insert_cmplib=YES)
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= (NO) Choose YES to add the proc fcmp wrapper.
35 @param [out] insert_cmplib= (NO) Choose YES to insert the package into the
36 CMPLIB reference.
37 @param [out] lib= (work) The output library in which to create the catalog.
38 @param [out] cat= (sasjs) The output catalog in which to create the package.
39 @param [out] pkg= (utils) The output package in which to create the function.
40 Uses a 3 part format: libref.catalog.package
41
42**/
43
44%macro mcf_string2file(wrap=NO
45 ,insert_cmplib=NO
46 ,lib=WORK
47 ,cat=SASJS
48 ,pkg=UTILS
49)/*/STORE SOURCE*/;
50
51%if &wrap=YES %then %do;
52 proc fcmp outcat=&lib..&cat..&pkg;
53%end;
54
55function mcf_string2file(filepath $, string $, mode $);
56 if mode='APPEND' then fmode='a';
57 else fmode='o';
58 length fref $8;
59 rc=filename(fref,filepath);
60 if (rc ne 0) then return( -1 );
61 fid = fopen(fref,fmode);
62 if (fid = 0) then return( -1 );
63 rc=fput(fid, string);
64 rc=fwrite(fid);
65 rc=fclose(fid);
66 rc=filename(fref);
67 return(0);
68endsub;
69
70
71%if &wrap=YES %then %do;
72 quit;
73%end;
74
75%if &insert_cmplib=YES %then %do;
76 options insert=(CMPLIB=(&lib..&cat));
77%end;
78
79%mend mcf_string2file;