DATA Step, Macro, Functions and more

how to use sas macro to replace a path

Accepted Solution Solved
Reply
Super Contributor
Posts: 345
Accepted Solution

how to use sas macro to replace a path

 

I find  “pipe” is quite helpful. I need to replace the old path with a different path so I am thinking to use a macro to do the work.

 

The old one:

filename DIRLIST pipe 'dir "C:\ab\c\*.txt" /b ';

 

data dirlist ;

   infile dirlist lrecl=200 truncover;

   input file_name $100.;

run;

 

I am thinking to define a sas macro like

 

%let datapath= d:\ce\fi\gh

And then put &datapath to replace the old path. It seems it does not work. Any advice how to define a sas macro to replace old path// no lib is needed

Thank you


Accepted Solutions
Solution
‎04-14-2017 12:19 PM
Super User
Super User
Posts: 7,060

Re: how to use sas macro to replace a path

Macro triggers are not resolved inside of single quotes. Use double quotes instead if you want SAS to recognize the reference to the macro variable.

%let datapath= d:\ce\fi\gh ;

filename DIRLIST pipe "dir ""&datapath\*.txt"" /b";

You can use the macro function %SYSFUNC() to call the QUOTE() function to make it easier. That way you do not need to double up the embedded double quote characters in the command string since the QUOTE() function will take care of that for you.

filename DIRLIST pipe %sysfunc(quote(dir "&datapath\*.txt" /b));

View solution in original post


All Replies
Solution
‎04-14-2017 12:19 PM
Super User
Super User
Posts: 7,060

Re: how to use sas macro to replace a path

Macro triggers are not resolved inside of single quotes. Use double quotes instead if you want SAS to recognize the reference to the macro variable.

%let datapath= d:\ce\fi\gh ;

filename DIRLIST pipe "dir ""&datapath\*.txt"" /b";

You can use the macro function %SYSFUNC() to call the QUOTE() function to make it easier. That way you do not need to double up the embedded double quote characters in the command string since the QUOTE() function will take care of that for you.

filename DIRLIST pipe %sysfunc(quote(dir "&datapath\*.txt" /b));
Frequent Contributor
Posts: 75

Re: how to use sas macro to replace a path

Trusted Advisor
Posts: 1,929

Re: how to use sas macro to replace a path


wenling wrote:

 

I find  “pipe” is quite helpful. I need to replace the old path with a different path so I am thinking to use a macro to do the work.

 

The old one:

filename DIRLIST pipe 'dir "C:\ab\c\*.txt" /b ';

 

data dirlist ;

   infile dirlist lrecl=200 truncover;

   input file_name $100.;

run;

 

I am thinking to define a sas macro like

 

%let datapath= d:\ce\fi\gh

And then put &datapath to replace the old path. It seems it does not work. Any advice how to define a sas macro to replace old path// no lib is needed

Thank you


Inside the single quotes, macro variables are not resolved, so &datapath will fail inside single quotes. Instead, try this:

 

filename DIRLIST pipe "dir ""&datapath\*.txt"" /b ";
Super Contributor
Posts: 345

Re: how to use sas macro to replace a path

Posted in reply to PaigeMiller

Thank you both of you. Yes, it works.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 162 views
  • 0 likes
  • 4 in conversation