Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mx_createfile.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Creates a file in the logical filesystem of the target platform
4 @details When building applications that run on multiple flavours of SAS, it
5 is convenient to use a single macro (like this one) to write a text file -
6 eg a settings file - without worrying about the platform primitive.
7
8 The file is created in the logical filesystem of the target platform (SAS
9 Content folder, SASjs Drive, or metadata folder). No file is created on a
10 physical filesystem. Platform behaviour:
11 - SASVIYA: creates a SAS Content file (%mv_createfile)
12 - SASJS: creates a file on SASjs Drive (%ms_createfile)
13 - SAS 9 / metadata: SAS 9 has no "file" concept for Stored Process content,
14 so a type 2 Stored Process is created in metadata (%mm_createstp), with
15 the content loaded as the STP source code
16
17 The alternative would be to compile a generic macro in target-specific
18 folders (SASVIYA, SAS9 and SASJS). This avoids compiling unnecessary macros
19 at the expense of a more complex sasjsconfig.json setup.
20
21 Usage:
22
23 filename ft15f001 temp;
24 data _null_;
25 file ft15f001;
26 put '%let mysetting=42;';
27 run;
28
29 %* viya: /Public/app/myapp/settings (Files Service) ;
30 %* sasjs: /Public/app/myapp/settings.sas (SASjs Drive) ;
31 %* sas9: /User Folders/sasdemo/myapp/settings (metadata) ;
32 %let filepath=/Public/app/myapp/settings;
33 %mx_createfile(&filepath, inref=ft15f001)
34
35 @param [in] filepath The full path of the file to create, INCLUDING the
36 filename (must contain at least one `/` delimiter). On SASjs Server the
37 name should carry the .sas extension if it will be executed as a Stored
38 Program.
39 @param [in] inref= (0) The fileref containing the file content. A valid
40 fileref is mandatory - if omitted, the macro aborts with an error.
41 @param [in] mdebug= (0) Set to 1 to enable DEBUG messages
42
43 <h4> SAS Macros </h4>
44 @li mf_getplatform.sas
45 @li mf_getuniquefileref.sas
46 @li mm_createstp.sas
47 @li ms_createfile.sas
48 @li mv_createfile.sas
49
50 <h4> Related Macros </h4>
51 @li mx_createwebservice.sas
52 @li mx_getcode.sas
53
54**/
55
56%macro mx_createfile(filepath
57 ,inref=0
58 ,mdebug=0
59)/*/STORE SOURCE*/;
60
61%local platform name shortloc;
62%let platform=%mf_getplatform();
63
64%if &inref=0 %then %do;
65 %put %str(ERR)OR: &sysmacroname requires a valid inref fileref;
66 %return;
67%end;
68
69%if &platform=SASJS %then %do;
70 %ms_createfile(%superq(filepath), inref=&inref, mdebug=&mdebug)
71%end;
72%else %if &platform=SASVIYA %then %do;
73 /* extract name & path from &filepath */
74 data _null_;
75 filepath=symget('filepath');
76 name=scan(filepath,-1,'/');
77 shortloc=substr(filepath,1,length(filepath)-length(name)-1);
78 call symputx('name',name,'l');
79 call symputx('shortloc',shortloc,'l');
80 run;
81 %mv_createfile(path=%superq(shortloc)
82 ,name=%superq(name)
83 ,inref=&inref
84 ,mdebug=&mdebug
85 )
86%end;
87%else %if &platform=SAS9 or &platform=SASMETA %then %do;
88 /* no file concept - create a type 2 STP (source code saved in metadata).
89 The mm_createstp macro requires a physical file, so copy the inref
90 content to WORK first. It then loads the source code, meaning no
91 separate mm_updatestpsourcecode call is needed. */
92 data _null_;
93 filepath=symget('filepath');
94 name=scan(filepath,-1,'/');
95 shortloc=substr(filepath,1,length(filepath)-length(name)-1);
96 call symputx('name',name,'l');
97 call symputx('shortloc',shortloc,'l');
98 run;
99 %local fref;
100 %let fref=%mf_getuniquefileref();
101 filename &fref "%sysfunc(getoption(work))/%superq(name).sas";
102 data _null_;
103 infile &inref lrecl=32767;
104 file &fref lrecl=32767;
105 input;
106 put _infile_;
107 run;
108 %mm_createstp(stpname=%superq(name)
109 ,tree=%superq(shortloc)
110 ,filename=%superq(name).sas
111 ,directory=%sysfunc(getoption(work))
112 ,stptype=2
113 ,mdebug=&mdebug
114 )
115 %if &mdebug=0 %then %do;
116 /* remove the temporary physical file */
117 %local rc;
118 %let rc=%sysfunc(fdelete(&fref));
119 filename &fref clear;
120 %end;
121%end;
122%else %do;
123 %put %str(ERR)OR: &sysmacroname: &platform is unsupported!!!;
124 %let syscc=1012;
125%end;
126
127%mend mx_createfile;