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);
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.
Thanks RW9,
Is the ,thevar, representing the column name?
Yes, just replace thevar with whatever your column name is.
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' ?
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:
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
Cheers,
Reema
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' + ');'
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: