BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
1239
Calcite | Level 5

I have the below code wherein it will work if path is hard coded but it's not working when macro variable is referenced (it says 0 records read). Please help me in getting the issue resolved.

/**  Working fine without macro variable **/

***************************************************************************************************

%let path1 = C:\work\test\final output;

filename DIRLIST pipe 'dir "C:\work\test\final output" /B';

data dirlist ;

     length buffer $256 ;

     infile dirlist length=reclen ;

     input buffer $varying256. reclen ;

run ;

================================================================================================

/** Result **/

1684  %let path1 = C:\work\test\final output;

1685

1686  filename DIRLIST pipe 'dir "C:\work\test\final output" /B';

1687  data dirlist ;

1688

1689       length buffer $256 ;

1690

1691       infile dirlist length=reclen ;

1692

1693       input buffer $varying256. reclen ;

1694  run ;

NOTE: The infile DIRLIST is:

      Unnamed Pipe Access Device,

      PROCESS=dir "C:\work\test\final output" /B,

      RECFM=V,LRECL=256

NOTE: 2 records were read from the infile DIRLIST.

      The minimum record length was 3.

      The maximum record length was 3.

NOTE: The data set WORK.DIRLIST has 2 observations and 1 variables.

NOTE: Compressing data set WORK.DIRLIST increased size by 100.00 percent.

      Compressed is 2 pages; un-compressed would require 1 pages.

NOTE: DATA statement used (Total process time):

      real time           0.03 seconds

      cpu time            0.01 seconds

/**  Not Working with macro variable **/

***************************************************************************************************

%let path1 = C:\work\test\final output;

filename DIRLIST pipe 'dir "&path1." /B';

data dirlist ;

     length buffer $256 ;

     infile dirlist length=reclen ;

     input buffer $varying256. reclen ;

run ;

================================================================================================

/** Result **/

1695  %let path1 = C:\work\test\final output;

1696

1697  filename DIRLIST pipe 'dir "&path1." /B';

1698  data dirlist ;

1699

1700       length buffer $256 ;

1701

1702       infile dirlist length=reclen ;

1703

1704       input buffer $varying256. reclen ;

1705  run ;

NOTE: The infile DIRLIST is:

      Unnamed Pipe Access Device,

      PROCESS=dir "&path1." /B,RECFM=V,LRECL=256

Stderr output:

File Not Found

NOTE: 0 records were read from the infile DIRLIST.

NOTE: The data set WORK.DIRLIST has 0 observations and 1 variables.

NOTE: DATA statement used (Total process time):

      real time           0.03 seconds

      cpu time            0.03 seconds

1 ACCEPTED SOLUTION

Accepted Solutions
ScottBass
Rhodochrosite | Level 12

/**  Not Working with macro variable **/

***************************************************************************************************

%let path1 = C:\work\test\final output;

filename DIRLIST pipe 'dir "&path1." /B';

data dirlist ;

     length buffer $256 ;

     infile dirlist length=reclen ;

     input buffer $varying256. reclen ;

run ;

================================================================================================

/** Result **/

1695  %let path1 = C:\work\test\final output;

1696

1697  filename DIRLIST pipe 'dir "&path1." /B';

1698  data dirlist ;

1699

1700       length buffer $256 ;

1701

1702       infile dirlist length=reclen ;

1703

1704       input buffer $varying256. reclen ;

1705  run ;

NOTE: The infile DIRLIST is:

      Unnamed Pipe Access Device,

      PROCESS=dir "&path1." /B,RECFM=V,LRECL=256

Stderr output:

File Not Found

NOTE: 0 records were read from the infile DIRLIST.

NOTE: The data set WORK.DIRLIST has 0 observations and 1 variables.

NOTE: DATA statement used (Total process time):

      real time           0.03 seconds

      cpu time            0.03 seconds

Double up on the double quotes:

%let path1 = C:\work\test\final output;

filename DIRLIST pipe "dir ""&path1"" /B";

I think this is documented in the Concepts Guide somewhere, but it's been a while since I've read it.

However, for this scenario, I've gotten good results from DOPEN/DREAD.  In general, I prefer to use portable code when I can.  Check out the sample code here:  http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000209687.htm.  It's easily converted to a data step.  The only downside is no support for wildcards in DOPEN, but this is easily supported with prxmatch.

Untested:

data dirlist;

   length filename $200;

   rc=filename("dir","c:\temp");

   did=dopen("dir");

   do i=1 to dnum(did);

      filename=dread(did,i);

      if prxmatch("/^test.*\.sas/io",filename) then output;

   end;

   did=dclose(did);

   rc=filename("dir");

run;

This would keep all the files named test*.sas in c:\temp.

I admit the prxmatch syntax isn't quite as straightforward as the dir command's wildcards, but it's not hard once you get used to it.  Plus you don't need it if you want to read the entire directory (your example doesn't use wildcards in the dir command).

It may look like a lot of code but it performs quite well, esp. since you're not spawning an external process to execute the pipe.

HTH,

Scott


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

View solution in original post

3 REPLIES 3
LinusH
Tourmaline | Level 20

Macro variables doesn't resolve within single quotes.

Data never sleeps
ScottBass
Rhodochrosite | Level 12

/**  Not Working with macro variable **/

***************************************************************************************************

%let path1 = C:\work\test\final output;

filename DIRLIST pipe 'dir "&path1." /B';

data dirlist ;

     length buffer $256 ;

     infile dirlist length=reclen ;

     input buffer $varying256. reclen ;

run ;

================================================================================================

/** Result **/

1695  %let path1 = C:\work\test\final output;

1696

1697  filename DIRLIST pipe 'dir "&path1." /B';

1698  data dirlist ;

1699

1700       length buffer $256 ;

1701

1702       infile dirlist length=reclen ;

1703

1704       input buffer $varying256. reclen ;

1705  run ;

NOTE: The infile DIRLIST is:

      Unnamed Pipe Access Device,

      PROCESS=dir "&path1." /B,RECFM=V,LRECL=256

Stderr output:

File Not Found

NOTE: 0 records were read from the infile DIRLIST.

NOTE: The data set WORK.DIRLIST has 0 observations and 1 variables.

NOTE: DATA statement used (Total process time):

      real time           0.03 seconds

      cpu time            0.03 seconds

Double up on the double quotes:

%let path1 = C:\work\test\final output;

filename DIRLIST pipe "dir ""&path1"" /B";

I think this is documented in the Concepts Guide somewhere, but it's been a while since I've read it.

However, for this scenario, I've gotten good results from DOPEN/DREAD.  In general, I prefer to use portable code when I can.  Check out the sample code here:  http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000209687.htm.  It's easily converted to a data step.  The only downside is no support for wildcards in DOPEN, but this is easily supported with prxmatch.

Untested:

data dirlist;

   length filename $200;

   rc=filename("dir","c:\temp");

   did=dopen("dir");

   do i=1 to dnum(did);

      filename=dread(did,i);

      if prxmatch("/^test.*\.sas/io",filename) then output;

   end;

   did=dclose(did);

   rc=filename("dir");

run;

This would keep all the files named test*.sas in c:\temp.

I admit the prxmatch syntax isn't quite as straightforward as the dir command's wildcards, but it's not hard once you get used to it.  Plus you don't need it if you want to read the entire directory (your example doesn't use wildcards in the dir command).

It may look like a lot of code but it performs quite well, esp. since you're not spawning an external process to execute the pipe.

HTH,

Scott


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
1239
Calcite | Level 5

Hi, Scott. Double up on the double quotes is working fine. Thanks a lot for your helpSmiley Happy

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 1965 views
  • 0 likes
  • 3 in conversation