Hi All,
I am trying to read in all .sas file from one location and then put in another file so I can run this file as batch file. I am creating a macro variable filename which is not getting resolved in the "put" statement below.
filename inprog pipe 'dir C:\test\*.sas /b';
filename dbatch "C:\test2\dbatch.sas";
data _null_;
infile inprog truncover;
input file_name $32.;
call execute ('data _null_ ; infile inprog truncover; input file_name $32. ; call symput("filename",filename) ; run;' );
file dbatch;
put '%include ' '"C:\test2\&filename." ' ';' ;
run;
Why do you want to create the macro variable? The current data step already has the value in a real variable. Probably easiest to just use DATA step code to prefix the directory name. Use the $QUOTE format to have the value printed with quotes for the %INCLUDE statement generation.
data _null_;
infile inprog truncover;
input file_name $256. ;
file_name = cats('C:\test1\',filename);
file dbatch;
put '%include ' file_name :$quote. ';' ;
run;
You're not understanding when the CALL EXECUTE will actually be processed. It generates all the code, but it doesn't actually execute anything until the end of the data step. Your PUT statement doesn't have the value accessible to execute it. Some workarounds include:
Did you know that %INCLUDE can be wild carded?
So you can use the code below to run all SAS scripts in a folder as well. Not sure why you're copying them from one location to another but you may want to look into FCOPY() instead of reading/re-writing the files. That forces you to process a file entirely, whereas FCOPY is a bulk move and should be faster.
%include 'path to folder/*.sas';
@Leo9 wrote:
Hi All,
I am trying to read in all .sas file from one location and then put in another file so I can run this file as batch file. I am creating a macro variable filename which is not getting resolved in the "put" statement below.
filename inprog pipe 'dir C:\test\*.sas /b';
filename dbatch "C:\test2\dbatch.sas";
data _null_;
infile inprog truncover;
input file_name $32.;
call execute ('data _null_ ; infile inprog truncover; input file_name $32. ; call symput("filename",filename) ; run;' );file dbatch;
put '%include ' '"C:\test2\&filename." ' ';' ;
run;
If I get that right and you just want to create a new file with %include statements then below code should do the job for you.
I've changed your inprog filename a bit so it hopefully can also deal with Windows paths that include folder names with blanks in the name.
%let source_dir=C:\test;
filename inprog pipe "dir ""%str(&source_dir)""\*.sas /b";
filename dbatch "C:\test2\dbatch.sas";
data _null_;
infile inprog truncover;
input file_name $32.;
/* file print;*/
file dbatch;
put '%include "' "%str(&source_dir)/" _infile_ '"/source2;';
run;
Sorry, just glanced at it now, the value (second argument) in the SYMPUT call routine should be "file_name" and not "filename".
Thank you all who responded, appreciate it.
The CALL EXECUTE (which only repeats your DATA _NULL_ step multiple times, always keeping the last filename in the macro variable) is not needed at all.
data _null_;
infile inprog truncover;
file dbatch;
input file_name $32.;
lenght line $50;
line = cats("%include 'C:\test2\",file_name,"'");
put line;
run;
Edit: added quotes around the pathname
Why do you want to create the macro variable? The current data step already has the value in a real variable. Probably easiest to just use DATA step code to prefix the directory name. Use the $QUOTE format to have the value printed with quotes for the %INCLUDE statement generation.
data _null_;
infile inprog truncover;
input file_name $256. ;
file_name = cats('C:\test1\',filename);
file dbatch;
put '%include ' file_name :$quote. ';' ;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.