BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bal23
Lapis Lazuli | Level 10

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
Tom
Super User Tom
Super User

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));
PaigeMiller
Diamond | Level 26

@Bal23 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 ";
--
Paige Miller
Bal23
Lapis Lazuli | Level 10

Thank you both of you. Yes, it works.

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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