Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion all.sas
Original file line number Diff line number Diff line change
Expand Up @@ -11828,7 +11828,8 @@ insert into &outds select distinct * from &append_ds;
LUA, you can also use this macro: mp_gsubfile.sas

@param [in] infile The QUOTED path to the file on which to perform the
substitution
substitution. Note that you can extract the pathname from a fileref using
the pathname function, eg: `"%sysfunc(pathname(fref))"`;
@param [in] findvar= Macro variable NAME containing the string to search for
@param [in] replacevar= Macro variable NAME containing the replacement string
@param [out] outfile= (0) Optional QUOTED path to the adjusted output file (to
Expand Down Expand Up @@ -24239,6 +24240,20 @@ run;
run;
%mv_createfile(path=/Public/temp,name=newfile.txt,inref=myfile)

The macro also supports find & replace (used by the SASjs Streaming App
build program). This allows one string to be replaced by another at the
point at which the file is created. This is done by passing in the NAMES of
the macro variables containing the values to be swapped, eg:

filename fref temp;
data _null_;
file fref;
put 'whenever life gets you down, Mrs Brown..';
run;
%let f=Mrs Brown;
%let r=just remember that you're standing on a planet that's evolving;
%mv_createfile(path=/Public,name=life.md,inref=fref,fin,swap=f r)


@param [in] path= The parent (SAS Drive) folder in which to create the file
@param [in] name= The name of the file to be created
Expand All @@ -24260,6 +24275,8 @@ run;
@li sas_services
@param [in] force= (YES) Will overwrite (delete / recreate) files by default.
Set to NO to abort if a file already exists in that location.
@param pin] swap= (0) Provide two macro variable NAMES that contain the values
to be swapped, eg swap=find replace (see also the example above)
@param [out] outds= (_null_) Output dataset with the uri of the new file

@param [in] mdebug= (0) Set to 1 to enable DEBUG messages
Expand All @@ -24273,6 +24290,7 @@ run;
@li mfv_getpathuri.sas
@li mp_abort.sas
@li mp_base64copy.sas
@li mp_replace.sas
@li mv_createfolder.sas

<h4> Related Macros</h4>
Expand All @@ -24291,6 +24309,7 @@ run;
,mdebug=0
,outds=_null_
,force=YES
,swap=0
);
%local dbg;
%if &mdebug=1 %then %do;
Expand Down Expand Up @@ -24335,6 +24354,12 @@ run;
%end;
%else %put %str(ERR)OR: invalid value for intype: &intype;

%if "&swap" ne "0" %then %do;
%mp_replace("%sysfunc(pathname(&fref))"
,findvar=%scan(&swap,1,%str( ))
,replacevar=%scan(&swap,2,%str( ))
)
%end;

%if &mdebug=1 %then %do;
data _null_;
Expand Down
3 changes: 2 additions & 1 deletion base/mp_replace.sas
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
LUA, you can also use this macro: mp_gsubfile.sas

@param [in] infile The QUOTED path to the file on which to perform the
substitution
substitution. Note that you can extract the pathname from a fileref using
the pathname function, eg: `"%sysfunc(pathname(fref))"`;
@param [in] findvar= Macro variable NAME containing the string to search for
@param [in] replacevar= Macro variable NAME containing the replacement string
@param [out] outfile= (0) Optional QUOTED path to the adjusted output file (to
Expand Down
45 changes: 35 additions & 10 deletions tests/viyaonly/mv_createfile.test.sas
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data _null_;
put 'hello testings';
run;
%mp_assertscope(SNAPSHOT)
%mv_createfile(path=&mcTestAppLoc/temp, name=&file..txt,inref=somefile,mdebug=1)
%mv_createfile(path=&mcTestAppLoc, name=&file..txt,inref=somefile,mdebug=1)
%mp_assertscope(COMPARE
,ignorelist=MCLIB0_JADP1LEN MCLIB0_JADP2LEN MCLIB0_JADPNUM
MCLIB0_JADVLEN MCLIB2_JADP1LEN
Expand All @@ -32,7 +32,7 @@ run;
)

%mp_assert(
iftrue=(%mfv_existfile(&mcTestAppLoc/temp/&file..txt)=1),
iftrue=(%mfv_existfile(&mcTestAppLoc/&file..txt)=1),
desc=Check if created file exists
)

Expand All @@ -42,10 +42,10 @@ data _null_;
file f2;
put '<html><body><p>Hello world</p></body></html>';
run;
%mv_createfile(path=&mcTestAppLoc/temp, name=test.html,inref=f2,mdebug=1)
%mv_createfile(path=&mcTestAppLoc, name=test.html,inref=f2,mdebug=1)

%mp_assert(
iftrue=(%mfv_existfile(&mcTestAppLoc/temp/test.html)=1),
iftrue=(%mfv_existfile(&mcTestAppLoc/test.html)=1),
desc=Check if created file exists
)

Expand All @@ -55,10 +55,10 @@ x=1;
run;
filename ds "%sysfunc(pathname(work))/temp.sas7bdat";

%mv_createfile(path=&mcTestAppLoc/temp, name=&file..sas7bdat,inref=ds,mdebug=1)
%mv_createfile(path=&mcTestAppLoc, name=&file..sas7bdat,inref=ds,mdebug=1)

%mp_assert(
iftrue=(%mfv_existfile(&mcTestAppLoc/temp/&file..sas7bdat)=1),
iftrue=(%mfv_existfile(&mcTestAppLoc/&file..sas7bdat)=1),
desc=Check if created dataset exists
)

Expand All @@ -68,19 +68,44 @@ data _null_;
file f4;
put '%put hello FromSASStudioBailey; ';
run;
%mv_createfile(path=&mcTestAppLoc/temp, name=test4.sas,inref=f4,mdebug=1)
%mv_createfile(path=&mcTestAppLoc, name=test4.sas,inref=f4,mdebug=1)

%mp_assert(
iftrue=(%mfv_existfile(&mcTestAppLoc/temp/test4.sas)=1),
iftrue=(%mfv_existfile(&mcTestAppLoc/test4.sas)=1),
desc=Check if created sas program exists
)



%put TEST 5 - reading from files service and writing back;
filename sendfrom filesrvc folderpath="&mcTestAppLoc/temp" filename='test4.sas';
filename sendfrom filesrvc folderpath="&mcTestAppLoc" filename='test4.sas';

OPTIONS MERROR SYMBOLGEN MLOGIC MPRINT;

%mv_createfile(path=&mcTestAppLoc/temp,name=test5.sas,inref=sendfrom,mdebug=1) ;
%mv_createfile(path=&mcTestAppLoc,name=test5.sas,inref=sendfrom,mdebug=1) ;

%put TEST 6 - try the find and replace;
filename f6 temp;
data _null_;
file f6;
put '//Hello world!';
put 'let var=/some/path/name;';
run;
%let in=/some/path/name;
%let out=/final/destination;
%mv_createfile(path=&mcTestAppLoc, name=test6.js,inref=f6,mdebug=1,swap=in out)

filename getback filesrvc folderpath="&mcTestAppLoc" filename='test6.js';

%let test6=0;
data _null_;
infile getback;
input;
if _infile_="let var=&out;" then call symputx('test6',1);
putlog _infile_;
run;

%mp_assert(
iftrue=(&test6=1),
desc=Check if find & replace worked
)
24 changes: 24 additions & 0 deletions viya/mv_createfile.sas
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
run;
%mv_createfile(path=/Public/temp,name=newfile.txt,inref=myfile)

The macro also supports find & replace (used by the SASjs Streaming App
build program). This allows one string to be replaced by another at the
point at which the file is created. This is done by passing in the NAMES of
the macro variables containing the values to be swapped, eg:

filename fref temp;
data _null_;
file fref;
put 'whenever life gets you down, Mrs Brown..';
run;
%let f=Mrs Brown;
%let r=just remember that you're standing on a planet that's evolving;
%mv_createfile(path=/Public,name=life.md,inref=fref,fin,swap=f r)


@param [in] path= The parent (SAS Drive) folder in which to create the file
@param [in] name= The name of the file to be created
Expand All @@ -38,6 +52,8 @@
@li sas_services
@param [in] force= (YES) Will overwrite (delete / recreate) files by default.
Set to NO to abort if a file already exists in that location.
@param pin] swap= (0) Provide two macro variable NAMES that contain the values
to be swapped, eg swap=find replace (see also the example above)
@param [out] outds= (_null_) Output dataset with the uri of the new file

@param [in] mdebug= (0) Set to 1 to enable DEBUG messages
Expand All @@ -51,6 +67,7 @@
@li mfv_getpathuri.sas
@li mp_abort.sas
@li mp_base64copy.sas
@li mp_replace.sas
@li mv_createfolder.sas

<h4> Related Macros</h4>
Expand All @@ -69,6 +86,7 @@
,mdebug=0
,outds=_null_
,force=YES
,swap=0
);
%local dbg;
%if &mdebug=1 %then %do;
Expand Down Expand Up @@ -113,6 +131,12 @@
%end;
%else %put %str(ERR)OR: invalid value for intype: &intype;

%if "&swap" ne "0" %then %do;
%mp_replace("%sysfunc(pathname(&fref))"
,findvar=%scan(&swap,1,%str( ))
,replacevar=%scan(&swap,2,%str( ))
)
%end;

%if &mdebug=1 %then %do;
data _null_;
Expand Down