06-29-2017 09:42 AM
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
06-29-2017 09:45 AM - edited 06-29-2017 09:45 AM
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.
07-02-2017 04:32 AM
I am getting an error saying ERROR: Macro parameter contains syntax error.
While I run the same thing with the actual value It runs successfully
Is there anything missing I need to specify with 'fname' ?
07-02-2017 10:57 AM
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:
07-05-2017 10:34 AM
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
07-03-2017 03:52 AM
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:
Between the two arrows here is where you problem lies. To fix this move the quote:
Note how we are now concatenating the string:
With the data from variable fname and then the string:
Thus if abc.txt is the filename
'%transfer_file(,file_path=' + 'abc.txt' + ');'