DATA Step, Macro, Functions and more

How to pass SAS observation value in macro one by one

Reply
Occasional Contributor
Posts: 5

How to pass SAS observation value in macro one by one

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);

Super User
Super User
Posts: 7,997

Re: How to pass SAS observation value in macro one by one

[ Edited ]

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.

Occasional Contributor
Posts: 5

Re: How to pass SAS observation value in macro one by one

Thanks RW9,

 

Is the ,thevar, representing the column name?

Super User
Super User
Posts: 7,997

Re: How to pass SAS observation value in macro one by one

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

Super User
Posts: 19,878

Re: How to pass SAS observation value in macro one by one

Look at the documentation for Call Execute, it has a fully worked example that helps to explain how to use it.
Occasional Contributor
Posts: 5

Re: How to pass SAS observation value in macro one by one

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' ?

Super User
Posts: 19,878

Re: How to pass SAS observation value in macro one by one

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:

 


Occasional Contributor
Posts: 5

Re: How to pass SAS observation value in macro one by one

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

 

Super User
Super User
Posts: 7,997

Re: How to pass SAS observation value in macro one by one

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' + ');'

Ask a Question
Discussion stats
  • 8 replies
  • 197 views
  • 3 likes
  • 3 in conversation