BookmarkSubscribeRSS Feed
ReemaRoy
Fluorite | Level 6

I have a sas dataset(file_list) wherin I have kept the filenames of all files in a directory. I have a Macro that needs to pick all the filenames from file_list and perform some operation one after the other. Can you help me to read the file names as a parameter to macro in a loop or something?

 

work.file_list contains below records say

file1

file2

file3

...

...

 

data file_list;

...

run;

 

macro transfer_file(file_path=);

...

...

%mend transfer_file;

transfer_file(file_path=/user/MyFolder/file1);

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

A datastep is a loop, so you can use this to your advantage:

data _null_;
  set work.file_list;
  call execute(cats('%transfer_file(file_path=',thevar,');'));
run;

This assumes the file path is in a variable called thevar in file_list, replace that with your actual variable.  This will generate out a code line for each observation with the macro call which executes after the datastep has finished running.

ReemaRoy
Fluorite | Level 6

Thanks RW9,

 

Is the ,thevar, representing the column name?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Yes, just replace thevar with whatever your column name is.

Reeza
Super User
Look at the documentation for Call Execute, it has a fully worked example that helps to explain how to use it.
ReemaRoy
Fluorite | Level 6

I am getting an error saying ERROR: Macro parameter contains syntax error.

data _null_;
  set work.file_list;
  call execute(cats('%transfer_file(',file_path=fname,'));
run;

While I run the same thing with the actual value It runs successfully

data _null_;
  set work.file_list;
  call execute(cats('%transfer_file(',file_path=file1.xml,'));
run;

 

Is there anything missing I need to specify with 'fname' ?

Reeza
Super User

Here's a trick I use when using call execute. 

 

1. Don't use a _Null_ dataset, give it a name. 

2. Create a string variable that has the macro call. 

3. Open dataset and check if String variable is being created correctly. 

4. Pass string variable to CALL EXECUTE. 

 

Your call execute cats is incorrect. Untested but you should be able to debug it from here. 

 

data run_macros_list;
  set work.file_list;
string = cats('%transfer_file(file_path="', fname,'");");
  *call execute(string);
run;

Proc print;
Var string;
Run:

 


ReemaRoy
Fluorite | Level 6

Thanks RW9 and Reeza for your help,

 

I added   call symput('file_name', fname);

 

And it retrieved the filename correctly from dataset. I have used your datastep and followed the procedure written to debug. It has helped me to achieve my Goals. Thanks Smiley Happy

 

Cheers,

Reema

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

As @Reeza has stated you can save what is generated to a dataset, you an also get this information from the log as code generated is included, much like if you included another file, so it will appear with + before each code line.  Now just looking at your code I can tell the quotes are not in the right place:

  call execute(cats('%transfer_file(',file_path=fname,'));

                                                      ^                             ^

Between the two arrows here is where you problem lies.  To fix this move the quote:

  call execute(cats('%transfer_file(,file_path=',fname,');'));

Note how we are now concatenating the string:

%transfer_file(,file_path=

With the data from variable fname and then the string:

);

Thus if abc.txt is the filename

'%transfer_file(,file_path=' + 'abc.txt' + ');'

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2871 views
  • 3 likes
  • 3 in conversation