BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Leo9
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

6 REPLIES 6
Reeza
Super User

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:

  1. Move the PUT to also be in a CALL EXECUTE()
  2. Switch DOSUBL
  3. Generate the list of file names ahead of time somehow and then do what ever you want next. 

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;


 

 

Patrick
Opal | Level 21

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;
qoit
Pyrite | Level 9

Sorry, just glanced at it now, the value (second argument) in the SYMPUT call routine should be "file_name" and not "filename".

Leo9
Quartz | Level 8

Thank you all who responded, appreciate it. 

Kurt_Bremser
Super User

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

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 532 views
  • 2 likes
  • 6 in conversation